Nested sort by multiple values ​​on Elasticsearch

I would like to sort nested Elasticsearch using multiple values. Here is an example:

I have Events that have nested topics like this

  "_source": { "topics": [ { "type": "Tools", "name": "Data Science", "id": 19 }, { "type": "Challenges", "name": "Disaster Resilience", "id": 1 }, { "type": "Tools", "name": "Entrepreneurship", "id": 21 }, { "type": "Challenges", "name": "Prosperity", "id": 8 } ] ... } 

In addition, Members have the same nested topics using the same structure.

I would like to sort events according to members' topics. For example, if a member has three suitable topics with an event, and two with another, I would like to show the most appropriate event first.

I tried something like this:

 "sort":[ { "topics.id":{ "nested_path":"topics", "mode":"sum", "order":"asc", "nested_filter":{ "match": { "topics.id": 13 } } } } ] 

What works for a specific topic. But I would like to do something like this below, using several values ​​in sort , returning the most appropriate event first. In this case, the event with topics 13 and 14 will be returned first than the event with only topic 13, and all other non-matching events will be shown after.

 "sort":[ { "topics.id":{ "nested_path":"topics", "mode":"sum", "order":"asc", "nested_filter":[ { "match": { "topics.id": 13 } }, { "match": { "topics.id": 14 } } ] } } ] 

EDITED: Here is the error I received while using this last snippet:

 { "error": { "root_cause": [ { "type": "illegal_argument_exception", "reason": "[field_sort] nested_filter doesn't support values of type: START_ARRAY" } ], "type": "illegal_argument_exception", "reason": "[field_sort] nested_filter doesn't support values of type: START_ARRAY" }, "status": 400 } 

But this, unfortunately, does not work. Is there any way to do this? Am I missing some awesome feature here?

Thanks!

+5
source share
1 answer

I managed to solve this problem using Painless script. I do not do this, but it works. If I find a better solution, I will post it as well.

 { "query":{ "bool":{ "must":[ { "nested":{ "path":"topics", "query":{ "function_score":{ "script_score":{ "script":{ "lang":"painless", "params":{ "ids":[ 1, 14, 19, 13, 19, 21, 8 ] }, "inline":"int s = 0; for (int i = 0; i < params.ids.length; i++){ for (int j = 0; j < doc['topics.id'].value; j++) { if (params.param1[i] == doc['topics.id'][j]) { s =+ 1 }}} return s" } } } } } } ] } } } 
0
source

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


All Articles