views:

110

answers:

2

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

+2  A: 

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.

erickson
Although what you're saying is correct in principal, Filters can be passed into Searchers as well but they work separately to queries
Khash
+2  A: 

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 seperatly 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.

Asaf