Search code examples
python-3.xelasticsearchelasticsearch-dsl

Elasticsearch Geo Polygon query with range filter


I am trying to retrieve some image fields from Elasticsearch using Geo polygon points. However I also want to filter out the images using a date field. The below two query works. Is it possible to combine them into one? I greatly appreciate any help.

**Query 1**
GET /manyimages_*/_search/
{
  "query": {
    "bool" : {
      "filter" : {
        "range": {"c_datetime": {"gte" : "2020-07-30T09:03:17.000"}}
      }
    }
  }
}

**Query 2**
GET /manyimages_*/_search/
{
  "size":10000,
  "query": 
  {    
    "geo_polygon":
        {"location":
            {"points": [[-98.59526078405563, 29.48456853315911],
[-98.59393149263758, 29.485036283823487],
[-98.58712997745901, 29.48730046713193]]}
            ,"validation_method":"STRICT","ignore_unmapped":false,"boost":1.0
          
        }
        }
    ,"_source":{"includes":["latitude","longitude","bearing_deg","c_datetime"],"excludes":["location"]
      
  }
}

Solution

  • Yes, you can simply have both constraints in the bool/filter clause:

    GET /manyimages_*/_search/
    {
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "c_datetime": {
                  "gte": "2020-07-30T09:03:17.000"
                }
              }
            },
            {
              "geo_polygon": {
                "location": {
                  "points": [
                    [
                      -98.59526078405563,
                      29.48456853315911
                    ],
                    [
                      -98.59393149263758,
                      29.485036283823487
                    ],
                    [
                      -98.58712997745901,
                      29.48730046713193
                    ]
                  ]
                },
                "validation_method": "STRICT",
                "ignore_unmapped": false,
                "boost": 1
              }
            }
          ]
        }
      }
    }