Recursive AI

Run a geo search query from JSON in C#

OneIndex is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

Pass this JSON to index.Parse(json) to find nearby places.

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

JSON
{
  "v": 1,
  "filter": { "type": "geoRadius",
    "field": "Location", "meters": 1000,
    "lat": 40, "lng": -74 },
  "sort": ["_geoPoint(40, -74):asc"]
}

The example is the full program below: unzip it and dotnet run.

The JSON selects places within 1,000 meters of (40, -74) and puts the nearest first.

The full program

The program includes all three input records. The bakery is inserted first, but the café is closer; the market is outside the radius.

C#
using OneIndex;
 
using var index = SearchIndex.InMemory<Place>();
index.Upsert([
    new() { Id = "Bakery", Location = new(40, -73.99) },
    new() { Id = "Cafe", Location = new(40, -73.995) },
    new() { Id = "Market", Location = new(40, -73.96) }
]);
index.Commit();
 
const string json = """
{
  "v": 1,
  "filter": {
    "type": "geoRadius",
    "field": "Location",
    "lat": 40, "lng": -74,
    "meters": 1000
  },
  "sort": ["_geoPoint(40, -74):asc"]
}
""";
 
foreach (var hit in index.Parse(json))
    Console.WriteLine(FormattableString.Invariant(
        $"{hit.Id}: {hit.GeoDistance:F0} m"));
 
public sealed class Place
{
    [SearchId] public string Id { get; set; } = "";
    [Geo] public GeoPoint Location { get; set; }
}
Text
Cafe: 426 m
Bakery: 852 m

If you misspell Location or use an invalid radius, Parse throws QueryParseException with the JSON path of the problem.

Variations

To keep the same circle but return the farthest matches first, replace the sort property:

JSON
"sort": ["_geoPoint(40, -74):desc"]

The bakery now precedes the café. The market is still excluded.

The short answer

Put a geoRadius node in the JSON filter, set the geographic center with lat and lng, and give meters the maximum distance to include. Add _geoPoint(lat, lng):asc to sort to rank the survivors by distance. The query format requires "v": 1. index.Parse(json) checks the request against the index schema and returns the search you can enumerate. You don’t need to rebuild a LINQ expression from the incoming coordinates.

What is a geo query?

A geo query selects records by position, not by whether their text mentions a place. The example treats each location as one latitude and longitude, in that order, both in degrees. The radius includes the boundary. OneIndex calculates great-circle distance on a sphere, so the number is an estimate of distance along the Earth’s surface, not a route or travel time. If your input comes from another data format, confirm its coordinate order before mapping it to lat and lng.

OneIndex needs an indexed geo field for the radius filter. Mark a GeoPoint or GeoPoint? property with [Geo] as shown; this makes the coordinates available for both filtering and sorting. A missing point won’t match a radius or bounding box. A record with several points matches if any point is within the radius. For a map viewport with four edges rather than a center and radius, use a geoBoundingBox filter instead. See the geo search rules for coordinates and boundary behavior.

How does a radius filter differ from sorting by distance?

Think of a nearby search as two decisions: which results belong in the list, and in what order to show those results. The radius handles the first decision; its presence alone does not rank places by distance. The sort handles the second and does not discard out-of-range places by itself. Use the same center in both to answer “what’s nearest to me within this distance?” Use different centers only if you mean to filter around one place and rank relative to another.

Use hit.GeoDistance for the distance from the sort center in meters. The display in this recipe rounds it to a whole meter, but the sort uses the underlying distance. When your schema has more than one geo field, name the field in the sort too: _geoPoint(Location, 40, -74):asc selects which coordinates drive the ranking. With a multi-point field the nearest point determines ascending distance; descending uses the farthest point, as described in the distance sort documentation.

All oneindex recipes