Thursday, November 12, 2009

Ruby on Rails : Thinking Sphinx : Search Engine (gem)

Sphinx is a very fast search engine that indexes data and provides flexible ways of searching it. Thinking Sphinx allows you to link up your models into Sphinx simply and painlessly -– because let’s face it, searching across multiple fields using SQL is a pain in the neck.

Step by step install Sphinx.

Basic files need to edit and according files to apply the function :
1. /config/environment.rb
2. /config/sphinx.yml
3. /app/controllers/articles_controller.rb
4. /app/models/article.rb
5. /app/views/articles/index.rhtml
6. /app/views/articles/_searchResult.rhtml

1. Add few line to /config/environment.rb
Rails::Initializer.run do |config|
config.gem(
'thinking-sphinx',
:lib => 'thinking_sphinx/0.9.8',
:version => '1.3.3'
)
end

2. /config/sphinx.yml
development:
enable_star: 1
min_infix_len: 1
test:
enable_star: 1
min_infix_len: 1
production:
enable_star: 1
min_infix_len: 1

3. /app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def SphinxSearch
@articles = Article.search '*book*', :with => {
:created_at => 8.week.ago..Time.now,
}

render :partial => 'searchResult', :collection => @articles
end
end

4. /app/models/article.rb
class Article < ActiveRecord::Base
define_index do
# search from articles db table
indexes title, :sortable => true, :facet => true
indexes author, :sortable => true, :facet => true
indexes type, :sortable => true, :facet => true

# search from publishers db table
indexes publishers(:name), :as => :publishers_name, :sortable => true

# search from books db table
indexes books(:description), :as => :books_description, :sortable => true

end
end

** When you make changes to your Sphinx index structure, you will need to stop and start Sphinx for these changes to take effect, as well as re-index the data. This is all wrapped up into a single task:
rake thinking_sphinx:rebuild
rake ts:rebuild

5. /app/views/articles/index.rhtml
<% form_remote_tag(:url => {:action => 'SphinxSearch'}, 
:update => "resultList") do %>
<%= submit_tag 'Search' %>
<% end %>

<div id="resultList">
<!-- Search Result will be displayed here -->
</div>

6. /app/views/articles/_searchResult.rhtml
<% if articles.length != 0 %>
<% for article in articles %>
Id : <%= article.id %><br>
Author : <%= article.author %><br>
Type : <%= article.type %><hr>
<% end %>
<% else %>
No record found!
<% end %>


Sphinx
http://www.sphinxsearch.com/

Ultrasphinx

http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html

Thinking Sphinx

http://freelancing-god.github.com/ts/en/
http://groups.google.com/group/thinking-sphinx

2 comments:

  1. Hello ! this is an old article but I wanted to complete response , nowadays, the rightest gem to use to manage a search engine is elasticsearch gem :)

    ReplyDelete
  2. It is nice blog Thank you porovide importent information and i am searching for same information to save my time thankyou for sharing importent informaation.Ruby on Rails Online Training Hyderabad

    ReplyDelete