Getting a query into filter context

(Brian Hudson) #1

If I have a simple query like:

termQuery("name", "Brian")

What is the suggested method of getting that query into filter context in Elasticsearch 2.0?

Wrapping it in a boolQuery?

boolQuery().filter(termQuery("name", "Brian"))

(Zachary Tong) #2


(Assuming you want to run it as a filter e.g. no scoring)

(Brian Hudson) #3

Great thanks.

Does a top level boolQuery().filter(someBigCompoundQuery) put all of the subqueries into filter context or do I need to put the sub-queries into filter context as well?

Does a "must" inside a "filter" open a useless query context?
(Zachary Tong) #4

Hmm, I'm not sure I understand?

If you add queries to the must / must_not / should clauses of a bool, they will be executed as queries. If you add them to the filter, they turn into a filtering, non-scoring context.

(Brian Hudson) #5

If I have something like

boolQuery().filter(boolQuery().must(termQuery("fname", "Brian"))
                              .must(termQuery("lname", "Hudson"));

Are the term queries now in filter context and thus individually eligible for caching?

(Zachary Tong) #6

Ah, I see. Yes, you are correct. When you place those queries in a filter context, they will execute as a filter with no scoring, caching, etc.

(system) #7