# Sort on collapsed best inner\_hit document field value

**URL:** https://discuss.elastic.co/t/sort-on-collapsed-best-inner-hit-document-field-value/199284
**Category:** Elasticsearch
**Created:** [September 12, 2019, 3:49pm UTC](https://discuss.elastic.co/t/sort-on-collapsed-best-inner-hit-document-field-value/199284 "2019-09-12T15:49:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ludovic\_boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ludovic_boutros/32/54088_2.png) [@ludovic\_boutros](https://discuss.elastic.co/u/ludovic_boutros)
#### Post date: [September 12, 2019, 3:49pm UTC](https://discuss.elastic.co/t/sort-on-collapsed-best-inner-hit-document-field-value/199284/1 "2019-09-12T15:49:52Z")

</div>

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 :

```json
{
  "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. 🙂

Any idea please ?

Thank you,

Ludovic.

---

<div class="post-metadata">

### Author: ![ludovic\_boutros](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ludovic_boutros/32/54088_2.png) [@ludovic\_boutros](https://discuss.elastic.co/u/ludovic_boutros)
#### Post date: [September 13, 2019, 7:38am UTC](https://discuss.elastic.co/t/sort-on-collapsed-best-inner-hit-document-field-value/199284/2 "2019-09-13T07:38:19Z")

</div>

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](https://github.com/elastic/elasticsearch/issues/45646)

Ludovic

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 11, 2019, 7:52am UTC](https://discuss.elastic.co/t/sort-on-collapsed-best-inner-hit-document-field-value/199284/3 "2019-10-11T07:52:40Z")

</div>

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