# Sorting by Nested Field

**URL:** <https://discuss.elastic.co/t/sorting-by-nested-field/103141>\
**Category:** Elasticsearch\
**Created:** [October 8, 2017, 7:23am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141 "2017-10-08T07:23:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atul\_Harsha](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@Atul\_Harsha](https://discuss.elastic.co/u/Atul_Harsha)\
**Post date:** [October 8, 2017, 7:23am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/1 "2017-10-08T07:23:27Z")

</div>

Error : `[nested] query does not support [filter]`  
Code :

```
GET /_search
    {
      "query": {
        "nested": { 
          "path": "comments",
          "filter": {
            "range": {
              "comments.date": {
                "gte": "2014-10-01",
                "lt": "2014-11-01"
              }
            }
          }
        }
      },
      "sort": {
        "comments.stars": { 
          "order": "asc",   
          "mode": "min",   
          "nested_filter": { 
            "range": {
              "comments.date": {
                "gte": "2014-10-01",
                "lt": "2014-11-01"
              }
            }
          }
        }
      }
    }

```

Detailed Error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[nested] query does not support [filter]",
        "line": 5,
        "col": 17
      }
    ],
    "type": "parsing_exception",
    "reason": "[nested] query does not support [filter]",
    "line": 5,
    "col": 17
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 8, 2017, 8:07am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/2 "2017-10-08T08:07:17Z")

</div>

Probably use a bool query with a filter clause instead of filter?

---

<div class="post-metadata">

**Author:** ![Atul\_Harsha](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@Atul\_Harsha](https://discuss.elastic.co/u/Atul_Harsha)\
**Post date:** [October 8, 2017, 8:41am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/3 "2017-10-08T08:41:21Z")

</div>

Mapping :

```
{
  "my_index": {
    "mappings": {
      "blogpost": {
        "properties": {
          "body": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "comments": {
            "type": "nested",
            "properties": {
              "age": {
                "type": "short"
              },
              "comment": {
                "type": "text"
              },
              "date": {
                "type": "date"
              },
              "name": {
                "type": "text"
              },
              "stars": {
                "type": "short"
              }
            }
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

```

Updated Code :

```
 GET /_search
{
"query": {
    "nested": { 
      "path": "comments",
      "bool" : {
              "must" : [
                    { 
                      "match" : {  
                       "range" : {
                        "comments.date": {
                      "gte": "2014-10-01",
                      "lt": "2014-11-01" }
                       }
                      }
                    }
                    ]
               }
       }
  },
  "sort": {
    "comments.stars": { 
      "order": "asc",   
      "mode": "min",   
      "nested_filter": { 
        "range": {
          "comments.date": {
            "gte": "2014-10-01",
            "lt": "2014-11-01"
          }
        }
      }
    }
  }
}
}

```

Updated Error :

```
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[nested] query does not support [bool]",
        "line": 5,
        "col": 16
      }
    ],
    "type": "parsing_exception",
    "reason": "[nested] query does not support [bool]",
    "line": 5,
    "col": 16
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 8, 2017, 9:01am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/4 "2017-10-08T09:01:47Z")

</div>

You are missing the « query » level. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)

---

<div class="post-metadata">

**Author:** ![Atul\_Harsha](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@Atul\_Harsha](https://discuss.elastic.co/u/Atul_Harsha)\
**Post date:** [October 8, 2017, 9:17am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/5 "2017-10-08T09:17:50Z")

</div>

Thankyou @dadoonet this worked 😄

---

<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:** [November 5, 2017, 9:18am UTC](https://discuss.elastic.co/t/sorting-by-nested-field/103141/6 "2017-11-05T09:18:19Z")

</div>

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