Lucene: What is the difference between Query and Filter

mamu picture mamu · Sep 15, 2010 · Viewed 12.5k times · Source

Lucene query vs filter?

They both does similar things like termquery filters by term value, filter i guess is there for similar purpose.

When would you use filter and when query?

Just starting on lucene today so trying to clear concept

Answer

Asaf picture Asaf · Sep 15, 2010

Filter doesn't affect the computation of the score of the non-filtered documents.

For instance imagine the following docs:

1.
loc: "uk", "london"
text: "i live in london, "london is the best"

2.
loc: "london avenue", "london street", "london"
text: "I like the shop in london st."

now let's say you do the following query:

q=+loc:"london" +text:"london"

in this query the score of doc 2 is higher than that of doc 1 (because loc is calculated in the document score)

using a filter:

q=+text:"london" f=+loc:"london"

in this query the score of doc 1 is higher than that of doc 2.

Excuse the Solr style formatting but the overall notion is clear.

Other reasons for using filters are for caching purposes, filters are cached separately from queries so if you have a dynamic query with a static part it would make sense to filter by the static part. In this way the index traversal is limited to the subset of filtered docs.