Aaf and dynamic attrs: a bug?

Hi!

I faced some issue while using it for dynamic attrs indexing/search.
Maybe I made something wrong. Here is test method. Everything works just
fine until last line Parked at Loopia . Tested on both
stable and trunk of aaf and ferret 0.11.4.

the short version of code below:

Contact.acts_as_ferret :fields => [ :first_name ]
assert Contact.find(:first).respond_to?(:first_name_to_ferret)

assert_equal 1, Contact.find_by_contents(‘Y*’).total_hits
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits

Contact.aaf_index.close
FileUtils.rm_rf ‘index’

Contact.acts_as_ferret :fields => [ :first_name, :last_name ]
assert Contact.find(:first).respond_to?(:last_name_to_ferret)

assert_equal 1, Contact.find_by_contents(‘Y*’).total_hits
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits

assert_equal 1, Contact.find_by_contents(‘last_name:K*’).total_hits
assert_equal 1, Contact.find_by_contents(‘K*’).total_hits # assertion
fails here: get 0 instead of 1 !!

Maybe there is any better solution to add new fields to existing index?

Best regards,
Yury K.
<;-) BrainHouse

web: http://www.brainhouse.ru
email: [email protected]

send all code we have so far and full test case code.

— model: contact.rb
class Contact < ActiveRecord::Base
end

— migration: 001_create_contacts_table.rb
class CreateContacts < ActiveRecord::Migration
def self.up
create_table :contacts do |t|
t.column :first_name, :string
t.column :last_name, :string
end
end

def self.down
drop_table :contacts
end
end

— fixture: contacts.yml
renat:
id: 1
first_name: Renat
last_name: Akhmerov
yura:
id: 2
first_name: Yury
last_name: Kotlyarov

— test: contact_test.rb

require File.dirname(FILE) + ‘/…/test_helper’
require ‘fileutils’

class ContactTest < Test::Unit::TestCase
fixtures :contacts

def setup
if File.exists?(‘index’)
FileUtils.rm_rf(‘index’)
end
end

def test_new_field
Contact.acts_as_ferret :fields => [ :first_name ]
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits
assert_equal 1, Contact.find_by_contents(‘Y*’).total_hits

Contact.aaf_index.close
FileUtils.rm_rf('index')

Contact.acts_as_ferret :fields => [ :first_name, :last_name ]

assert_equal 1, Contact.find_by_contents('last_name:K*').total_hits
# it fails on the following line!! a bug here?
assert_equal 1, Contact.find_by_contents('K*').total_hits

end
end

direct access to the index works - thanks to Thomas Nichols for the idea

— test: contact_test.rb
require File.dirname(FILE) + ‘/…/test_helper’
require ‘fileutils’
require ‘ferret’

class ContactTest < Test::Unit::TestCase
fixtures :contacts

def setup
if File.exists?(‘index’)
FileUtils.rm_rf(‘index’)
end
end

def test_new_field
Contact.acts_as_ferret :fields => [ :first_name ]
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits
assert_equal 1, Contact.find_by_contents(‘Y*’).total_hits

Contact.aaf_index.close
FileUtils.rm_rf('index')

Contact.acts_as_ferret :fields => [ :first_name, :last_name ]

assert_equal 1, Contact.find_by_contents('last_name:K*').total_hits

# it fails on the following line!! a bug here?
#assert_equal 1, Contact.find_by_contents('K*').total_hits

idx = Ferret::Index::Index.new(ath => 'index/test/contact',

:create_if_missing => false)
assert_equal 1, idx.search(‘last_name:K*’).total_hits
assert_equal 1, idx.search(‘K*’).total_hits
end
end

Yury K. wrote:

  t.column :first_name, :string

renat:
require File.dirname(FILE) + ‘/…/test_helper’

assert_equal 1, Contact.find_by_contents('last_name:K*').total_hits

Best regards,
Yury K.
<;-) BrainHouse

web: http://www.brainhouse.ru
email: [email protected]
skype: yura__115
phone: +7 905 758 1491
jabber: [email protected]

Got a workaround!

Adding following line solves the problem:

Contact.aaf_index.ferret_index.options[:default_field] << ‘last_name’

So is it a bug in aaf?

— here is full testrequire File.dirname(FILE) + ‘/…/test_helper’
require ‘fileutils’

class ContactTest < Test::Unit::TestCase
fixtures :contacts

def setup
if File.exists?(‘index’)
FileUtils.rm_rf(‘index’)
end
end

def test_new_field
Contact.acts_as_ferret :fields => [ :first_name ]
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits
assert_equal 1, Contact.find_by_contents(‘Y*’).total_hits

Contact.acts_as_ferret :fields => [ :first_name, :last_name ]
Contact.aaf_index.close
FileUtils.rm_rf('index')

Contact.aaf_index.ferret_index.options[:default_field] << 

‘last_name’
assert_equal 1, Contact.find_by_contents(‘K*’).total_hits
end
end

Yury K. wrote:

def setup
Contact.aaf_index.close
:create_if_missing => false)

— model: contact.rb
end
last_name: Akhmerov
class ContactTest < Test::Unit::TestCase
assert_equal 1, Contact.find_by_contents(‘first_name:Y*’).total_hits
end

Best regards,
Yury K.
<;-) BrainHouse

web: http://www.brainhouse.ru
email: [email protected]
skype: yura__115
phone: +7 905 758 1491
jabber: [email protected]