---
title: "Query a Spatial Index"
url: "https://docs.ravendb.net/6.2/indexes/querying/spatial"
version: "6.2"
language: "java"
---

# Query a Spatial Index

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/indexes/querying/spatial.md), [Python](https://docs.ravendb.net/6.2/indexes/querying/spatial.python.md), [PHP](https://docs.ravendb.net/6.2/indexes/querying/spatial.php.md), [Node.js](https://docs.ravendb.net/6.2/indexes/querying/spatial.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

To perform a spatial search, you can use the `spatial` method which contains a full spectrum of spatial capabilities.
You can check the detailed Client API reference for this method [here](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.java.md).

## Radius Search

The most basic usage and probably most common one is to search for all points or shapes within provided distance from the given center point. To perform this search use the `withinRadius` method.

### Java

```java
List<Event> results = session
  .query(Event.class)
  .spatial(new PointField("latitude", "longitude"),
      criteria -> criteria.withinRadius(500, 30, 30))
  .toList();
```

### RQL

```sql
from Events
where spatial.within(spatial.point(latitude, longitude), spatial.circle(500, 30, 30))
```

## Advanced Search

The most advanced (and low-level) method available is `relatesToShape`

### Java

```java
List<Event> results = session
  .query(Event.class)
  .spatial(new PointField("latitude", "longitude"),
      criteria -> criteria.relatesToShape(
          "Circle(30 30 d=500.0000)",
          SpatialRelation.WITHIN
      ))
  .toList();
```

### RQL

```sql
from Events
where spatial.within(spatial.point(latitude, longitude), spatial.wkt('Circle(30 30 d=500.0000)'))
```

Where the shape is in [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) format and the relation is one of `within`, `contains`, `disjoint`, `intersects`. The above example will yield the same results as the example from the `Radius Search` section.

> **Info:** Polygons {#polygons}
>
> When using `spatial.wkt()` to define a **polygon**, the vertices (points that form the corners of the polygon) must be listed
> in a counter-clockwise order:
>
> ![NoSQL ACID DB - Query a Spatial Index](https://docs.ravendb.net/assets/images/spatial_1-779c509ab611ef1c93896d4b50a1790d.png)

## Static Indexes

All of the above examples are using the dynamic querying capabilities of RavenDB and will create automatic indexes to retrieve their results. However, spatial queries can also be performed against static indexes, and this is done in a very similar way.

### Java

```java
List<Event> results = session
  .query(Event.class, Events_ByCoordinates.class)
  .spatial("coordinates",
      criteria -> criteria.withinRadius(500, 30, 30))
  .toList();
```

### Index

```java
public static class Events_ByCoordinates extends AbstractIndexCreationTask {
  public Events_ByCoordinates() {
      map = "docs.Events.Select(e => new {" +
          "    Coordinates = this.CreateSpatialField(((double ? ) e.latitude), ((double ? ) e.longitude))" +
          "})";
  }
}
```

### RQL

```sql
from index 'Events/ByCoordinates'
where spatial.within(coordinates, spatial.circle(500, 30, 30))
```

> **Info**
>
> If you want to know how to setup and customize a spatial field in static index please refer to [this](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.java.md) article.

## Ordering

In order to sort the results by distance, please use the `orderByDistance` or `orderByDistanceDescending` methods. You can read more about them [here](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.java.md).

## Remarks

> **Info**
>
> Distance in RavenDB by default is measured in **kilometers**.

## See also

- [Indexing Spatial Data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.java.md)
- [How to make a spatial query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.java.md)
- [How to Query a Spatial Index](https://docs.ravendb.net/6.2/indexes/querying/spatial.java.md)
- [Spatial query view](https://docs.ravendb.net/6.2/studio/database/queries/spatial-queries-map-view.md)
