Forum: Ferret aaf and dynamic attrs: a bug?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2007-05-31 19:05
(Received via mailing list)
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 http://pastie.caboo.se/66274 . 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 Kotlyarov
<;-) BrainHouse

web: http://www.brainhouse.ru
email: yura@brainhouse.ru
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2007-05-31 22:38
(Received via mailing list)
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
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2007-05-31 23:20
(Received via mailing list)
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 Kotlyarov 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 Kotlyarov
<;-) BrainHouse

web: http://www.brainhouse.ru
email: yura@brainhouse.ru
skype: yura__115
phone: +7 905 758 1491
jabber: yury.kotlyarov@jabber.ru
94ef1c99f4d4567563f576783979804f?d=identicon&s=25 Yury Kotlyarov (yura)
on 2007-06-01 00:01
(Received via mailing list)
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 Kotlyarov 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 Kotlyarov
<;-) BrainHouse

web: http://www.brainhouse.ru
email: yura@brainhouse.ru
skype: yura__115
phone: +7 905 758 1491
jabber: yury.kotlyarov@jabber.ru
This topic is locked and can not be replied to.