Sorting by Nested Field

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
}
1 Like

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

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
}

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

1 Like

Thankyou @dadoonet this worked :smile:

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