Search modes

Geosearch

Store coordinates in a geopoint field in TypeScript, Python, Go, or Rust, keep the documents inside a radius or a polygon, flip either test, and filter across the antimeridian without a search term.

A geopoint field holds a latitude and a longitude. A geo test on that field is an ordinary filter, so you can combine it with a search term, with other filters, and with facets. Every example on this page is part of one program for a courier with depots in Kenya, Tanzania, Fiji, and Tonga. The program filters the depots by a radius and by a polygon, flips each test, and then filters across the antimeridian. Each code block below holds the whole file, with green lines marking the lines that you add at each step and struck lines marking the lines that you remove.

Install the package

The TypeScript, Python, Go, and Rust packages all accept the same geo filters, so every step below has a tab for each language.

pnpm add @delali/narsil

Store a location with each document

A geopoint field holds an object with lat and lon in degrees. The engine refuses a latitude outside -90 to 90, a longitude outside -180 to 180, a coordinate that is not a number, and an array in place of the object, each with SCHEMA_INVALID_GEOPOINT. A radius filter holds a centre, a distance, and a unit of km, mi, or m. With the query below, the engine keeps the depots within 50 km of central Nairobi.

import { createNarsil } from '@delali/narsil'
 
const narsil = await createNarsil()
 
await narsil.createIndex('depots', {
  schema: { name: 'string', city: 'string', location: 'geopoint' },
})
 
await narsil.insertBatch('depots', [
  { id: 'nairobi-central', name: 'Nairobi central depot', city: 'Nairobi', location: { lat: -1.2841, lon: 36.8155 } },
  { id: 'thika-road', name: 'Thika road depot', city: 'Thika', location: { lat: -1.0396, lon: 37.09 } },
  { id: 'nakuru-west', name: 'Nakuru west depot', city: 'Nakuru', location: { lat: -0.3031, lon: 36.08 } },
  { id: 'arusha-central', name: 'Arusha central depot', city: 'Arusha', location: { lat: -3.3869, lon: 36.683 } },
  { id: 'suva-walu', name: 'Suva Walu Bay depot', city: 'Suva', location: { lat: -18.1416, lon: 178.4419 } },
  { id: 'nukualofa-mau', name: "Nuku'alofa depot", city: "Nuku'alofa", location: { lat: -21.1393, lon: -175.2049 } },
])
 
const results = await narsil.query('depots', {
  term: 'depot',
  filters: { fields: { location: { radius: { lat: -1.2841, lon: 36.8155, distance: 50, unit: 'km' } } } },
  document: false,
})
 
console.log(JSON.stringify({ count: results.count, ids: results.hits.map((hit) => hit.id) }, null, 2))
 
await narsil.shutdown()
{
  "count": 2,
  "ids": [
    "nairobi-central",
    "thika-road"
  ]
}

The Thika depot is about 41 km from central Nairobi, so the engine keeps it with the Nairobi depot, while the Nakuru and Arusha depots are outside the circle. The engine measures a radius on a sphere by default. With highPrecision: true, it measures on the WGS-84 ellipsoid, on which the distance from central Nairobi to the Nakuru depot is 135.90 km in place of 136.33 km, while the distance to the Arusha depot is 232.99 km in place of 234.28 km. In Go, the same switch is the HighPrecision field of engine.GeoRadius, while in Rust it is GeoRadius::high_precision(true).

The engine raises SEARCH_INVALID_FILTER for a centre outside the range of latitudes and longitudes, and for a unit other than km, mi, or m. The engine finds the candidates through a grid of cells that covers the circle and then measures the distance to each candidate, so for a small circle, it checks only the points in the few cells that the circle covers.

Keep the documents inside a polygon

A polygon filter holds its corners in order, and you can leave the ring open, because the engine closes it for you. List the corners counter-clockwise around the area, which is the order that GeoJSON uses. For a small area such as this box around central Kenya, the engine matches the same depots in either order. The engine raises SEARCH_INVALID_FILTER for a ring of fewer than three corners or more than 65,536 corners, and for a corner outside the range of latitudes and longitudes.

import { createNarsil } from '@delali/narsil'
 
const narsil = await createNarsil()
 
await narsil.createIndex('depots', {
  schema: { name: 'string', city: 'string', location: 'geopoint' },
})
 
await narsil.insertBatch('depots', [
  { id: 'nairobi-central', name: 'Nairobi central depot', city: 'Nairobi', location: { lat: -1.2841, lon: 36.8155 } },
  { id: 'thika-road', name: 'Thika road depot', city: 'Thika', location: { lat: -1.0396, lon: 37.09 } },
  { id: 'nakuru-west', name: 'Nakuru west depot', city: 'Nakuru', location: { lat: -0.3031, lon: 36.08 } },
  { id: 'arusha-central', name: 'Arusha central depot', city: 'Arusha', location: { lat: -3.3869, lon: 36.683 } },
  { id: 'suva-walu', name: 'Suva Walu Bay depot', city: 'Suva', location: { lat: -18.1416, lon: 178.4419 } },
  { id: 'nukualofa-mau', name: "Nuku'alofa depot", city: "Nuku'alofa", location: { lat: -21.1393, lon: -175.2049 } },
])
 
const results = await narsil.query('depots', {
  term: 'depot',
  filters: { fields: { location: { radius: { lat: -1.2841, lon: 36.8155, distance: 50, unit: 'km' } } } }, 
  filters: { 
    fields: {
      location: {
        polygon: {
          points: [
            { lat: -2, lon: 35.5 },
            { lat: -2, lon: 37.5 },
            { lat: 0.5, lon: 37.5 },
            { lat: 0.5, lon: 35.5 },
          ],
        },
      },
    },
  },
  document: false,
})
 
console.log(JSON.stringify({ count: results.count, ids: results.hits.map((hit) => hit.id) }, null, 2))
 
await narsil.shutdown()
{
  "count": 3,
  "ids": [
    "nairobi-central",
    "nakuru-west",
    "thika-road"
  ]
}

The box encloses Nakuru as well as Nairobi and Thika, while Arusha is south of it. When a query holds a radius and a polygon on the same field, the engine applies both, so a document has to pass both tests.

Keep the documents outside an area

With inside: false on either test, the engine keeps the documents outside the area. In Go, the same flag is Inside: engine.Some(false), while in Rust it is outside(true) on a GeoPolygon or a GeoRadius.

import { createNarsil } from '@delali/narsil'
 
const narsil = await createNarsil()
 
await narsil.createIndex('depots', {
  schema: { name: 'string', city: 'string', location: 'geopoint' },
})
 
await narsil.insertBatch('depots', [
  { id: 'nairobi-central', name: 'Nairobi central depot', city: 'Nairobi', location: { lat: -1.2841, lon: 36.8155 } },
  { id: 'thika-road', name: 'Thika road depot', city: 'Thika', location: { lat: -1.0396, lon: 37.09 } },
  { id: 'nakuru-west', name: 'Nakuru west depot', city: 'Nakuru', location: { lat: -0.3031, lon: 36.08 } },
  { id: 'arusha-central', name: 'Arusha central depot', city: 'Arusha', location: { lat: -3.3869, lon: 36.683 } },
  { id: 'suva-walu', name: 'Suva Walu Bay depot', city: 'Suva', location: { lat: -18.1416, lon: 178.4419 } },
  { id: 'nukualofa-mau', name: "Nuku'alofa depot", city: "Nuku'alofa", location: { lat: -21.1393, lon: -175.2049 } },
])
 
const results = await narsil.query('depots', {
  term: 'depot',
  filters: {
    fields: {
      location: {
        polygon: {
          points: [
            { lat: -2, lon: 35.5 },
            { lat: -2, lon: 37.5 },
            { lat: 0.5, lon: 37.5 },
            { lat: 0.5, lon: 35.5 },
          ],
          inside: false, 
        },
      },
    },
  },
  document: false,
})
 
console.log(JSON.stringify({ count: results.count, ids: results.hits.map((hit) => hit.id) }, null, 2))
 
await narsil.shutdown()
{
  "count": 3,
  "ids": [
    "nukualofa-mau",
    "arusha-central",
    "suva-walu"
  ]
}

The engine returns the three depots outside central Kenya. The engine returns only the documents that hold a location outside the area, so a document with no location appears under neither form of the test.

Filter across the antimeridian without a search term

The engine matches no document for a query without a term, even under a geo filter, so use the listing call to filter an index by location alone. The call is listDocuments in TypeScript and list_documents or ListDocuments in the other languages. The box in this step crosses the antimeridian, which is the line where the longitude changes from 180 to -180, so its corners span from longitude 170 eastwards to -170.

import { createNarsil } from '@delali/narsil'
 
const narsil = await createNarsil()
 
await narsil.createIndex('depots', {
  schema: { name: 'string', city: 'string', location: 'geopoint' },
})
 
await narsil.insertBatch('depots', [
  { id: 'nairobi-central', name: 'Nairobi central depot', city: 'Nairobi', location: { lat: -1.2841, lon: 36.8155 } },
  { id: 'thika-road', name: 'Thika road depot', city: 'Thika', location: { lat: -1.0396, lon: 37.09 } },
  { id: 'nakuru-west', name: 'Nakuru west depot', city: 'Nakuru', location: { lat: -0.3031, lon: 36.08 } },
  { id: 'arusha-central', name: 'Arusha central depot', city: 'Arusha', location: { lat: -3.3869, lon: 36.683 } },
  { id: 'suva-walu', name: 'Suva Walu Bay depot', city: 'Suva', location: { lat: -18.1416, lon: 178.4419 } },
  { id: 'nukualofa-mau', name: "Nuku'alofa depot", city: "Nuku'alofa", location: { lat: -21.1393, lon: -175.2049 } },
])
 
const results = await narsil.query('depots', { 
  term: 'depot', 
const listing = await narsil.listDocuments('depots', { 
  filters: {
    fields: {
      location: {
        polygon: {
          points: [
            { lat: -2, lon: 35.5 }, 
            { lat: -2, lon: 37.5 }, 
            { lat: 0.5, lon: 37.5 }, 
            { lat: 0.5, lon: 35.5 }, 
            { lat: -25, lon: 170 }, 
            { lat: -25, lon: -170 },
            { lat: -15, lon: -170 },
            { lat: -15, lon: 170 },
          ],
          inside: false, 
        },
      },
    },
  },
  document: false,
})
 
console.log(JSON.stringify({ count: results.count, ids: results.hits.map((hit) => hit.id) }, null, 2)) 
console.log(JSON.stringify({ total: listing.total, ids: listing.documents.map((entry) => entry.id) }, null, 2)) 
 
await narsil.shutdown()
{
  "total": 2,
  "ids": [
    "nukualofa-mau",
    "suva-walu"
  ]
}

When the corners span 180 degrees of longitude or more, the engine matches the area on the left of the path of the ring. These corners go counter-clockwise round the area that holds Suva and Nuku'alofa, so the engine returns those two depots. In clockwise order, the same corners enclose the band of latitude on the other side of the globe, so the engine returns no depot. A radius has no such rule, because the engine measures the distance across the line directly. An 800 km circle around Suva therefore encloses Nuku'alofa.

The engine returns no distance with a hit, and it raises SEARCH_INVALID_FIELD for a sort on a geopoint field. To order hits by distance, compute the distance in your application.

Where to go next