Posted on 10-10-2008
Filed Under (inglês, ruby on rails, search engine) by David William

In this post I would like to share how to use Sphinx through the Ultrasphinx plugin.

I wrote another post about the basic things you should know about Sphinx. If Sphinx is something real new for you, I highly recommend you read it before continue.

First, a quick explanation about Sphinx:

Sphinx is a full-text search engine. The main goal is to provide good and fast search with a lot of options to increase the results’s relevance.

The distribution includes four programs: indexer (to create fulltext indices), search (to query fulltext indices from command line), searchd (a daemon to search through fulltext indices from external software - the most common case) and sphinxapi (a set of API libraries).

Why to use Sphinx?

It’s easy to understand after take a look on Sphinx features list. Basicly, if you want high indexing and search speed, high scalability, good relevance, boolean, phrase, and word proximity queries, stopwords support, English and Russian Stemming, then, I can tell you, I’ll really want to try Sphinx.

Quick tour

Download Sphinx:
http://www.sphinxsearch.com/downloads.html
Get the latest release.

Extract everything:

$ tar xvzf sphinx-sphinx-0.9.8.tar.gz
$ cd sphinx

Then, configure:

$ ./configure

Despite the variety of options you have to configure, trust me: if you are getting started, just run ./configure with no options. By default, it will install MySQL database. Later, you can try other options, but first it will be better to see how everything works. To see all the options for configure, use –help switch.

Build the binaries:

$ make

Install the binaries:

$ (sudo) make install

Were’re almost there.

Now, the plugin: Ultrasphinx

Ultrasphinx is a Ruby on Rails configurator and client to the Sphinx fulltext search engine. I just started to use it. There’s a lot of things to learn. I’m enjoing.

Ultrasphinx features:

  • searching and ranking across multiple models
  • delta index support
  • excerpt highlighting
  • Google-style query parser
  • spellcheck
  • faceting on text, date, and numeric fields
  • field weighting, merging, and aliasing
  • geodistance
  • belongs_to and has_many includes
  • drop-in compatibility with will_paginate
  • drop-in compatibility with Interlock
  • multiple deployment environments
  • comprehensive Rake tasks

Farther then Sphinx, you also need the chronic gem:

$ sudo gem install chronic

So, you’re ready to install the plugin:

$ script/plugin install -x svn://rubyforge.org/var/svn/fauna/ultrasphinx/trunk

Or you just download Ultrasphinx (yourapp/vendor/plugin/ultrasphinx/):

http://github.com/fauna/ultrasphinx/tree/master

Inside the plugin’s directory, there is the file examples/default.base. Copy it to the config directory (inside your app root):

$ cp vendor/plugin/examples/default.base yourapp/config/ultrasphinx/default.base

Let’s say you are working in a Book Store project. Assuming you have a Book model, use is_indexed method to configure a model as searchable:

Class Book < ActiveRecord::Base
  is_indexed :fields => ['title', 'author']
end

To build the index, run:

$ rake ultrasphinx:configure
$ rake ultrasphinx:index
$ rake ultrasphinx:daemon:start

Or, just run:

$ rake ultrasphinx:bootstrap  #(do the same thing - three in one)

Now It’s time to see some action

In books_controller.rb, create a new method:

def search
  @books = Ultrasphinx::Search.new(:query => params[:query])
  @books.run
  @books.results
end

Assuming you have the view search.html.erb, let’s write a new entry in config/routes.rb

map.search  '/books/search/:query', :controller => 'books', :action => 'search'

So now, we can run a search using Sphinx trhough Ultrasphinx plugin, just typing the url with the criteria in the very end. In this example, we are gonna search for “Scott Adams” in our Books index. Remember: We have indexed title and author.

http://localhost:3000/books/search/scott adams

Easy like that.

By default, the search is case insensitive and symbols irrelevant. Of course, you can add some interesting features to you search, such as pagination and weights:

def search
  @musics = Ultrasphinx::Search.new(
        :query => params[:query],
        :per_page => 30,
        :weights => { 'author' => 2.0} # 1.0 is the default value
        )
  @books.run
  @books.results
end

If you want to learn more about Class:Ultrasphinx::Search, go to:

http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/classes/Ultrasphinx/Search.html

That’s it. Hope you enjoy.

See you.

    Read More   
Post a Comment
Name:
Email:
Website:
Comments: