# Help with sorting by nested field

**URL:** <https://discuss.elastic.co/t/help-with-sorting-by-nested-field/171942>\
**Category:** Elasticsearch\
**Created:** [March 12, 2019, 12:06pm UTC](https://discuss.elastic.co/t/help-with-sorting-by-nested-field/171942 "2019-03-12T12:06:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)\
**Post date:** [March 12, 2019, 12:06pm UTC](https://discuss.elastic.co/t/help-with-sorting-by-nested-field/171942/1 "2019-03-12T12:06:59Z")

</div>

I'm trying to sort the resulting documents of a nested query, by another nested field. I've based this on the original documentation, but can't seem to make it work. Does anyone know what the problem might be? Here's the query that I use:

```auto
{
    "query": {
      "bool": {
        "filter": [
          {
            "nested": {
              "path": "properties",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "match": {
                        "properties.key": "PROPERTY_TO_FILTER_DOCUMENTS_ON"
                      }
                    },
                    {
                      "match": {
                        "properties.value": "yes"
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    },
    "sort": [
      {
        "properties.value.keyword": {
          "order": "asc",
          "nested": {
            "path": "properties",
            "filter": {
              "term": {
                "properties.key": "PROPERTY_TO_SORT_ON"
              }
            }
          }
        }
      }
    ],
    "size": 1000,
    "_source": [
      "field1",
      "field2",
      "field3",
      "field4",
      "field5"
    ]
  }

```

---

<div class="post-metadata">

**Author:** ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)\
**Post date:** [March 13, 2019, 4:11pm UTC](https://discuss.elastic.co/t/help-with-sorting-by-nested-field/171942/2 "2019-03-13T16:11:20Z")

</div>

For anyone having the same problem, I was missing a ".keyword"

```auto
{
    "query": {
      "bool": {
        "filter": [
          {
            "nested": {
              "path": "properties",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "match": {
                        "properties.key": "PROPERTY_TO_FILTER_DOCUMENTS_ON"
                      }
                    },
                    {
                      "match": {
                        "properties.value": "yes"
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    },
    "sort": [
      {
        "properties.value.keyword": {
          "order": "asc",
          "nested": {
            "path": "properties",
            "filter": {
              "term": {
                "properties.key **.keyword**": "PROPERTY_TO_SORT_ON"
              }
            }
          }
        }
      }
    ],
    "size": 1000,
    "_source": [
      "field1",
      "field2",
      "field3",
      "field4",
      "field5"
    ]
  }

```

---

<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:** [April 10, 2019, 4:11pm UTC](https://discuss.elastic.co/t/help-with-sorting-by-nested-field/171942/3 "2019-04-10T16:11:28Z")

</div>

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