| Module | ThinkingSphinx::ActiveRecord::ClassMethods |
| In: |
lib/thinking_sphinx/active_record.rb
|
Allows creation of indexes for Sphinx. If you don‘t do this, there isn‘t much point trying to search (or using this plugin at all, really).
An example or two:
define_index
indexes :id, :as => :model_id
indexes name
end
You can also grab fields from associations - multiple levels deep if necessary.
define_index do
indexes tags.name, :as => :tag
indexes articles.content
indexes orders.line_items.product.name, :as => :product
end
And it will automatically concatenate multiple fields:
define_index do
indexes [author.first_name, author.last_name], :as => :author
end
The indexes method is for fields - if you want attributes, use has instead. All the same rules apply - but keep in mind that attributes are for sorting, grouping and filtering, not searching.
define_index do
# fields ...
has created_at, updated_at
end
One last feature is the delta index. This requires the model to have a boolean field named ‘delta’, and is enabled as follows:
define_index do
# fields ...
# attributes ...
set_property :delta => true
end
Check out the more detailed documentation for each of these methods at ThinkingSphinx::Index::Builder.
Temporarily disable delta indexing inside a block, then perform a single rebuild of index at the end.
Useful when performing updates to batches of models to prevent the delta index being rebuilt after each individual update.
In the following example, the delta index will only be rebuilt once, not 10 times.
SomeModel.suspended_delta do
10.times do
SomeModel.create( ... )
end
end