> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Spatial queries

Declare the geometry field last in an `index` entry, after the fields that
partition it:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { t, table } from "@bijectionhq/bijection";

export const places = table(
  { district: t.string, name: t.string, location: t.geometry({ kind: ["Point"] }) },
  { index: [["district", "location"]] },
);
```

Read through the table's addresses, so the table's label applies:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const nearby = await ctx.list(places.where({ district }).near("location", origin, { distance: 5_000 }).first(10));
const inside = await ctx.list(places.where({ district }).region("location", area).first(50));
const close = await ctx.list(places.where({ district }).region("location", origin, { distance: 500 }).first(50));
```

* `.near(field, origin, { distance })` ranks facts by distance from `origin`.
  Each hit is `{ fact, score }`, with `score` the distance in metres;
  `distance` caps it.
* `.region(field, area, { relation, distance })` selects the facts whose
  geometry relates to `area`: `"intersects"` by default, or `"within"`,
  `"coveredBy"`, `"covers"`, `"contains"`, `"disjoint"`, `"equals"`,
  `"touches"`, `"crosses"` or `"overlaps"`. With `distance`, it selects the
  facts within that many metres of `area`.
* Both are always bounded by `.first(n)`. The `.where` fields must fix the
  index's partition fields.

A query over these addresses is watchable like any other: a client's
`watch` or `useWatch` re-runs it when a write lands in what it read.

This is a spatial index over geometry. For embedding vectors see
[Ranked indexes](https://docs.bijection.com/database/indexes.md#ranked-indexes).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.