Sort on collapsed best inner_hit document field value

Hi all,

I have documents which must be collapsed.
I would like to sort the global result on a field of the best first document of each group.

For instance :

{
  "took" : 49,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "dummy",
        "_type" : "_doc",
        "_id" : "f95RJW0Bqo1W9ROBRYs8",
        "_score" : 1.0,
        "_source" : {
          "group-id" : 21,
          "group-sort-value" : 5883,
          "id" : 214117,
          "sort-value" : 4117
        },
        "fields" : {
          "group-id" : [
            21
          ]
        },
        "inner_hits" : {
          "best-group" : {
            "hits" : {
              "total" : {
                "value" : 10000,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "dummy",
                  "_type" : "_doc",
                  "_id" : "at5RJW0Bqo1W9ROBRHt6",
                  "_score" : null,
                  "_source" : {
                    "group-id" : 21,
                    "group-sort-value" : 10000,
                    "id" : 210000,
                    "sort-value" : 0
                  },
                  "sort" : [
                    10000
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

In this example I would like to sort the global result on the sort-value field of the best group:
0 and not 4117.

It does not seem to be possible but well, perhaps is it possible with some scripting. :slight_smile:

Any idea please ?

Thank you,

Ludovic.

I think this is just what I need:
collapse_sort to choose the top field-collapsing document independently from the main query score without inner_hits

Ludovic