Elasticsearch sort by one subkey of a document in an array

I have documents that look like this (here are two examples):

{ "id": 1234, "title": "the title", "body": "the body", "examples": [ { "evidence_source": "friend", "source_score": 15 }, { "evidence_source": "parent", "source_score": 12 } ] } 

and

 { "id": 6346, "title": "new title", "body": "lots of content", "examples": [ { "evidence_source": "friend", "source_score": 10 }, { "evidence_source": "parent", "source_score": 27 }, { "evidence_source": "child", "source_score": 4 } ] } 

The format of the subdocuments in the examples array will always have evidence_source and source_score , but there will be a variable sum of these subdocuments, each of which has different evidence_source values.

I am wondering if it is possible to sort documents in this format based on one of the source_score values โ€‹โ€‹corresponding to the concrete evidence_source value. I would really like to do this:

  • Sort documents by source_score in descending order, where the associated evidence_source is friend . The resulting order of the id document will be 1234.6346.
  • Sort documents by source_score in descending order, where the associated evidence_source is parent . The resulting order of the id document will be 6346.1234.

The immediate results that I came up with to create something like 1 and 2 , but I do not believe that they understand exactly what I want to do.

Any ideas on how I can do this? I reflected on some ideas based on indexing these examples subfiles separately, but I'm pretty new to elasticsearch and therefore looking for some tips on how to achieve my goal in the simplest way (which could be a dream pipe ...)

Update . A message on the elasticsearch mailing list seems to indicate that this is NOT possible, but I wonder if anyone has any other ideas?

+6
source share
1 answer

Support for sorting by fields inside nested documents was added to elasticsearch in 0.90:

https://github.com/elasticsearch/elasticsearch/issues/2662

Sorting by nested field support has the following parameters: the top of existing sorting options:

  • nested_path - Defines the sort type of a nested object. This sort field must be a direct field inside this nested object. default - use the most directly inherited nested object from the sort field.
  • nested_filter - Filter internal objects inside a nested path must match so that its field values โ€‹โ€‹are equal counted by sorting. The general case is to repeat the query / filter inside a nested filter or query. By default, no nested_filter active.

Given your example data, the following query should give you what you need:

 { "query": { "match_all": {} }, "sort": [ { "examples.source_score": { "order": "desc", "nested_path": "examples", "nested_filter": { "term": { "examples.evidence_source": "friend" } } } } ] } 
+18
source

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


All Articles