0

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

Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76
  • Very strange, as the `geo_bounding_box` query should handle intersection out of the box. Have you tried a `geo_shape` query with `relation: intersects` instead? – Val Dec 29 '21 at 07:01
  • @Val We did, but that didn't work at all. Maybe because that PHP library does not support it? I am not sure how to convert this query object to use relation... – Shurik Agulyansky Dec 29 '21 at 07:28
  • Which version of ES are you using? And are you sure that the PHP client is compatible with that version? – Val Dec 29 '21 at 07:35
  • It's 7.15.2 . Seem to be working for everything else. – Shurik Agulyansky Dec 29 '21 at 07:42
  • Ok, I'm not sure I understand what you mean by "touch the edges", though – Val Dec 29 '21 at 07:50
  • In other words, if the searchable polygons are fully contained within the viewport, they are not part of the result set. If at least one point is touching the borders of the bounding box, then they are. – Shurik Agulyansky Dec 29 '21 at 07:53
  • Wow, that's very unusual... Anything worth mentioning about your mapping? Was the index created with an older version and then upgraded to 7.15.2? – Val Dec 29 '21 at 07:55
  • Nope, everything is under the same version. We even deleted everything and reindexed all the data from scratch. – Shurik Agulyansky Dec 29 '21 at 07:56
  • So, if you move the viewport slightly to the bottom, the missing polygon will appear? – Val Dec 29 '21 at 07:59
  • Yep :) Exactly! – Shurik Agulyansky Dec 29 '21 at 08:00
  • Do you mind sharing the mapping definition of your `areas` `geo_shape` field? – Val Dec 29 '21 at 08:01
  • Absolutely, added into the post. – Shurik Agulyansky Dec 29 '21 at 08:22
  • In addition can you run this `GET crimes/_mapping/field/areas` and update your question with the output please? – Val Dec 29 '21 at 08:25
  • Done, updated. see above. – Shurik Agulyansky Dec 29 '21 at 08:31
  • Really weird, I can't reproduce it. I have a geo_shape and I can retrieve it with a bounding box that either fully contains it or not – Val Dec 29 '21 at 09:06
  • hmmm... And how does your request look like? Same as ours? – Shurik Agulyansky Dec 29 '21 at 09:08
  • Exact same query, nothing fancy, just a `geo_bounding_box` constraint. Anyway I could reproduce with your specific polygon? Can you gist a reproducible case for me to try? – Val Dec 29 '21 at 09:35
  • Do you mean the code itself? Or the data too? I definitely can, but tomorrow (it's almost 2am :) ) – Shurik Agulyansky Dec 29 '21 at 09:40
  • Just an export of the problematic shape and the exact query you're running. No worries, tomorrow is fine too – Val Dec 29 '21 at 10:02
  • @Val Instead of gist, I added more details about the issue. There isn't much to create gist out of. – Shurik Agulyansky Dec 29 '21 at 23:06
  • Thank you. The polygons in the export are not in the Vancouver area, though, right? https://i.stack.imgur.com/B8Puj.jpg – Val Dec 30 '21 at 05:29
  • Moreover, I think you have your query coordinates wrong, the points you gave are bottom_left/top_right, instead of top_left/bottom_right: https://i.stack.imgur.com/W0ka9.jpg I think that explains the problem – Val Dec 30 '21 at 05:38

1 Answers1

1

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.

Val
  • 207,596
  • 13
  • 358
  • 360