1

I am applying to two search requests a filter and a query semantically identical like so:

static FilterBuilder filter(String field1Value, String field2Value){
    return FilterBuilders.boolFilter().must(FilterBuilders.termFilter("field1",field1Value)).should(FilterBuilders.termFilter("field2",field2Value));
}
static QueryBuilder query(String field1Value, String field2Value){
    return QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field1",field1Value)).should(QueryBuilders.termQuery("field2",field2Value));
}
client.prepareSearch(indexName).setPostFilter(filter("hello", "world")).setTypes("mytype");
client.prepareSearch(indexName).setQuery(query("hello","world")).setTypes("mytype");

However, while the search with the query returns results, the search with the filter doesn't return any result. Aren't the two suppose to behave identically and if not, why?

3
  • Don't use PostFilter. It removes documents after the query stage. This is meant to remove documents after aggregating against some superset. You want to use the filteredQuery that contains your filter. Commented Jul 6, 2015 at 23:16
  • Thanks although I oversimplified. I am doing a filtered aggregation, is that a bad idea? Commented Jul 7, 2015 at 8:31
  • Filtered aggregation is a good use. You really just want to be sure that you really want to do a post filter. The other types of filters are harder to misuse. Commented Jul 7, 2015 at 14:02

1 Answer 1

1

They are not exactly the same. In a bool query with a must clause a document would be a match if none of the clauses in should are matched provided there is no explicit minimum_should_match in the query.

In filter bool query at-least one should clause needs to be satisfied for a document to be considered a match. In filters there is no option of minimum_should_match and can be treated as always set to one.

i.e for filters it can be viewed as follows

 [must_clause] && [should_clause1 || should_clause_2]

For the example in the OP :

1) the documents would pass the filter if and only if they match field1 criteria in must clause and field2 criteria in should clause .

2) Whereas for bool query it would suffice for a document to be considered a match if must-clause is satisfied i.e field1 match

Sign up to request clarification or add additional context in comments.

1 Comment

In both queries there's a must though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.