关于图形相交查询及Elasticsearch查询报错的技术问询
Hey there! Let's break down your two geospatial questions and work through that query error you're facing with your 780-document GeoJSON index.
1. Matching All Graphics Related to a Single Shape
When you say "related," this usually covers common spatial relationships like intersecting, containing, being contained within, or being adjacent. Assuming you're using a tool like Elasticsearch (since you mentioned index mappings and GeoJSON imports), you'll use the geo_shape query with the relation parameter to define the exact relationship you want.
For example:
- To find all shapes that intersect with a target shape:
GET /your_index/_search { "query": { "geo_shape": { "your_geo_field": { "shape": { "type": "Polygon", // Match the type of your target shape "coordinates": [[/* Your target shape's coordinates here */]] }, "relation": "intersects" } } } } - To find all shapes that are contained within the target shape:
Change therelationvalue to"within". - To find all shapes that contain the target shape:
Use"contains"as therelationvalue.
2. Querying Shapes That Intersect With ID 500
To do this, you first need to reference the shape from the document with ID 500 directly in your query. Here's how:
Option 1: Use indexed_shape to reference the existing document
This is cleaner because it pulls the shape directly from your index instead of copying coordinates:
GET /your_index/_search { "query": { "geo_shape": { "your_geo_field": { "indexed_shape": { "index": "your_index", "id": "500", "path": "your_geo_field" // The field name where the shape is stored in document 500 }, "relation": "intersects" } } } }
Option 2: Fetch the shape first then query
If indexed_shape isn't working for some reason, first grab the shape from document 500:
GET /your_index/_doc/500
Then copy the your_geo_field coordinates into the shape parameter of the geo_shape query (like the example in question 1).
Troubleshooting Query Errors
Since you've already loaded your data, let's check the most common issues that cause geospatial query failures:
- Incorrect Index Mapping: Double-check that your geospatial field is mapped as
geo_shape(notgeo_pointor another type). Verify with:
You should see a section like this for your field:GET /your_index/_mapping"your_geo_field": { "type": "geo_shape" } - Invalid GeoJSON: Ensure all imported documents have valid GeoJSON. Common issues include unclosed polygons (the first and last coordinate must match) or malformed coordinate arrays.
- Query Syntax Mistakes: Typos in parameter names (e.g.,
relatoninstead ofrelation) or mismatched shape types (e.g., using aPointquery against aPolygonfield) will throw errors. Double-check your JSON structure and parameter spelling. - Field Name Mismatch: Make sure the field name in your query matches exactly what's in your index mapping (case-sensitive!).
If you can share the exact error message you're getting, we can narrow this down even more, but these steps should cover most scenarios.
内容的提问来源于stack exchange,提问作者dbj44

