Sorting by Nested Field


(Atul Harsha) #1

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
}

(David Pilato) #2

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


(Atul Harsha) #3

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
}

(David Pilato) #4

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


(Atul Harsha) #5

Thankyou @dadoonet this worked :smile:


(system) #6

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