Filter Queries in Solr

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.

Try to solve the new Formula Cube! It works exactly like a Rubik's Cube but it is only $2, from China. Learn to solve it with the tutorial on or use the solver to calculate the solution in a few steps.

Write a comment
Cancel Reply