"Non scoring" queries

Hi,

I'm coming with 1.X experience and I'm facing with some 2.X new features and radical changes. I'm a bit confused.
in this part of the documentation you talk about filters and queries: https://www.elastic.co/guide/en/elasticsearch/guide/current/_queries_and_filters.html

From my understanding ES 2.X do not have separation between queries and filters anymore.
So what kind of construct do I have to use if I want do migrate a filter from 1.X to a query in 2.X?
If I want only the result (a direct answer) regardless of the score what kind of constructs do I have to use (possibly in Java API)?

Example:

How can I implement a non scoring query?

this way?

QueryBuilders.boolQuery().must( QueryBuilders.matchAllQuery() ).filter( QueryBuilders.termsQuery( "id", references ) );

Is this a non scoring query?
If I want more performant cosntructs can I bypass filters and use queries in another way?

Thanks for any help and regards,
Andrea

Lookup the query alternative for your filter (most filters have the same settings and name as their query alternative. For example range filter -> range query ) and wrap it in a bool query's filter clause.

Yes, the filter method makes queries non scoring. In your example the must clause with the match all query is not needed. Your query will be more performant if you remove this must clause.

Hi Martin,

Thank you very much for the answer.
I've another little doubt.
If do I have to perform a "not" query?
I saw from [1] that not filter and not query have been replaced by boolean must_not. But isn't must_not bool query scoring? If I want a "non-scoring" bool must_not?

for example:
QueryBuilders.boolQuery().filter( query ).mustNot( QueryBuilders.existsQuery( "alias" ) );

Does mustNot contribute to scoring? I guess no, but want only to be sure.

[1] https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-not-query.html

Best regards,
Andrea

No, must_not clauses never contribute to scoring. Only a bool query's must and should clauses contribute to scoring.

1 Like