Sorting by max value of property of nested object array

I have the following object model:

    "classification": [
        { "label": "aaa", "probability": 0.9923 },
        { "label": "bbb", "probability": 0.3452 },
        { "label": "ccc", "probability": 0.0012 },

I want to sort documents based on the label that has the highest probability value.

ie: Identify the label value of the nested object with the highest probability and sort on that.

I've tried this:

  "sort": {
    "classification.probability": {
      "mode": "max",
      "order": "desc",
      "missing": "_last",
      "nested": {
        "path": "classification"

But it doesn't seem to be working - it seems to be sorting on the max probability value instead of the label of the object with the max probability value.

Is what I want even possible?

This is the solution:

  "sort": {
    "_script": {
      "type": "string",
      "script": {
        "lang": "painless",
        "source": "params._source['classification'] == null ? '' : params._source['classification'].stream().max((a, b) ->, b.probability)).get().label;"
      "order": "asc"

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.