Search code examples
elasticsearchgeojsonelasticsearch-geo-shape

Elasticsearch - seach document with geo_shape based on other document geo_shape


is there an option to perform search using one query in ElasticSearch like that:

  1. Get document with ID = 1
  2. This document has a field with geo_shape mapping
  3. Get values from that field
  4. Search for other documents which geo_shape field intersects with doc(id=1) geo_shape
  5. Return found docs

?


Solution

  • Yes, you can use pre-indexed shapes for this purpose.

    POST /_search
    {
        "query": {
            "bool": {
                "must": {
                    "match_all": {}
                },
                    "filter": {
                        "geo_shape": {
                            "your_shape_field": {
                                "indexed_shape": {
                                    "id": "1",
                                    "type": "your_type",
                                    "index": "your_index",
                                    "path": "shape"
                                },
                                "relation": "intersects"
                            }
                        }
                    }
            }
        }
    }
    

    This query will return all documents having your_shape_field intersect the shape field in document with id 1.