# Sort nested fields (not parent doc) by nested field

**URL:** <https://discuss.elastic.co/t/sort-nested-fields-not-parent-doc-by-nested-field/175635>\
**Category:** Elasticsearch\
**Created:** [April 5, 2019, 4:41pm UTC](https://discuss.elastic.co/t/sort-nested-fields-not-parent-doc-by-nested-field/175635 "2019-04-05T16:41:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdpark](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jdpark](https://discuss.elastic.co/u/jdpark)\
**Post date:** [April 5, 2019, 4:41pm UTC](https://discuss.elastic.co/t/sort-nested-fields-not-parent-doc-by-nested-field/175635/1 "2019-04-05T16:41:37Z")

</div>

Hello,

I'm trying to get a sorted list of nested fields. [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html) talks about how to sort the parent docs by nested field. E.g., the following

```auto
{
    "query": {
        "nested": {
            "path": "myNestedPath",
            "query": {
                "exists": {
                    "field": "myNestedPath.myNestedField"
                }
            }
        }
    },
    "_source": [
        "myNestedPath.myNestedField"
    ],
    "sort": {
        "myNestedPath.myNestedField": {
            "mode": "avg",
            "order": "asc",
            "nested": {
                "path": "myNestedPath",
                "filter": {
                    "exists": {
                        "field": "myNestedPath.myNestedField"
                    }
                }
            }
        }
    }
}

```

will

1. Filter by documents that have `myNestedField`
2. Sort documents by the average of `myNestedField`
3. Return a list of documents containing `myNestedField`

So something like

```auto
    "hits": {
        "hits": [
            {
                "_id": "containerId2",
                "_score": null,
                "_source": {
                    "myNestedPath": [
                        {
                            "myNestedField": 1
                        },
            },
            {
                "_id": "containerId1",
                "_score": null,
                "_source": {
                    "myNestedPath": [
                        {
                            "myNestedField": 0
                        },
                        {
                            "myNestedField": 10
                        },

```

But is there a way to flatten the list and sort by `myNestedField` (not `avg`)? Something like

```auto
    "hits": {
        "hits": [
                        {
                           "myNestedField": 0
                        },
                        {
                            "myNestedField": 1
                        },
                        {
                            "myNestedField": 10
                        },

```

Thank you.

---

<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:** [May 3, 2019, 4:42pm UTC](https://discuss.elastic.co/t/sort-nested-fields-not-parent-doc-by-nested-field/175635/2 "2019-05-03T16:42:05Z")

</div>

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