# Nested Inner Hits sorting

**URL:** <https://discuss.elastic.co/t/nested-inner-hits-sorting/60365>\
**Category:** Elasticsearch\
**Created:** [September 13, 2016, 10:09am UTC](https://discuss.elastic.co/t/nested-inner-hits-sorting/60365 "2016-09-13T10:09:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![souad](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@souad](https://discuss.elastic.co/u/souad)\
**Post date:** [September 13, 2016, 10:09am UTC](https://discuss.elastic.co/t/nested-inner-hits-sorting/60365/1 "2016-09-13T10:09:42Z")

</div>

According to documentation ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits)), inner hits returned can be sorted, and default sorting is score. I have a document with nested documents indexed in a defined order, and I would like to query the documents with inner hits returned in the indexed order. I realized however, that it is returned in revered order.

Here is one returned hit:  
"hits": [

```
{
    "_index": "myIndex",
    "_type": "myType",
    "_id": "AVcPID001OHiI-svKuW9",
    "_score": 37.105354,
    "_source": {},
"inner_hits": {
    "innerObject": {
        "hits": {
            "total": 4,
            "max_score": 1,
            "hits": [
                {
                    "_index": "myIndex",
                    "_type": "myType",
                    "_id": "AVcPID001OHiI-svKuW9",
                    "_nested": {
                        "field": "innerObject",
                        "offset": 3
                    },
                    "_score": 1,
                    "_source": {
                        "id": 3123636
                }
                ,
                {
                    "_index": "myIndex",
                    "_type": "myType",
                    "_id": "AVcPID001OHiI-svKuW9",
                    "_nested": {
                        "field": "innerObject",
                        "offset": 2
                    },
                    "_score": 1,
                    "_source": {
                        "id": 4797314
                    }
                }
                ,
                {
                    "_index": "myIndex",
                    "_type": "myType",
                    "_id": "AVcPID001OHiI-svKuW9",
                    "_nested": {
                        "field": "innerObject",
                        "offset": 1
                    },
                    "_score": 1,
                    "_source": {
                        "id": 4538830
                    }
                }
                ,
                {
                    "_index": "myIndex",
                    "_type": "myType",
                    "_id": "AVcPID001OHiI-svKuW9",
                    "_nested": {
                        "field": "innerObject",
                        "offset": 0
                    },
                    "_score": 1,
                    "_source": {
                        "id": 5653177
                    }
                }
            ]
        }
    }
}

```

}  
]

So you can see the inner hits have the same score 1 (since my request was "match\_all"), and i didn't define any sorting, just set the size 4. But the "offset" indicates 3 than 2 than 1 than 0, which is the reverse order of the nested documents.  
How can I get my inner hits in the right order? I tried by the way to sort them by different fields (String, integer) and I always get them in the same order.

My elasticsearch version is 2.0.1

---

<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:** [July 5, 2017, 10:20pm UTC](https://discuss.elastic.co/t/nested-inner-hits-sorting/60365/2 "2017-07-05T22:20:45Z")

</div>


