ElasticSearch - different result ordering for simple request and aggregation request (NEST)
Your query has problems.
What you are using is combination of
must
andshould
inside amust
as part ofbool
query.So if you read more in this link, you can see for
must
The clause (query) must appear in matching documents and will contribute to the score.
so it will five equal scoring to all your documents which matched the condition. Any other condition which didn't match the condition won't even be there in results to score.
What you should do it use
should
query but outside ofmust
query, so Elasticsearch will be able to score your documents correctlyFor more info as part of this question
Can someone direct me to the right path how to solve this problem?
you should pass
'explain': true
in the query. You can read more about explain query and how to interpret results in this link.You answer for this question is
how can I achieve the same order in the results?
As every score is same therefore Elasticsearch can sort the result in any way it gets the response from its nodes.
Possible Solution:
You should reorganize your query to make real use of should
query and its boosting capabilities. You can read more about boosting here.
I tried two query similar to yours but with correct usage of should
and they gave me same order as expected. Your both query should be constructed as below:
{ "from": 0, "size": 10, "_source": [ "content1^3", "content2^2", "content3^1" ], "query": { "bool": { "should": [ { "match": { "languageId": 1 } }, { "match": { "languageId": 0 } } ], "must": [ { "multi_match": { "boost": 1.1, "query": ".....", "fuzzy_rewrite": "constant_score_boolean", "fuzziness": 1, "cutoff_frequency": 0.5, "prefix_length": 1, "max_expansions": 100, "slop": 2, "lenient": true, "tie_breaker": 1, "minimum_should_match": 2, "operator": "or", "fields": [ "content1^3", "content2^2", "content3^1", "category" ], "zero_terms_query": "all" } } ] } }}
and second query as
{ "size": 0, "query": { "bool": { "should": [ { "match": { "languageId": 1 } }, { "match": { "languageId": 0 } } ], "must": [ { "multi_match": { "boost": 1.1, "query": ".....", "fuzzy_rewrite": "constant_score_boolean", "fuzziness": 1, "cutoff_frequency": 0.5, "prefix_length": 1, "max_expansions": 100, "slop": 2, "lenient": true, "tie_breaker": 1, "minimum_should_match": 2, "operator": "or", "fields": [ "content1^3", "content2^2", "content3^1", "category" ], "zero_terms_query": "all" } } ] } }, "aggs": { "categories": { "terms": { "field": "category", "size": 10 }, "aggs": { "produdtcs": { "top_hits": { "_source": [ "content1^3", "content2^2", "content3^1" ], "size": 3 } } } } }}