CodexBloom - Programming Q&A Platform

Elasticsearch 8.5 Geo-Polygon Query Not Filtering Results Correctly with Nested Objects

👀 Views: 393 💬 Answers: 1 📅 Created: 2025-06-25
elasticsearch geo-query nested-objects json

After trying multiple solutions online, I still can't figure this out. I'm writing unit tests and I'm relatively new to this, so bear with me... I'm relatively new to this, so bear with me... I'm having trouble with a geo-polygon query in Elasticsearch 8.5 that is not filtering results as expected when dealing with nested objects. I have documents that contain nested geo-points, and I'm trying to filter out all documents that lie outside a specified polygon. Here's a simplified version of my document structure: ```json { "location": { "type": "Object", "properties": { "coordinates": { "type": "geo_point" } } }, "nested_locations": [ { "coordinates": { "type": "geo_point" } } ] } ``` My query looks like this: ```json { "query": { "bool": { "must": [ { "geo_polygon": { "nested_locations.coordinates": { "points": [ {"lat": 40.73, "lon": -74.1}, {"lat": 40.74, "lon": -74.2}, {"lat": 40.73, "lon": -74.3}, {"lat": 40.72, "lon": -74.2} ] } } } ] } } } ``` However, I'm getting unexpected results, where documents that should not be included are appearing in the results. I’ve also verified individual coordinates in the polygon, and they seem correct. To troubleshoot, I enabled the `explain` parameter in my query to see how Elasticsearch is scoring my documents, and I noticed that the geo-polygon filter appears to be returning `true` for documents that are actually outside the polygon, which is confusing. I've tried changing the field mapping for `nested_locations.coordinates` to `geo_shape`, but that resulted in different errors about indexing. When I revert it back to `geo_point`, I don't receive any errors, but the filtering still doesn't work correctly. Is there something I'm missing in my query or the structure of my documents? Any insights or best practices would be greatly appreciated! For context: I'm using Json on Windows. For context: I'm using Json on macOS. This is my first time working with Json 3.11. What's the correct way to implement this? This is for a application running on Ubuntu 20.04. Any ideas how to fix this?