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-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:
> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Ferret-talk mailing list
> [email protected]
> 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
[EMAIL PROTECTED] | www.webit.de
Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk