Showing posts with label search engine. Show all posts
Showing posts with label search engine. Show all posts

Wednesday, June 16, 2010

Ruby on Rails: Acts_as_xapian: Search engine (plugin)

Installing Xapian

The first step to installing Xapian is to download the latest version of xapian-core and extract:

curl -O http://oligarchy.co.uk/xapian/1.0.10/xapian-core-1.0.10.tar.gz
tar -xzvf xapian-core-1.0.10.tar.gz

From here, we need to configure and build the Xapian library:

cd xapian-core-1.0.10
./configure
make
sudo make install

Once this is complete, we need to download the Xapian bindings, which will let Xapian interact with Ruby, PHP and other scripting languages:

curl -O http://oligarchy.co.uk/xapian/1.0.10/xapian-bindings-1.0.10.tar.gz
tar -xzvf xapian-bindings-1.0.10.tar.gz

Again, now that we have downloaded them, we need to configure and build the Xapian-bindings:

cd xapian-bindings-1.0.10
./configure
make
sudo make install

Once that finishes, Xapian will be installed on your system. Of course, these instructions are for installing from source. If you are using a system that has aptitude installed, or another package manager, you can also install Xapian that way. For example, to install Xapian using aptitude:

aptitude search xapian

You need the following two files to complete the install:

libxapian15 - search engine library
libxapian-ruby1.8 - xapian search engine inferface for Ruby 1.8


Finally, simply enter the following commands to install the necessary components:

sudo aptitude install libxapian15
sudo aptitude install libxapian-ruby1.8

or

sudo apt-get install libxapian15 libxapian-ruby1.8

For more detailed instructions, visit http://xapian.org/docs/install.html.

Installing the acts_as_xapian Rails plugin

script/plugin install git://github.com/frabcus/acts_as_xapian.git vendor/plugins/acts_as_xapian

Once the plugin is installed, run the migrations to add the required tables to your database:

script/generate acts_as_xapian
rake db:migrate


Setting Up The Models Code

To include fields to your index, you will need to add an “acts_as_xapian” call to the model you want to index.

class Lesson < ActiveRecord::Base
acts_as_xapian :texts => [:name, :description]
end

In this case, only the data found in the “name” and “description” attributes of our Lesson model will be searched.

Building and Updating the Xapian Indexes

Xapian indexing takes place “offline,” which means that the index is not automatically updated after a model/database is changed. While this may change in the future, currently the index must be built and updated periodically by you, or using a cron job (See Crontab Reference).

At this point, you can quickly test that your Xapian install, plugin, and model code are all working together via the following rake commands:

To build the index:
rake xapian:rebuild_index models="Post"

** do cron job for rebuild_index for automatically updated after a model/database is changed
** if you have more than 1 model to build the index, rake xapian:rebuild_index models="ModelName1 ModelName2"
eg: rake xapian:rebuild_index models="Post Lesson"

To update index:
rake xapian:update_index

To test index:
rake xapian:query models="Post" query="golf"


In controller.rb

query = 'software'

Basic Search:
search = ActsAsXapian::Search.new([Post], query, :limit => 15)
@posts = search.results.collect {|p| p[:model]}

Similar Results:
similar = ActsAsXapian::Similar.new([Post], @posts, :limit => 15)
@similar_posts = similar.results.collect {|p| p[:model]}


In views.html.erb

<% for post in @posts %>
<%= post.name%><br>
<% end %>



Related Link:
http://github.com/frabcus/acts_as_xapian
http://terra-firma-design.com
http://locomotivation.squeejee.com
http://groups.google.com/group/acts_as_xapian
http://factore.ca/
http://blog.mattenoble.com/
Pagination - http://railsdog.com/

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

Wednesday, November 11, 2009

Ruby on Rails : Simple search engine using Ajax

After type words in the input field and wait awhile, the result will display at the partial page, no need to hit enter.

In views.rhtml
<%= javascript_include_tag "prototype" %>
Search : <%= text_field "article", "title" %>
<%= observe_field :article_title,
:frequency => 1.0,
:update => 'article_list',
:url => { :action => 'search' },
:with => "'search=' + encodeURIComponent(value)"
%>
<div id = 'article_list'>
<%= render :partial => 'article', :collection => @articles %>
</div>

In partial page, _article.rhtml
Title: <%= article.title %><br>
Author: <%= article.author %>
<hr>

In controller.rb
def search
@articles = Article.find(:all,
:conditions => ["lower(title) like ?", "%" + params[:search].downcase + "%"])

if params[:search].to_s.size < 1
@articles = Article.find(:all)
render :partial => 'article', :collection => @articles
else
if @articles.size > 0
render :partial => 'article', :collection => @articles
else
render :text => "Result not found!", :layout => false
end
end
end

** To wider search :
@articles = Article.find(:all, :conditions => ["lower(title) like ? and lower(type) like ?
and number like ?", "%" + params[:search][:title].downcase + "%",
"%" + params[:search][:type].downcase + "%", "%" + params[:search][:number] + "%"])

Prototype Helper : http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645