Facet Searches are search summaries - they provide a breakdown of result counts for each of the defined categories/facets.
You define facets inside the define_index method, within your model. To specify that a field or attribute should be considered a facet, explicitly label it using the :facet symbol.
You cannot use custom SQL statements as string facet sources. Thinking Sphinx is unable to interpret the SQL within the context of the model, and strings can’t be stored as strings when they are attributes in Sphinx.
Even if you define your facet as a field, Thinking Sphinx duplicates it into an attribute, because facets are essentially grouped searches, and grouping can only be done with attributes.
Facets are available through the facets class method on all ActiveRecord models that have Sphinx indexes, and are returned as a subclass of Hash.
The facets method accepts the same options as the search method.
You can also explicitly request just certain facets:
To retrieve the ActiveRecord object results based on a selected
facet(s), you can use the
for method on a facet search result. When
using the for method, Thinking Sphinx will automatically CRC any
string values and use their respective
If you call
for without any arguments, then all the matching search
results for the initial facet query are returned.
Faceted searches can be made across all indexed models, using the same arguments.
By default, Thinking Sphinx does not request all possible facets, only those common to all models. If you don’t have any of your own facets, then this will just be the class facet, providing a summary of the matches per model.
To disable the class facet, just set :class_facet to false.
And if you want absolutely every facet defined to be returned, whether or not they exist in all indexed models, set :all_facets to true.
To get you started, here is a basic example displaying the facet options in a view:
Thinking Sphinx does not sort facet results. If this is what you’d prefer, then one option is to use Ruby’s sort or sort_by methods. Keep in mind you will then get arrays of two values (the facet value, and the facet count), instead of a hash key/value pair.
When you define fields as facets, then an attribute with the same
columns is created with the suffix
_facet. If the field is a string
(which is the case in most situations), then the value is converted to a
This CRC32 value is necessary as Sphinx currently doesn’t support true string attributes, and thus we need a value to filter and group by when determining the facet results.
In the above examples, we have the author’s name as a facet. This means there’s an author_facet attribute, which you could filter on with the following query:
This means you can step around the
for calls to get
results for specific facet arguments using
search (again, using