API version: 0.3.0

---

# Geospatial search

Properties of the [geo types](/reference/formats-and-languages/geojson.md#supported-geometries) are copied to
Elasticsearch as `geo_point` (the type `point`) or `geo_shape` (all other geo types), so the
[geo queries](https://www.elastic.co/docs/reference/query-languages/query-dsl/geo-queries) of Elasticsearch work on
them.

## Setup

The recorded searches on this page run against two schemas of the reference dataset. Venues have a `point` property
`location`:

```json
{
  "name": "Venue",
  "kind": "node",
  "properties": {
    "name": {
      "type": "string",
      "required": false
    },
    "category": {
      "type": "string",
      "required": false
    },
    "location": {
      "type": "point",
      "required": false,
      "dimensions": 2,
      "index": [
        "geo"
      ]
    }
  }
}
```

Delivery zones have a `polygon` property `area`:

```json
{
  "name": "DeliveryZone",
  "kind": "node",
  "properties": {
    "name": {
      "type": "string",
      "required": false
    },
    "area": {
      "type": "polygon",
      "required": false,
      "dimensions": 2,
      "index": [
        "geo"
      ]
    }
  }
}
```

The `geo` index copies both properties to Elasticsearch, see [geo indices](/reference/indices/geo.md).

## Points near a position

The search finds the venues within 3 kilometers of a position, the nearest first. The position is a GeoJSON point in
the step parameters, e.g. sent by an app which knows where its user is. The query reads it with expressions:
`{{ [geoLon(position), geoLat(position)] }}` turns it into the `[longitude, latitude]` array of Elasticsearch, with the
[geo functions](/reference/formats-and-languages/expr-lang/geo-functions.md) `geoLon` and `geoLat`. The conversion is
needed because `geo_distance` does not accept GeoJSON objects. Writing `[13.3777, 52.5163]` into the query directly
works as well, but the position then appears twice, in the query and in the sort.

```json
{
  "debug": true,
  "steps": [
    {
      "type": "elasticsearch-query-dsl-mixin",
      "query": {
        "query": {
          "geo_distance": {
            "distance": "3km",
            "location": "{{ [geoLon(position), geoLat(position)] }}"
          }
        },
        "sort": [
          {
            "_geo_distance": {
              "location": "{{ [geoLon(position), geoLat(position)] }}",
              "order": "asc",
              "unit": "m"
            }
          }
        ]
      },
      "parameters": {
        "nodeTypes": [
          "Venue"
        ],
        "position": {
          "type": "Point",
          "coordinates": [
            13.3777,
            52.5163
          ]
        }
      }
    },
    {
      "type": "element-hydration"
    }
  ]
}
```

- `geo_distance` keeps the venues within the `distance`.
- The `_geo_distance` sort orders them by their distance to the position. Elasticsearch returns the value each result
  was sorted by, here the distance to the position in meters (`"unit": "m"`), as `metadata.sort` of the result; the
  hydration step keeps it. The nearest venue, the Brandenburg Gate, is about 3 meters away:

```json
{
  "type": "Venue",
  "data": {
    "name": "Brandenburg Gate",
    "location": {
      "type": "Point",
      "coordinates": [
        13.377704,
        52.516275
      ]
    }
  },
  "metadata": {
    "score": 1,
    "sort": [
      2.7939852302121655
    ]
  }
}
```

The map shows the position of the query in red; a click on a venue shows its distance.

##### Response Body

```json
{
  "type": "_SearchResultResponse",
  "results": [
    {
      "type": "Venue",
      "id": "02IiNr0GPV85vvz5Itxulpsz4gQx2GKsumc08",
      "data": {
        "location": {
          "type": "Point",
          "coordinates": [
            13.377704,
            52.516275
          ]
        },
        "name": "Brandenburg Gate",
        "scenario": "endpoint.search.geo",
        "category": "monument",
        "createdAt": "2026-12-05T15:20:14+00:00",
        "createdBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3",
        "updatedAt": "2026-12-05T15:20:14+00:00",
        "updatedBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
      },
      "metadata": {
        "score": 1,
        "sort": [
          2.7939852302121655
        ]
      }
    },
    {
      "type": "Venue",
      "id": "02IiNr0GPV85vxWomCYAwStXP74FCMiAJLhni",
      "data": {
        "location": {
          "type": "Point",
          "coordinates": [
            13.376198,
            52.518623
          ]
        },
        "name": "Reichstag Building",
        "scenario": "endpoint.search.geo",
        "category": "building",
        "createdAt": "2026-12-05T15:20:14+00:00",
        "createdBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3",
        "updatedAt": "2026-12-05T15:20:14+00:00",
        "updatedBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
      },
      "metadata": {
        "score": 1,
        "sort": [
          277.5780032414164
        ]
      }
    },
    {
      "type": "Venue",
      "id": "02IiNr0GPV85vyZma9JPm7eSCqHNhtOob5LPe",
      "data": {
        "location": {
          "type": "Point",
          "coordinates": [
            13.390372,
            52.507541
          ]
        },
        "name": "Checkpoint Charlie",
        "scenario": "endpoint.search.geo",
        "category": "monument",
        "createdAt": "2026-12-05T15:20:14+00:00",
        "createdBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3",
        "updatedAt": "2026-12-05T15:20:14+00:00",
        "updatedBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
      },
      "metadata": {
        "score": 1,
        "sort": [
          1297.6864988703953
        ]
      }
    },
    "... 2 more items omitted (shapes seen: type:Venue) ..."
  ],
  "debug": [
    {
      "input": {
        "type": "elasticsearch-query-dsl-mixin",
        "query": {
          "query": {
            "geo_distance": {
              "distance": "3km",
              "location": "{{ [geoLon(position), geoLat(position)] }}"
            }
          },
          "sort": [
            {
              "_geo_distance": {
                "location": "{{ [geoLon(position), geoLat(position)] }}",
                "order": "asc",
                "unit": "m"
              }
            }
          ]
        },
        "parameters": {
          "nodeTypes": [
            "Venue"
          ],
          "position": {
            "type": "Point",
            "coordinates": [
              13.3777,
              52.5163
            ]
          },
          "stepResults": []
        }
      },
      "step-handler": "elasticsearch-query-dsl-mixin (App\\EventSystem\\SearchStep\\EventListener\\ElasticsearchQueryDslMixinSe...",
      "start": 1796484201.852913,
      "indices": "node_venue",
      "combinedQuery": {
        "_source": [
          "_id"
        ],
        "from": 0,
        "size": 25,
        "query": {
          "bool": {
            "must": [
              {
                "geo_distance": {
                  "distance": "3km",
                  "location": [
                    13.3777,
                    52.5163
                  ]
                }
              }
            ],
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "_groupsWithSearchAccess.keyword": []
                      }
                    },
                    {
                      "term": {
                        "_usersWithSearchAccess.keyword": {
                          "value": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
                        }
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        "sort": [
          {
            "_geo_distance": {
              "location": [
                13.3777,
                52.5163
              ],
              "order": "asc",
              "unit": "m"
            }
          }
        ],
        "track_scores": true
      },
      "end": 1796484202.227694,
      "duration": 0.374781
    },
    {
      "input": {
        "type": "element-hydration",
        "query": null,
        "parameters": {
          "stepResults": [
            {
              "elements": [
                {
                  "id": "02IiNr0GPV85vvz5Itxulpsz4gQx2GKsumc08",
                  "type": "Venue",
                  "metadata": {
                    "score": 1,
                    "sort": [
                      2.7939852302121655
                    ]
                  }
                },
                {
                  "id": "02IiNr0GPV85vxWomCYAwStXP74FCMiAJLhni",
                  "type": "Venue",
                  "metadata": {
                    "score": 1,
                    "sort": [
                      277.5780032414164
                    ]
                  }
                },
                {
                  "id": "02IiNr0GPV85vyZma9JPm7eSCqHNhtOob5LPe",
                  "type": "Venue",
                  "metadata": {
                    "score": 1,
                    "sort": [
                      1297.6864988703953
                    ]
                  }
                },
                "... 2 more items omitted (shapes seen: type:Venue) ..."
              ],
              "totalHits": 5,
              "maxScore": 1
            }
          ]
        }
      },
      "step-handler": "element-hydration (App\\EventSystem\\SearchStep\\EventListener\\ElementHydrationSearchStepEventListener)",
      "start": 1796484202.228072,
      "accessibleElementIds": [
        "02IiNr0GPV85vvz5Itxulpsz4gQx2GKsumc08",
        "02IiNr0GPV85vxWomCYAwStXP74FCMiAJLhni",
        "02IiNr0GPV85vyZma9JPm7eSCqHNhtOob5LPe",
        "... 2 more items omitted (shapes seen: string) ..."
      ],
      "end": 1796484202.246522,
      "duration": 0.01845
    }
  ]
}
```

##### Response Headers

```http
Access-Control-Allow-Headers: Authorization, Content-Type, Prefer
Access-Control-Allow-Methods: OPTIONS, POST
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Preference-Applied
Allow: OPTIONS, POST
Cache-Control: no-cache, private
Content-Type: application/json; charset=utf-8
Date: Sat, 05 Dec 2026 15:23:13 GMT
Transfer-Encoding: chunked
Vary: Prefer, Accept-Encoding
X-Powered-By: Ember Nexus API
```

A memorial between the Brandenburg Gate and Checkpoint Charlie is not part of the result: it belongs to another user,
and the API adds the access filter to every query. Tempelhof Field (about 5 kilometers away) and Sanssouci Palace in
Potsdam are too far away.

Distances in Elasticsearch are `arc` distances on a sphere by default, the same model as the
[geo functions](/reference/formats-and-languages/expr-lang/geo-functions.md#semantics) of Expr, so rules and searches agree
on how far apart two points are.

## Areas which contain a point

The search finds the delivery zones which contain a point, sorted by their name. The `geo_shape` query with the relation
`contains` matches shapes which contain the query shape.

```json
{
  "debug": true,
  "steps": [
    {
      "type": "elasticsearch-query-dsl-mixin",
      "query": {
        "query": {
          "geo_shape": {
            "area": {
              "shape": {
                "type": "Point",
                "coordinates": [
                  13.390372,
                  52.507541
                ]
              },
              "relation": "contains"
            }
          }
        },
        "sort": [
          {
            "name.keyword": "asc"
          }
        ]
      },
      "parameters": {
        "nodeTypes": [
          "DeliveryZone"
        ]
      }
    },
    {
      "type": "element-hydration"
    }
  ]
}
```

##### Response Body

```json
{
  "type": "_SearchResultResponse",
  "results": [
    {
      "type": "DeliveryZone",
      "id": "02IiNr0GPV85w6tZgvD3tAfKcwiR2fkYTGxIk",
      "data": {
        "area": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                13.38,
                52.485
              ],
              [
                13.45,
                52.485
              ],
              [
                13.45,
                52.51
              ],
              "... 2 more items omitted (shapes seen: array) ..."
            ]
          ]
        },
        "name": "Kreuzberg",
        "scenario": "endpoint.search.geo",
        "createdAt": "2026-12-05T15:20:14+00:00",
        "createdBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3",
        "updatedAt": "2026-12-05T15:20:14+00:00",
        "updatedBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
      },
      "metadata": {
        "score": 0,
        "sort": [
          "Kreuzberg"
        ]
      }
    },
    {
      "type": "DeliveryZone",
      "id": "02IiNr0GPV85w5qxpaRKWDsrNu6143uUrkiHU",
      "data": {
        "area": {
          "type": "Polygon",
          "coordinates": [
            [
              [
                13.36,
                52.5
              ],
              [
                13.42,
                52.5
              ],
              [
                13.42,
                52.53
              ],
              "... 2 more items omitted (shapes seen: array) ..."
            ]
          ]
        },
        "name": "Mitte",
        "scenario": "endpoint.search.geo",
        "createdAt": "2026-12-05T15:20:14+00:00",
        "createdBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3",
        "updatedAt": "2026-12-05T15:20:14+00:00",
        "updatedBy": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
      },
      "metadata": {
        "score": 0,
        "sort": [
          "Mitte"
        ]
      }
    }
  ],
  "debug": [
    {
      "input": {
        "type": "elasticsearch-query-dsl-mixin",
        "query": {
          "query": {
            "geo_shape": {
              "area": {
                "shape": {
                  "type": "Point",
                  "coordinates": [
                    13.390372,
                    52.507541
                  ]
                },
                "relation": "contains"
              }
            }
          },
          "sort": [
            {
              "name.keyword": "asc"
            }
          ]
        },
        "parameters": {
          "nodeTypes": [
            "DeliveryZone"
          ],
          "stepResults": []
        }
      },
      "step-handler": "elasticsearch-query-dsl-mixin (App\\EventSystem\\SearchStep\\EventListener\\ElasticsearchQueryDslMixinSe...",
      "start": 1796484202.288211,
      "indices": "node_deliveryzone",
      "combinedQuery": {
        "_source": [
          "_id"
        ],
        "from": 0,
        "size": 25,
        "query": {
          "bool": {
            "must": [
              {
                "geo_shape": {
                  "area": {
                    "shape": {
                      "type": "Point",
                      "coordinates": [
                        13.390372,
                        52.507541
                      ]
                    },
                    "relation": "contains"
                  }
                }
              }
            ],
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "_groupsWithSearchAccess.keyword": []
                      }
                    },
                    {
                      "term": {
                        "_usersWithSearchAccess.keyword": {
                          "value": "02IiNr0GPV85vqHhM49BeOGnFv13ra0t5Xvd3"
                        }
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        "sort": [
          {
            "name.keyword": "asc"
          }
        ],
        "track_scores": true
      },
      "end": 1796484202.29779,
      "duration": 0.009579
    },
    {
      "input": {
        "type": "element-hydration",
        "query": null,
        "parameters": {
          "stepResults": [
            {
              "elements": [
                {
                  "id": "02IiNr0GPV85w6tZgvD3tAfKcwiR2fkYTGxIk",
                  "type": "DeliveryZone",
                  "metadata": {
                    "score": 0,
                    "sort": [
                      "Kreuzberg"
                    ]
                  }
                },
                {
                  "id": "02IiNr0GPV85w5qxpaRKWDsrNu6143uUrkiHU",
                  "type": "DeliveryZone",
                  "metadata": {
                    "score": 0,
                    "sort": [
                      "Mitte"
                    ]
                  }
                }
              ],
              "totalHits": 2,
              "maxScore": 0
            }
          ]
        }
      },
      "step-handler": "element-hydration (App\\EventSystem\\SearchStep\\EventListener\\ElementHydrationSearchStepEventListener)",
      "start": 1796484202.298187,
      "accessibleElementIds": [
        "02IiNr0GPV85w6tZgvD3tAfKcwiR2fkYTGxIk",
        "02IiNr0GPV85w5qxpaRKWDsrNu6143uUrkiHU"
      ],
      "end": 1796484202.308191,
      "duration": 0.010004
    }
  ]
}
```

##### Response Headers

```http
Access-Control-Allow-Headers: Authorization, Content-Type, Prefer
Access-Control-Allow-Methods: OPTIONS, POST
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Preference-Applied
Allow: OPTIONS, POST
Cache-Control: no-cache, private
Content-Type: application/json; charset=utf-8
Date: Sat, 05 Dec 2026 15:23:13 GMT
Transfer-Encoding: chunked
Vary: Prefer, Accept-Encoding
X-Powered-By: Ember Nexus API
```

Two of the six zones contain the point. One zone around it has a hole exactly where the point is, so the point is not
part of that zone, another one belongs to another user. Other relations are `intersects` (the default), `within` (the
zones inside of the query shape) and `disjoint`.

In schema rules, the geo functions
[`geoContains`](/reference/formats-and-languages/expr-lang/geo-functions.md#geocontains), `geoWithin` and `geoIntersects`
check the same relations for single elements, e.g. that the location of a venue lies
inside of its delivery zone.

---

Home: [Ember Nexus API docs](/index.md)

Full site map: [llms.txt](/llms.txt)

Previous: [Vector search](/reference/search/elasticsearch-query-dsl-mixin/vector-search.md)

Next: [Combining queries with bool](/reference/search/elasticsearch-query-dsl-mixin/combining-queries-with-bool.md)
