7

I'd like to sort nested Elasticsearch using multiple values. Here's an example:

I have some Events, which have a 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
        }
      ]
      ...
    }

Also, Members have the same nested topics, using the same structure.

What I'd like to do is sort the Events according to the Members topics. For instance, if a Member has three matching topics with an Event, and two with another, I'd like to show the most matching Event first.

I tried something like this:

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

Which works for a specific Topic. But I'd like to do something like this below, using multiple values in the sort, returning the most matched Event first. In this case, an Event with Topics 13 and 14 will be returned first than an Event with just Topic 13 and all the 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's the error I got when 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 a way to do this? Am I missing some awesome feature here?

Thanks!

2
  • what are you getting with this? can you post the response and also the expected response? Apr 18, 2017 at 17:13
  • Just edited with the response. The expected response is describe on the paragraph before, about the Events ordering. Apr 18, 2017 at 17:25

1 Answer 1

0

I was able to solve this problem using a Painless script. I don't if this is performance-wise, but it's working. If I find a better solution, will post this 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"
                           }
                        }
                     }
                  }
               }
            }
         ]
      }
   }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.