Elasticsearch: how do you know in which field the results are sorted?

Is there a way in Elasticsearch to check in which field the results are sorted? I want something like inner-hits for a sort expression.

Imagine that your documents have the following form:

{"numerals" : [ // nested {"key": "point", "value": 30}, {"key": "points", "value": 200}, {"key": "score", "value": 20}, {"key": "scores", "value": 40} ] } 

and you sort the results by:

 {"numerals.value": { "nested_path": "numerals", "nested_filter": { "match": { "numerals.key": "score"}}}} 

Now I have no idea how to find out the field by which the results are actually sorted: maybe scores in this document, but maybe score for the rest? There are 2 problems - 1. You cannot use internal hits or highlight for nested fields. and - 2. Even if possible, this does not solve the problem if there are several suitable candidates.

+5
source share
1 answer

Question about sorting by fields inside nested objects.

So here is what the documentation is https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html and also https://www.elastic.co/guide/en/elasticsearch/reference /current/search-request-sort.html#_nested_sorting_example says:

Elasticsearch first restricts the attached documents using the "nested_filter" -query, and then sorted the same way as for multi-valued fields: In the same way as if there were only filtered attached documents as internal objects, as if there was only a root document with a multi-valued a field containing exactly the entire value belonging to the filtered nested objects
(in your example, only one value will remain: 20). If you want to be sure of the sort order, insert the "mode" parameter: "min", "max", "sum", "avg" or "median"

If you do not specify the "mode" parameter according to the corresponding problem , the minimum value will be selected for "asc" and the maximum value will be selected for "desc" -order:

By default, when sorting in a multi-valued field, the lowest or highest value will be selected from the field values โ€‹โ€‹depending on the type of order.

+2
source

Source: https://habr.com/ru/post/1268743/


All Articles