views:

37

answers:

1

I use TS for full text search in my rails app. I am trying to save the search term to present "most searched" type list in my app. Here is my search controller index action. I notice that with the "save" the search feature the search takes about 1.28s and without it about 1.04s.

Couple of questions.

1- Is there a better way to do this, so that I don't add the extra time to the search?

2 - What's in general the best way to speed up full text searching outside of following the standard best practices of TS or Sphinx i.e is there any kind of caching or something like that?

Thanks

def index
        terms = params[:search_term]
        terms ||= ""
        if params[:city]
            @search_results  = Post.search terms, :conditions => {:city => params[:city]}, :page => params[:page] || 1, :per_page => Constants::LISTINGS_PER_PAGE
        elsif params[:state]
            @search_results  = Post.search terms, :conditions => {:state => params[:state]}, :page => params[:page] || 1, :per_page => Constants::LISTINGS_PER_PAGE
        else
            @search_results  = Post.search terms, :page => params[:page] || 1, :per_page => 3
        end
#        if @search_results.total_entries > 0
#            Search.create(:term => terms)
#        end
        respond_to do |format|
            format.html
            format.js
        end
    end
+1  A: 

Thinking out loud, maybe you can use Delayed::Job to save the search term, behind the scenes. No one really should have to wait for their search results while you compile your own stats, imo. mind you it's only .280 of a second, still.

check out tobi's delayed job on github if you are unfamiliar with it.

pjammer
I use delayed, not for this purpose though. I haven't tried this yet, but I wonder if starting creating the delayed job would take less than .280 seconds. My question was for understanding the general best practices in rails as it relates to searching. thanks for your suggestion!
badnaam