Ultrasphinx

Ruby on Rails configurator and client to the Sphinx full text search engine.

License

Copyright 2007 Cloudburst, LLC. Licensed under the AFL 3. See included LICENSE file. Portions copyright Dmytro Shteflyuk and Alexey Kovyrin, distributed under the Ruby License, and used with permission.

Requirements

  • MySQL (or Postgres, experimental)

  • Sphinx 0.97

Features

Advanced Sphinx usage:

  • multi-source merging

  • excerpt highlighting

  • field weighting

ActiveRecord-style SQL generation:

  • belongs_to and has_many includes

  • field merging

  • field aliasing

Good Rails integration:

  • automatic memcached loads via cache_fu

  • query spellcheck

  • Google-style query parser

  • multiple deployment environments

  • comprehensive rake tasks

And some other things.

Basic setup

Install the plugin, and also Sphinx itself (www.sphinxsearch.com).

Next, copy the example/default.base file to RAILS_ROOT/config/ultrasphinx/default.base.

If you need per-environment configuration, you can use RAILS_ROOT/config/ultrasphinx/development.base, etc. This file sets up the basic Sphinx and Ultrasphinx options such as ports, hosts, and index locations.

Now, in your models, use the is_indexed macro to configure a model as searchable. For example:

class Post
  is_indexed :fields => ["created_at", "title", "body"]
end

For more index options, see ActiveRecord::Base .is_indexed.

Building the index

Run:

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

To rotate the index, just rerun rake ultrasphinx:index. If the search daemon is running, it will have its index rotated. Otherwise the new index will be installed but the daemon will remain stopped.

Make sure to manually stop and restart the daemon if you change the field configuration or model set. It will not reload the configuration file automatically.

Running queries

Query the daemon as so:

@search = Ultrasphinx::Search.new(@query)
@search.run
@search.results

For more query options, including excerpt mode, see Ultrasphinx::Search.

Spellcheck

See Ultrasphinx::Spell.

Available Rake tasks

These rake tasks are made available to your Rails app:

ultrasphinx:configure

Rebuild the configuration file for this particular environment.

ultrasphinx:index

Reindex the database and send an update signal to the search daemon.

ultrasphinx:daemon:restart

Restart the search daemon.

ultrasphinx:daemon:start

Start the search daemon.

ultrasphinx:daemon:status

Check if the search daemon is running.

ultrasphinx:daemon:stop

Stop the search daemon.

ultrasphinx:spelling:build

Rebuild the custom spelling dictionary.

ultrasphinx:bootstrap

Bootstrap a full Sphinx environment by running configure, index, then daemon:start.

Further resources

blog.evanweaver.com/pages/code#ultrasphinx rubyforge.org/forum/forum.php?forum_id=14244