Sort nested fields (not parent doc) by nested field

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 talks about how to sort the parent docs by nested field. E.g., the following

{
    "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

    "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

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

Thank you.

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