# Order inner\_hits by offset

**URL:** <https://discuss.elastic.co/t/order-inner-hits-by-offset/222770>\
**Category:** Elasticsearch\
**Created:** [March 9, 2020, 5:04pm UTC](https://discuss.elastic.co/t/order-inner-hits-by-offset/222770 "2020-03-09T17:04:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daven](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@daven](https://discuss.elastic.co/u/daven)\
**Post date:** [March 9, 2020, 5:04pm UTC](https://discuss.elastic.co/t/order-inner-hits-by-offset/222770/1 "2020-03-09T17:04:36Z")

</div>

I have a set of documents that contain nested paragraphs and sentences, I need the order of the inner hits to be in the order they occur in the document, eg. paragraph 0 sentence 0, paragraph 0 sentence 5, paragraph 2 sentence 0. Currently they return in the reverse order, but I have no sort order applied.

I understand the `offset` property in the response is not part of my data, but is it possible to sort the inner\_hits results in the manner I would like using the `offset` property? Or would it require adding numeric fields for the paragraph and sentence position values and sorting off of those new fields?

Still using ES 5.3, so sorry about older syntax.

Mapping

```auto
PUT /myindex
{
    "mappings": {
        "doc": {
            "paragraphs": {
                "type": "nested",
                "properties": {
                    "prop1": {
                        "type": "float"
                    },
                    "sentences": {
                        "type": "nested",
                        "properties": {
                            "value": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

```

Document

```auto
PUT /myindex/doc/1
{
   "title": {
        "value": "I am a test document"
    },
    "paragraphs": [
        {
            "sentences": [
                {
                    "value": "I am the first sentence."
                },
                {
                    "value": "I am the second sentence."
                },
                {
                    "value": "This is some more test data"
                }
            ]
        }
    ]
}

```

Query

```auto
POST /myindex/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "bool": {
                                "must": {
                                    "prefix": {
                                        "paragraphs.sentences.value": "I am"
                                    }
                                }
                            }
                        },
                        "path": "paragraphs.sentences",
                        "inner_hits": {
                            "from": 0,
                            "size": 10,
                            "sort": {
                            }
                        }
                    }
                }
            ]
        }
    }
}

```

---

<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:** [April 6, 2020, 5:04pm UTC](https://discuss.elastic.co/t/order-inner-hits-by-offset/222770/2 "2020-04-06T17:04:38Z")

</div>

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