On 9/8/06, Clare <[EMAIL PROTECTED]> wrote:
> Caspar
>
> I have been trying to get the same thing working for a while but did not
> ever find a solution. It would help greatly if someone has the answer to
> this because I want to add this capability to my search to provide
> additional information to the user in the results page.
>
> But I only got the :id from the index also... :(
>
> Any help would be appreciated on this one.
>
> Thanks in advance as always!

By default acts_as_ferret only stores the :id. You need to set the
:store parameter of any other fields that you want stored. Something
like this;

acts_as_ferret :fields => {
    :title => { :store => :yes }
    :content => { :store => :yes }
}

As for counting the the frequency of terms in a resultset,
IndexReader#doc_freq probably won't work. It will counts the frequency
of terms in the index, not in the resultset.

So back to the problem. Jens gave the solution I would probably use.
Ferret's searches are faster enough that this solution is quite
feasible for most indexes. Try it. You might be surprised.

The alternative is counting throught the resultset. To do this you
will need to set :limit => :all in the search_each method so you get
all results back, then iterate through each result counting the
occurances. For a huge index - slow query - small resultset this might
be faster. Also, with the new filter_proc method there is another way
you can do this without having to retrieve all results;

    require 'rubygems'
    require 'ferret'

    include Ferret

    index = I.new

    words = %w{one two three four five}

    100000.times do |i|
      index << {:id => "%05d" % i, :word => words[rand(words.size)]}
    end

    counter = Hash.new(0)
    filter_proc = lambda do |doc, score, searcher|
      counter[searcher[doc][:word]] += 1
    end

    resultset = index.search("id:[10000 20000}", :limit => 1,
:filter_proc => filter_proc)
    puts resultset.total_hits
    puts counter.inspect

Hope that helps,

Dave
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to