# How to sort nested documents regardless of different parents

**URL:** <https://discuss.elastic.co/t/how-to-sort-nested-documents-regardless-of-different-parents/198556>\
**Category:** Elasticsearch\
**Created:** [September 8, 2019, 11:32am UTC](https://discuss.elastic.co/t/how-to-sort-nested-documents-regardless-of-different-parents/198556 "2019-09-08T11:32:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmadam/32/53779_2.png) [@ahmadam](https://discuss.elastic.co/u/ahmadam)\
**Post date:** [September 8, 2019, 11:32am UTC](https://discuss.elastic.co/t/how-to-sort-nested-documents-regardless-of-different-parents/198556/1 "2019-09-08T11:32:21Z")

</div>

I'm new to elasticsearch and struggling this situation (for example):  
I want to index offices and clerks of each office. So, I've created this mapping:

```
PUT office
{
  "mappings": {
    "properties": {
      "office_name": {
        "type": "text" 
      },
      "clerks": {
        "type": "nested"
      }
    }
  }
}

```

and indexing some data:

```
PUT /office/_doc/1
{
  "name": "office a",
  "clerks": [
    {
      "name": "a a a a"
    },
    {
      "name": "a a"
    },
    {
      "name": "b"
    }
  ]
}

PUT /office/_doc/2
{
  "name": "office b",
  "clerks": [
    {
      "name": "a a a"
    }
  ]
}

```

Now, I want to search for 'a':

```
GET /office/_search
{
  "query": {
    "nested": {
      "path": "clerks",
      "query": {
        "match": {
          "clerks.name": "a"
        }
      },
      "inner_hits": {}
    }
  }
}

```

the result is:

```
{
  "took" : 624,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.1272885,
    "hits" : [
      {
        "_index" : "office",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.1272885,
        "_source" : {
          "name" : "office b",
          "clerks" : [
            {
              "name" : "a a a"
            }
          ]
        },
        "inner_hits" : {
          "clerks" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.1272885,
              "hits" : [
                {
                  "_index" : "office",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_nested" : {
                    "field" : "clerks",
                    "offset" : 0
                  },
                  "_score" : 1.1272885,
                  "_source" : {
                    "name" : "a a a"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "office",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.1184678,
        "_source" : {
          "name" : "office a",
          "clerks" : [
            {
              "name" : "a a a a"
            },
            {
              "name" : "a a"
            },
            {
              "name" : "b"
            }
          ]
        },
        "inner_hits" : {
          "clerks" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : 1.1468691,
              "hits" : [
                {
                  "_index" : "office",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "clerks",
                    "offset" : 0
                  },
                  "_score" : 1.1468691,
                  "_source" : {
                    "name" : "a a a a"
                  }
                },
                {
                  "_index" : "office",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "clerks",
                    "offset" : 1
                  },
                  "_score" : 1.0900666,
                  "_source" : {
                    "name" : "a a"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

I expect 'a a a' appears before 'a a' and the result to be like this:

```
[
{
  "name": "a a a a"
},
{
  "name": "a a a"
},
{
  "name": "a a"
}
]

```

How can I achieve this result?

---

<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:** [October 6, 2019, 11:32am UTC](https://discuss.elastic.co/t/how-to-sort-nested-documents-regardless-of-different-parents/198556/2 "2019-10-06T11:32:21Z")

</div>

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