# Sorting on multi-level nested documents

**URL:** <https://discuss.elastic.co/t/sorting-on-multi-level-nested-documents/34706>\
**Category:** Elasticsearch\
**Created:** [November 16, 2015, 5:39pm UTC](https://discuss.elastic.co/t/sorting-on-multi-level-nested-documents/34706 "2015-11-16T17:39:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huwkthomas](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@huwkthomas](https://discuss.elastic.co/u/huwkthomas)\
**Post date:** [November 16, 2015, 5:39pm UTC](https://discuss.elastic.co/t/sorting-on-multi-level-nested-documents/34706/1 "2015-11-16T17:39:12Z")

</div>

I have the following mapping, which has two levels of nested documents. The first nested document is Parties, and within Parties are nested documents; Details.

I am trying to sort on Parties.Details.Value and that works perfectly.

I can filter the sort so that I only sort on Parties.Details.Value where Parties.Details.Key = "propertyNumber" and that works fine.

Where I am failing is where I want to sort on Parties.Details.Value where Parties.Key = "shipper" AND Parties.Details.Key = "propertyNumber".

```
{
  "mappings": {
    "transactionentity": {
      "properties": {
        "EditedBy": {
          "type": "string"
        },
        "EditedDate": {
          "type": "date"
        },
        "Id": {
          "type": "integer"
        },
        "Parties": {
          "properties": {
            "Key": {
              "type": "string"
            },
            "Name": {
              "type": "string"
            },
            "Details": {
              "properties": {
                "Unit": {
                  "type": "string"
                },
                "Key": {
                  "type": "string"
                },
                "Value": {
                  "type": "string"
                }
              },
              "type": "nested"
            }
          },
          "type": "nested"
        }
      }
    }
  }
}

```

This sort clause works:

```
  "sort": [
    {
      "Parties.Details.Value": {
        "order": "asc",
        "nested_filter": {
          "term": {
            "Parties.Details.Key": "propertyNumber"
          }
        }
      }
    }

```

I have tried many variations on nested filters for the "nested\_filter" but have had no luck. Variations like below result in "Infinity" as the search value.

```
"sort": [
    {
      "Parties.Details.Value": {
        "mode": "min",
        "order": "asc",
        "nested_filter": {
          "bool": {
            "must": [
              {
                "nested": {
                  "filter": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "Parties.Key": "shipper"
                          }
                        },
                        {
                          "nested": {
                            "filter": {
                              "term": {
                                "Parties.Details.Key": "propertyNumber"
                              }
                            },
                            "path": "Parties.Details"
                          }
                        }
                      ]
                    }
                  },
                  "path": "Parties"
                }
              }
            ]
          }
        }
      }
    }

```

This is the only thing stopping us from going into production and any help would be GREATLY appreciated.

---

<div class="post-metadata">

**Author:** ![jdiezf](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@jdiezf](https://discuss.elastic.co/u/jdiezf)\
**Post date:** [August 5, 2016, 3:26pm UTC](https://discuss.elastic.co/t/sorting-on-multi-level-nested-documents/34706/2 "2016-08-05T15:26:10Z")

</div>

Hi @huwkthomas,

We are having this same issue and we can't make it. Did you finally resolve it?

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:** [July 5, 2017, 10:29pm UTC](https://discuss.elastic.co/t/sorting-on-multi-level-nested-documents/34706/3 "2017-07-05T22:29:46Z")

</div>


