Forum: Ferret problems with acts_as_ferret

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.
24c01558b9bd537833ac70f032551ba3?d=identicon&s=25 William Monk (Guest)
on 2007-07-03 14:29
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] %><br/><br/>
<% 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
C9dd93aa135988cabf9183d3210665ca?d=identicon&s=25 Jens Kraemer (Guest)
on 2007-07-03 15:33
(Received via mailing list)
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:
<ul>
  <% @results.each do |result| %>
    <li><%= result.title %><br />
      Score: <%= result.ferret_score %>
    </li>
  <% end %>
</ul>


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


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
> Ferret-talk@rubyforge.org
> 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
kraemer@webit.de | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa
24c01558b9bd537833ac70f032551ba3?d=identicon&s=25 William Monk (Guest)
on 2007-07-03 16:12
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:


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


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
C9dd93aa135988cabf9183d3210665ca?d=identicon&s=25 Jens Kraemer (Guest)
on 2007-07-03 17:35
(Received via mailing list)
On Tue, Jul 03, 2007 at 04:12:09PM +0200, William Monk wrote:
> <div id="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

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
kraemer@webit.de | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa
24c01558b9bd537833ac70f032551ba3?d=identicon&s=25 William Monk (Guest)
on 2007-07-04 01:23
Jens Kraemer 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
This topic is locked and can not be replied to.