Failing to search polygons that intersect with other polygons with elasticsearch

216 Views Asked by At

In our app, ES holds objects with areas field, where areas field in a type of MultiPyligon. (basically, it's an array of polygons).

Now, we need to search for all the objects in which one of their polygons in at least partially falls within a given polygon (in our case it is the current viewport of the map).

The current query that we are experimenting with is the following:

$params = [
            'index' => self::CrimeIndex,
            'body' => [
                'size' => 10000,
                'query' => [
                    'bool' => [
                        'filter' => [
                            'geo_bounding_box' => [
                                'areas' => [
                                    "top_left" => [
                                        "lat" => $neLat,
                                        "lon" => $neLng
                                    ],
                                    "bottom_right" => [
                                        "lat" => $swLat,
                                        "lon" => $swLng
                                    ]
                                ],
                            ]
                        ]
                    ]
                ]
            ],
];

The problem is that this query gets all the polygons that touch the edges of the bounding box. (see picture). How can we get all the polygons that are at least partially within the bounding box?

enter image description here

Mappings are done as follows:

$params = [
    'index' => CrimeService::CrimeIndex,
    'body' => [
        "mappings" => [
            'properties' => [
                'areas' => [
                    'type' => 'geo_shape'
                ]
            ],
        ],
    ],
];
$client->indices()->create($params);

Based on the docs, geo_shape can be MultiPolygon. https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html

And here is the example of how it looks like populated:

enter image description here

GET crimes/_mapping/field/areas provides the following: enter image description here


UPDATE - More Detailed Steps to reproduce

The dump of the collection/index is attached: https://www.dropbox.com/s/8inavsvcrnuozw1/dump-2021-12-29t21_54_04.639z.json.zip?dl=0

The query that is executed with elasticsearch-php is:

$params = [
            'index' => 'crime',
            'body' => [
                'size' => 10000,
                'query' => [
                    'bool' => [
                        'filter' => [
                            'geo_bounding_box' => [
                                'areas' => [
                                    "top_left" => [
                                        "lat" => $neLat,
                                        "lon" => $neLng
                                    ],
                                    "bottom_right" => [
                                        "lat" => $swLat,
                                        "lon" => $swLng
                                    ]
                                ],
                            ]
                        ],
                    ]
                ]
            ],
        ];

If we execute it with the parameters: 49.29366604017385,-123.00491857934166,49.19709977562233,-123.26617317321401

We get the following: enter image description here

In case that the viewport is changed a bit, so the polygons touch the borders of viewport: 49.28031011582358,-122.92300503734472,49.18371770837152,-123.18425963121705, we get the rest of the polygons: enter image description here

1

There are 1 best solutions below

0
On

Your query coordinates are wrong, instead of top_left + bottom_right, you have bottom_left + top_right (see image below)

enter image description here

I think that pretty much explains why you're seeing what you're seeing.