Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c29da29b7c
Fetching contributors…

Cannot retrieve contributors at this time

139 lines (92 sloc) 4.454 kb
layout title
ts_en
Indexing

Indexing your Models

Everything to set up the indexes for your models goes in the define_index method, within your model. Don’t forget to place this block below your associations, otherwise any references to them for fields and attributes will not work.

{% highlight ruby %}
class Article < ActiveRecord::Base

define_index do indexes subject, :sortable => true indexes content indexes author(:name), :as => :author, :sortable => true has author_id, created_at, updated_at end

  1. end
    {% endhighlight %}

Fields

The indexes method adds one (or many) fields, by referencing the model’s column names. You cannot reference model methods – Sphinx talks directly to your database, and Ruby doesn’t get loaded at this point.

{% highlight ruby }
indexes content
{ endhighlight %}

Keep in mind that if you’re referencing a column that shares its name with a core Ruby method (such as id, name or type), then you’ll need to specify it using a symbol.

{% highlight ruby }
indexes :name
{ endhighlight %}

You don’t need to keep the same names as the database, though. Use the :as option to signify an alias.

{% highlight ruby }
indexes content, :as => :post
{ endhighlight %}

You can also flag fields as being sortable.

{% highlight ruby }
indexes subject, :sortable => true
{ endhighlight %}

Use the :facet option to signify a facet.

{% highlight ruby }
indexes authors.name, :as => :author, :facet => true
{ endhighlight %}

If there are associations in your model, you can drill down through them to access other columns. Explicit aliases are required when doing this.

{% highlight ruby }
indexes author(:name), :as => :author
indexes author.location, :as => :author_location
{ endhighlight %}

There may be times when a normal column value isn’t exactly what you’re after, so you can also define your indexes as raw SQL:

{% highlight ruby %} indexes "LOWER(first_name)", :as => :first_name, :sortable => true {% endhighlight %}

Again, in this situation, an explicit alias is required.

Attributes

The has method adds one (or many) attributes, and just like the indexes method, it requires references to the model’s column names.

{% highlight ruby }
has author_id
{ endhighlight %}

The syntax is very similar to setting up fields. You can set aliases, and drill down into associations. You don’t ever need to label an attribute as :sortable though – in Sphinx, all attributes can be used for sorting.

Also, just like fields, if you’re referring to a reserved method of Ruby (such as id, name or type), you need to use a symbol (which, when dealing with associations, is within a method call).

{% highlight ruby }
has :id, :as => :article_id
has tags(:id), :as => :tag_ids
{ endhighlight %}

Conditions and Groupings

Because the index is translated to SQL, you may want to add some custom conditions or groupings manually – and for that, you’ll want the where and group_by methods:

{% highlight ruby %}
define_index do

where “status = ‘active’” group_by “user_id”

end
{% endhighlight %}

SQL

As previously mentioned, your index definition results in SQL from the indexes, the attributes, conditions and groupings, etc. With this in mind, it may be useful to simplify your index.

One way would be to use something like ActiveRecord::Base.sanitize_sql to generate the required SQL for you. For example:

{% highlight ruby %}
define_index do

where sanitize_sql([“published”, true])

end
{% endhighlight %}

This will produce the expected WHERE published = 1 for MySQL.

Processing your Index

Once you’ve got your index set up just how you like it, you can run the rake task to get Sphinx to process the data.

{% highlight sh }
rake thinking_sphinx:index
{ endhighlight %}

As each model is processed, you will see a message much like the one below. It is just a warning, not an error. Everything will work fine.

{% highlight sh }
distributed index ‘article’ can not be directly indexed; skipping.
{ endhighlight %}

However, if you have made structural changes to your index (which is anything except adding new data into the database tables), you’ll need to stop Sphinx, re-index, and then re-start Sphinx – which can be done through a single rake call.

{% highlight sh }
rake thinking_sphinx:rebuild
{ endhighlight %}

Jump to Line
Something went wrong with that request. Please try again.