This is the final type of faceting, and it offers a lot of flexibility. Instead of choosing a field to facet its values on or faceting a specified range of values, we specify some number of Solr queries so that each itself becomes a facet. For each facet query specified, the number of documents matching the facet query that also match the main search is counted. Each facet query with its facet count is returned in the results under the facet_queries
section. Facet queries are each cached in the filter cache.
There is only one parameter to configure facet queries:
In general, if field value faceting or range faceting don't do what you want, you can probably turn to facet queries. For example, if range faceting is too limiting because facet.range.gap
is fixed, then you could specify a facet query for each particular range you need. Let's use that scenario for our example. Here are search results showing a few facet queries on MusicBrainz release dates, using the /mb_artists
request handler. I've used echoParams
to make the search parameters clear instead of showing a lengthy URL:
{"responseHeader":{ "status":0, "QTime":80, "params":{ "facet":"on", "facet.query":[ "a_release_date_latest:[NOW/DAY-1YEAR TO *]", "a_release_date_latest:[NOW/DAY-5YEAR TO *]", "a_release_date_latest:[NOW/DAY-20YEAR TO *]" ], "wt":"json", "rows":"0", "echoParams":"explicit", "indent":"on"}}, "response":{"numFound":399182,"start":0,"docs":[]}, "facet_counts":{ "facet_queries":{ "a_release_date_latest:[NOW/DAY-1YEAR TO *]":0, "a_release_date_latest:[NOW/DAY-5YEAR TO *]":33, "a_release_date_latest:[NOW/DAY-20YEAR TO *]":80009}, "facet_fields":{}, "facet_dates":{}, "facet_ranges":{}}}
In this example, the facet.query
parameter was specified three times showing releases released in the past 1 year, 5 years, and 20 years. An interesting thing to note about the facet query response is that the name of each of these facets is the query itself.
Query facets with range queries allow arbitrary "bucket" value sizes. Solr 4.10 supports a new interval facet that also provides this capability. In some cases, this facet type may perform better than query/range faceting. Details can be found in the reference guide at https://cwiki.apache.org/confluence/display/solr/Faceting#Faceting-IntervalFaceting.