Filter Queries in Solr


By: Shelly Singh | May 15, 2015

Solr provides Query (q parameter) and Filter Query (fq parameter) for searching.

The query (q parameter), as the name suggests, is the main query used for searching. Example

q = title:james

Filter queries are used alongside query (q parameter) to limit results of queries using additional filters.  Example

q = title:james & fq = price: [0 TO 10]

(Note : You cannot have a filter query if you do not have a main query (q parameter) in the first place).

The good things about filter queries is that they are cached. The filter cache stores the results of any filter queries (“fq” parameters) that Solr is explicitly asked to execute. Each filter is executed and cached separately. When it’s time to use them to limit the number of results returned by a query, this is done using set intersections.

While caching is the default behavior for some advanced scenarios, you can tell Solr to not cache your filter query result using local param chache, as in,

 fq = {!cache=false}year:[2014 TO *]

Yet, the filter query does not impact the score or ranking of the results; it only eliminates those which do not pass through the filter.

What would be an efficient query for your application depends on your application’s searching pattern. Make use of caching to the best but do not overdo it.

One common way used in several e-commerce applications which have heavy faceting and categorization is to have the main query returning all results and all criteria is applied as filter queries. Like demonstrated below-

q = *:*

fq = price:[1 to 10] (a filter)

fq = genre_s:fantasy (another filter)

… and so on.

This promotes good re-use. Also, don’t shy away from complex filter queries if you know that the complex combination will be used repeatedly. For example,

fq = rating:[4 TO 5] AND inStock:T

But, filter queries like all other features should be used thoughtfully. For uncommon searches, or user-entered search strings, it may just incur needless overhead to cache the results, and pollute the cache with a useless result set.

This post has been viewed 4,523 times

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>