28
votes

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

3

3 Answers

20
votes

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.

9
votes

A Query can be passed to a Searcher to find documents. A Filter cannot; it can only modify the results produced by a Query.

Implementing a new Query type is fairly complicated, and requires an understanding of the relationship of Lucene internals like Weight, Scorer, and Similarity. A Filter implementation could be fairly simple, and not interact with the IndexReader at all.

0
votes

After you close a database, the filter's selection disappears. But when you close a Query, and open it again, it will still be there.

You can also create a Query using a Form. But you cannot use Filter in a Form.