Facet Searches are search summaries - they provide a breakdown of result counts for each of the defined categories/facets.
You define facets inside your index definition. To specify that a field or attribute should be considered a facet, explicitly label it using the
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
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. Please note that you’ll need Sphinx 2.2 for filtering on string attributes.
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 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_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.