Skip to content

Conversation

lerouxb
Copy link
Contributor

@lerouxb lerouxb commented Aug 19, 2021

Description

mergeGeoFilter incorrectly detected geo filter queries by looking at the field name to see if it is "coordinates", but coordinate fields can be called anything.

The geo queries coming from the schema are actually using Geospatial Query Operators, so newGeoQueryFilter looks like this for the first query:

    {
        "coordinates": {
            "$geoWithin": {
                "$centerSphere": [
                    [
                        -139.23222157287597,
                        37.19803100919542
                    ],
                    0.3673330493291723
                ]
            }
        }
    }

Where "coordinates" is just the field name. It could also be something like "start station location" or "end station location".

Then for the second one onwards, newGeoQueryFilter looks something like like this:

    {
        "$or": [
            {
                "coordinates": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [
                                -139.23222157287597,
                                37.19803100919542
                            ],
                            0.3673330493291723
                        ]
                    }
                }
            },
            {
                "coordinates": {
                    "$geoWithin": {
                        "$centerSphere": [
                            [
                                -87.06862449645996,
                                23.72228020780053
                            ],
                            0.44983066175313
                        ]
                    }
                }
            }
        ]
    }

If you draw a polygon rather than a circle, then it still uses $geoWithin, but $centerSphere is just replaced with $geometry. Here's a nice complicated query with multiple circles and geometry across two different coordinate fields:

{"$or":[{"end station location":{"$geoWithin":{"$centerSphere":[[-73.9923432050273,40.72411731487377],0.0001854464478985859]}}},{"end station location":{"$geoWithin":{"$centerSphere":[[-73.98943598382175,40.75213296564792],0.0001678904245349514]}}},{"start station location":{"$geoWithin":{"$centerSphere":[[-73.99662979412825,40.73280968156368],0.00012901824776155355]}}},{"start station location":{"$geoWithin":{"$centerSphere":[[-73.98485598620029,40.75153049645215],0.00019046097385540318]}}},{"end station location":{"$geoWithin":{"$geometry":{"type":"Polygon","coordinates":[[[-74.0376926958561,40.87608664321146],[-74.13294095546009,40.681051874565135],[-73.8812904059887,40.49634035708515],[-73.70418012142183,40.603675733344964],[-73.74768052250147,40.77739378279334],[-74.0376926958561,40.87608664321146]]]}}}}]}

Changes

  • This change now detects geo conditions by looking for one of ['$geoIntersects', '$geoWithin', '$near', '$nearSphere']. This means it works with any coordinate field names and the exact coordinate format used ( type: "point" vs just two-tuples for longitude and latitude) is irrelevant.
  • I also had to change the tests because it all used { lon: X, lat: Y} to specify exact coordinates. Technically that's a correct way to specify coordinates because it is the legacy coordinate pair via an embedded document (where the first one is the field to use for longitude and the second one the field for latitide) but it was incorrect in that the schema analyzer doesn't build queries with exact coordinates - it builds $geoWithin style queries.
  • I moved newGeoQueryFilter out of the store as i figured it is its own unit
  • I moved the tests for newGeoQueryFilter out of the renderer tests since it had nothing to do with it
  • sorted some imports

Copy link
Contributor

@rose-m rose-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Pretty surprising this has been lingering there for quite a while probably!

Copy link
Collaborator

@mcasimir mcasimir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Great idea to put that on separate files too.

@lerouxb lerouxb force-pushed the COMPASS-4968-geo-query branch from d63f608 to a29869a Compare August 19, 2021 12:37
@lerouxb
Copy link
Contributor Author

lerouxb commented Aug 19, 2021

I just changed the utils dir to modules which should be more in line with what happens elsewhere.

@lerouxb lerouxb merged commit 83146b5 into main Aug 19, 2021
@lerouxb lerouxb deleted the COMPASS-4968-geo-query branch August 19, 2021 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants