Search code examples
elasticsearchelastic-stack

"[geo_distance] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"


I am tried to create search query with a values in 15m radios and between 3 weeks. I tried to execute this query:

"query": {
"bool": {
  "must": {
    "match_all": {}
  }
, "filter": [
  {
    "geo_distance": {
     
      "distance": "1000km",
      "geoLocation": {
        "lat": 31.966467334184614,
        "lon": 35.83242623178664
      }
    }
    ,
   "range": {
      "map_date": {
        "gte": "now-3w/w",
        "lte": "now/w"
      }
    }
    
  }
]
}}

My date filed is: map_date and my geo point filed is geoLocation

I get this response :

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "[geo_distance] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line" : 18,
        "col" : 8
      }
    ],
    "type" : "x_content_parse_exception",
    "reason" : "[18:8] [bool] failed to parse field [filter]",
    "caused_by" : {
      "type" : "parsing_exception",
      "reason" : "[geo_distance] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
      "line" : 18,
      "col" : 8
    }
  },
  "status" : 400
}

Help me please to figure out what I am doing wrong


Solution

  • Your filter part was mal formated, try :

    {
      "query": {
        "bool": {
          "must": {
            "match_all": {}
          },
          "filter": [
            {
              "geo_distance": {
                "distance": "1000km",
                "geoLocation": {
                  "lat": 31.966467334184614,
                  "lon": 35.83242623178664
                }
              }
            },
            {
              "range": {
                "map_date": {
                  "gte": "now-3w/w",
                  "lte": "now/w"
                }
              }
            }
          ]
        }
      }
    }
    

    Il your filter array you list a list of {}, take a look at: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html