# How to enforce ordering within nested objects?

**URL:** <https://discuss.elastic.co/t/how-to-enforce-ordering-within-nested-objects/326889>\
**Category:** Elasticsearch\
**Created:** [March 2, 2023, 9:19pm UTC](https://discuss.elastic.co/t/how-to-enforce-ordering-within-nested-objects/326889 "2023-03-02T21:19:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pure](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pure/32/117992_2.png) [@pure](https://discuss.elastic.co/u/pure)\
**Post date:** [March 2, 2023, 9:19pm UTC](https://discuss.elastic.co/t/how-to-enforce-ordering-within-nested-objects/326889/1 "2023-03-02T21:19:27Z")

</div>

I'm using elasticsearch as a Key-Value store, so that I can guarantee the query always return just 1 document. There is a nested field in the document schema, and I want to make sure multiple values within the nested object are returned in order.

More concretely, I have

```auto
{
    "mappings": {
        "properties": {
            "neighborhood": {
                "type": "keyword"
            },
            "forecast": {
                "type": "nested",
                "properties": {
                    "date": {
                        "type": "date"
                    },
                    "demand": {
                        "type": "double"
                    }
                }
            }
        }
    }
}

```

my base query looks like this:

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "neighborhood": "Hogwarts"
                    }
                }
            ]
        }
    }
}

```

and I got back result with randomly ordered forecast objects not sorted by date

```auto
"_source": {
                    "neighborhood": "Hogwarts",
                    "forecast": [
                        {
                            "date": 3,
                            "demand": 4.895964622497559
                        },
                        {
                            "date": 1,
                            "demand": 5.326427459716797
                        },
                        {
                            "date": 2,
                            "demand": 5.326427459716797
                        }
}

```

How do i modify the query so that it can return document where within the forecast object it is sorted by `date`.

I found many examples which explained how to sort the multiple parent documents based on nested objects fields, but I haven't seen an example explaining how to enforce ordering of values within nested objects itself.

Thanks!

---

<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:** [March 30, 2023, 9:20pm UTC](https://discuss.elastic.co/t/how-to-enforce-ordering-within-nested-objects/326889/2 "2023-03-30T21:20:19Z")

</div>

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