Note: This feature is available in version 1.2 or later
One of the popular features of ActiveRecord is scopes. You can try using them with searches, but they will not impact search results, just the SQL queries used to gather the ActiveRecord objects - Sphinx itself does not use SQL for querying.
However, Thinking Sphinx does have sphinx scopes, which work in pretty much the same manner.
To add a scope to the model, use the
sphinx_scope method. This should be called within the class definition, not the index definition. You will also need to include the
ThinkingSphinx::Scopes module if you’re using Thinking Sphinx v3 or newer.
Just like ActiveRecord scopes, you can use arguments if you need to.
Once you have set up your scopes, you can use them on your model, just like you would use ActiveRecord’s scopes.
The search call is optional, if you don’t actually have any extra arguments to pass in.
Feel free to add to the chain at any point - Thinking Sphinx won’t populate the search results until you actually need them.
default_sphinx_scope method allows a sphinx scope to always be used when searching a model. This can be useful if all searches on a model require the scope is used.