Should i replace the bool query with filter query?


(Hubo3085632) #1

i'm reading the es document,one chapter says filter will be cached and filter clause not be calculated in score.in my business scenario, i use es to search some eligible data then sorting them by date ,so i need't the score ,does query will be faster if i replace my bool query with filter query?


(Martijn Van Groningen) #2

In ES 1.7 or lower yes. If you are on ES 2.0 and later you should place
your query in the filter clause of the bool query [1].
This will make sure that individual query elements wrapped in the bool
query's filter cache are cached. In your case you're sorting by date and
then ES will never compute score. However if you were sorting by score the
query elements wrapped in the bool query's filter clause will be omitted
from score computation.

1:
https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-bool-query.html#_scoring_with_literal_bool_filter_literal


(Hubo3085632) #3

thank you,i got it.
here is my java query example :
"
QueryBuilders
.boolQuery()
.filter(QueryBuilders.termQuery("status", 1))
.filter(QueryBuilders.termQuery("gender", 1))
"
does it right?


(Martijn Van Groningen) #4

That looks all right.


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.