Adding SEARCH functionality to a website

Hi there,

I’ve tried to implement ferret into my web application which would be
used to search the various models and return results based on the word
entered.

I cant get this to work, and wondered if anyone uses any other
searchable plugin for Ruby on Rails, and if so, what was best?

I don’t need anything specifically fancy – but something that at least
returns me some results.

Any Ideas?

If anyone has had success using ferret – please check out:
http://groups.google.co.uk/group/rubyonrails-talk/browse_thread/thread/683e0888c1c6a8cb

And see if you can help me out….

Cheers

use acts as ferret

On Tue, Oct 27, 2009 at 3:42 PM, RubyonRails_newbie <

thanks

I have done that and it doesn’t work. Nothing is returned.

see my ferret post

We r using it for a decade!!!

what have I missed?

I’m new to ruby on rails, and followed the tutorial to set it up…

What other config do I need???

u did not configured it properly.

On Tue, Oct 27, 2009 at 4:01 PM, RubyonRails_newbie <

RubyonRails_newbie wrote:

what have I missed?

I’m new to ruby on rails, and followed the tutorial to set it up…

What other config do I need???

How should we know, without any info on what errors you’re getting?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

There are no errors

On 27 Oct, 16:34, Marnen Laibow-Koser <rails-mailing-l…@andreas-

IN model…user.rb.

acts_as_ferret({:fields => {:get_text_for_fields_to_index => {}}})

def get_text_for_fields_to_index
[self.first_name, self.last_name, self.login, self.email,
self.basic_profile.about_me, self.basic_profile.city,
self.basic_profile.state, self.basic_profile.country,
self.personal_profile.activities, self.personal_profile.interests,
self.personal_profile.movies,
self.personal_profile.music, self.personal_profile.tv,
self.personal_profile.turn_ons,
self.personal_profile.turn_offs ].join(" ").strip
end

Inside controller…

def search
@query = params[:query].to_s.strip
conditions = [“users.is_test = ? and users.user_type != ?”, false,
User::ANONYMOUS]
@users = User.find_with_ferret(@query, {:per_page =>
@@items_per_page,
:page => params[:page]}, :conditions => conditions)
@total = @users.total_hits
@page_title = “Searched users for ‘#{@query}’ - fStation”
@meta_desc = "Searched users for #{@query} - " +
@users.collect(&:display_fullname).join(’,’)
render “jokes/search”
end

On Tue, Oct 27, 2009 at 4:06 PM, RubyonRails_newbie <

I have found a log file if it helps…

It doesn’t mean a lot to me though…

Query: username1
total hits: 0, results delivered: 0
[user] register class User with index user
[user] refusing re-registration of class User
index_for [User(id: integer, username: string, email: string,
password: string, created_at: datetime, updated_at: datetime,
authorization_token: string)]
options: {:page=>nil, :offset=>0, :limit=>:all}
ar_options: {}
[user] stored_fields: nil

On 27 Oct, 18:21, RubyonRails_newbie [email protected]

Update: I seem to have some success in returning a value in the log,
but it still isn’t displaying it in the browser…

the log shows

total hits: 2, results delivered: 2

however, why is the data not displaying in the browser?

Here’s how my controller looks so far:

def search

@title = " Search"
if params[:q]
query = params[:q]
# First find the user hits...

users = User.find_with_ferret(query, :limit => :all)

@users = users.collect { |user| info.user }

end

so the log finds 2 records, but nothing visible to the user… any
ideas?

On 27 Oct, 20:42, RubyonRails_newbie [email protected]

Ah actually, in Terminal there is this:

configured index for class Info:
{:ferret_fields=>{“username”=>
{:term_vector=>:with_positions_offsets, :boost=>1.0, :highlight=>:yes,
:via=>“username”, :store=>:no, :index=>:yes},
“email”=>
{:term_vector=>:with_positions_offsets, :boost=>1.0, :highlight=>:yes,
:via=>“email”, :store=>:no, :index=>:yes}}, :user_default_field=>nil,
:enabled=>true, :fields=>
[“username”, “email”], :index_dir=>"/Users/me/apps/ruby/project_test/
index/development/
info", :mysql_fast_batches=>true, :single_index=>false,
:index_base_dir=>"/
Users/me/apps/ruby/project_test/index/development/
info", :name=>:info, :reindex_batch_size=>1000,
:raise_drb_errors=>false, :registered_models=>
[Info(id: integer, user_id: integer, first_name: string, last_name:
string, gender: string, birthdate: date, city: string, ski_snowboard:
string)], :ferret=>{:default_field=>[“username”, “email”], :path=>"/
Users/me/apps/ruby/project_test/index/development/
info", :analyzer=>#<Ferret::Analysis::StandardAnalyzer:
0x26db044>, :lock_retry_time=>2, :auto_flush=>true, :or_default=>false,
:key=>:key, :dir=>#<Ferret::Store::FSDirectory:
0x26c8d04>, :handle_parse_errors=>true, :create_if_missing=>true}}

But I don’t get why it isnt returning anything

On 27 Oct, 16:34, Marnen Laibow-Koser <rails-mailing-l…@andreas-

I’ve changed that too, and that hasn’t changed the end result…

The log definately shows the user, but nothing in the browser…

what should i write to output the results?

On 27 Oct, 21:22, Marnen Laibow-Koser <rails-mailing-l…@andreas-

RubyonRails_newbie wrote:

Update: I seem to have some success in returning a value in the log,
but it still isn’t displaying it in the browser…

the log shows

total hits: 2, results delivered: 2

however, why is the data not displaying in the browser?

Here’s how my controller looks so far:

def search

@title = " Search"
if params[:q]
query = params[:q]
# First find the user hits...

users = User.find_with_ferret(query, :limit => :all)

@users = users.collect { |user| info.user }

Did you mean user.info ?

end

so the log finds 2 records, but nothing visible to the user… any
ideas?

On 27 Oct, 20:42, RubyonRails_newbie [email protected]
Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

RubyonRails_newbie wrote:

I’ve changed that too, and that hasn’t changed the end result…

The log definately shows the user, but nothing in the browser…

what should i write to output the results?

What does your view file look like?

On 27 Oct, 21:22, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I assume that you have tried this but if not it is worth a shot. If
your model was filled with data prior to setting up ferret, then I
don’t believe those rows will be indexed. You will need to build the
index or rebuild it. If you have not added any new data to the
database, you may very well have it all set up correctly but it will
return nothing. Try Model.rebuild_index, if you haven’t and try the
search function again. Also, it may be helpful to go to the script/
console to bypass your controllers and views to see what data is
coming back. Just my thoughts.

Andrew

what do you have in your views folders, specifically search.html or
search.html.erb?

Use sphinx instead of ferret
pat allan has written a beautiful plugin to incorporate sphinx
called thinking sphinx
i would advice you to use sphinx .

On 10/28/09, RubyonRails_newbie [email protected] wrote:

password: string, created_at: datetime, updated_at: datetime,

configured index for class Info:
:index_base_dir=>"/
0x26c8d04>, :handle_parse_errors=>true, :create_if_missing=>true}}


Posted viahttp://www.ruby-forum.com/.

Krishnaprasad V.

thanks for the comments - i will look into these, and if need be, try
sphinx.

On 28 Oct, 06:12, Krishnaprasad V. [email protected]

InventoryTrackers wrote:

I’ve been developing Rails apps now for about three years and I was
always mystified why there was no mainstream approach for running
searches against my models.
I didn’t like the ferret and sphinx approaches as they create MONSTER
index files that have to be deleted from time to time with a CRON job.
Instead, I stumbled upon a reasonably universal approach and have
followed this for all projects.
If you’re still in a quandry after you read this, send me an email and
I’ll copy you some parts of my code that will explain it.
Best of Luck,
David

Or just go to GitHub - marnen/quorum2: The Quorum calendar system. and look at the Search
class. (I’m not doing full-text searches yet, but adding them won’t be
hard when I get around to it.)

On Oct 27, 4:12�am, RubyonRails_newbie [email protected]

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]