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

thanks

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

see my ferret post

u did not configured it properly.

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???

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

There are no errors

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

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

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?

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 do you have in your views folders, specifically search.html or search.html.erb?

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,

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

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 .

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

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

Are you talking about database searches? I’ve always built them myself, is that an option for you?

Do you pass @users to the page correctly? Can you set breakpoints in you ide and confirm that @users actually has a list of hits?