1

I have a document with areas property. This property is an array of polygons. Let's say polygons represent the boundaries of some cities.

Given that I have a bounding box I want to query for those documents that have at least one of the polygons WITHIN the bounding box.

My query works if there is a single area matching the filter or the bounding box is so large all of the polygons are within it.

{
   "query":{
      "bool":{
         "filter":[
            {
               "geo_shape":{
                  "areas":{
                     "shape":{
                        "type":"envelope",
                        "coordinates":[
                           [
                              20.9325116,
                              52.2280665
                           ],
                           [
                              21.0069884,
                              52.1928718
                           ]
                        ]
                     },
                     "relation":"within"
                  }
               }
            }
         ]
      }
   }
}

A sample areas property looks like this:

{
   "areas":[
      {
         "type":"polygon",
         "coordinates":[

         ]
      },
      {
         "type":"polygon",
         "coordinates":[

         ]
      }
   ]
}
Kamil Latosinski
  • 756
  • 5
  • 28

1 Answers1

0

I didn't manage to solve this using a single query but I've found a work-around.

I have created an additional index holding only areas and their boundaries and second index is holding only the identifiers. So I have to do two queries, one to fetch ares matching my criteria and another one to fetch main documents.

This gave me the flexibility of writing more complex queries and as a bonus I'm not duplicating polygons that can be really large in some cases causing my index to grow over few GB in the blink of an eye.

First query fetching areas (this would match either districts or whole cities, this is why i'm using contains or within):

{
   "query":{
      "bool":{
         "should":[
            {
               "geo_shape":{
                  "boundary":{
                     "shape":{
                        "type":"envelope",
                        "coordinates":[
                           [
                              20.5992053,
                              52.1227965
                           ],
                           [
                              20.6671965,
                              52.0876378
                           ]
                        ]
                     },
                     "relation":"contains"
                  }
               }
            },
            {
               "geo_shape":{
                  "boundary":{
                     "shape":{
                        "type":"envelope",
                        "coordinates":[
                           [
                              20.5992053,
                              52.1227965
                           ],
                           [
                              20.6671965,
                              52.0876378
                           ]
                        ]
                     },
                     "relation":"within"
                  }
               }
            }
         ],
         "minimum_should_match":1
      }
   }
}

Second query fetching main documents:

{
   "query":{
      "bool":{
         "filter":[
            {
               "terms":{
                  "areas":[9, 6]
               }
            }
         ]
      }
   }
}
Kamil Latosinski
  • 756
  • 5
  • 28