Problems with acts_as_ferret


#1

Hi,

I have i am trying to add a search feature to a ruby on rails blog, so
ive decided to use ferret. So far i have had quite a few problems with
it, from following a few tutorials i didnt really understand… i am at
the point where i can make a search and it returns the score of the
result. I want it to also show the title of the post and i think i have
implemented it correctly but it doesn’t.

This is the code in my search.rhtml:

<% @results.each_with_index do |result, index| %>
<%= result[:title] %>
Score: <%= result[:score] %>


<% end %>

And this is the controller:

def search
@query = params[:q]
@total, @results = Post.find_storage_by_contents(@query, :page =>
(params[:page]||1))
@pages = pages_for(@total)
end

This is the code from post.rb:

def self.find_storage_by_contents(query, options = {})
# Get the index that acts_as_ferret created for us
index = self.aaf_index.ferret_index
results = []

# search_each is the core search function from Ferret, which

Acts_as_ferret hides
total_hits = index.search_each(query, options) do |doc, score|
result = {}

  # Store each field in a hash which we can reference in our views
  result[:title] = index[doc][:title]


  # We can even put the score in the hash, nice!
  result[:score] = score

  results.push result
end
return block_given? ? total_hits : [total_hits, results]

end

there is probably something i have missed before this is able to work,
if u have any ideas please help! thanks, Will


#2

Hi!

Why do you try doing this the hard way?
There’s really no reason to use Ferret directly for a blog search
once you have acts_as_ferret in your Rails app. Here’s the simplest
possible example:

Model:
class Post
acts_as_ferret :fields => { :title => {}, :content => {} }
end

Controller:
def search
@results = Post.find_by_contents(query)
end

View:

    <% @results.each do |result| %>
  • <%= result.title %>
    Score: <%= result.ferret_score %>
  • <% end %>

For implementing paging across your result set, please check out
http://www.railsenvy.com/2007/2/19/acts-as-ferret-tutorial#pagination

Regarding your initial problem with missing titles - acts_as_ferret by
default stores no attributes but the id of your records in the ferret
index, to keep index size small. To get around this specify :store =>
:yes in the field options for your title field:

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

However as I said above, just go the easy way…

Jens

On Tue, Jul 03, 2007 at 02:29:15PM +0200, William Monk wrote:

(params[:page]||1))
# search_each is the core search function from Ferret, which

Ferret-talk mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/ferret-talk


Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#3

Hi,

Thanks for your really fast reply. I am quite new to rails and couldnt
find anything which was pointing me in the right direction apart froms
some complicated tutorials. I have done what you have said, but i am not
sure what the form should look like to give results, this is what i am
using at the moment, plus everything you told me:

<% form_tag({:action => :search}, :method => 'get') do %> <%= text_field_tag :query, params[:query] %><%= submit_tag 'Search', :id => 'hide'%> <% end %>

but i get an error:

TypeError in BlogController#search

wrong argument type Symbol (expected Data)

im not sure if this is to do with the posts being indexed incorectly,
but if you could help again that would be great! Thanks Will


#4

On Tue, Jul 03, 2007 at 04:12:09PM +0200, William Monk wrote:

wrong argument type Symbol (expected Data)

im not sure if this is to do with the posts being indexed incorectly,
but if you could help again that would be great! Thanks Will

I had a small typo in the controller, it should read

find_by_contents(params[:query])

Jens


Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#5

Jens K. wrote:

I had a small typo in the controller, it should read

find_by_contents(params[:query])

Thanks a lot Jens, you have been a massive help for getting search to
work, and now it works perfectly.

Thanks! Will