---
title: "Run a geo search query from JSON in C#"
url: https://recursiveai.org/oneindex/cookbook/run-geo-search-from-json/
description: "Filter indexed locations by a radius in meters and sort nearest first. Run the JSON query in C# and read each hit’s distance from the center."
version: "0.4.0-preview.2"
---

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

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/oneindex/cookbook/run-geo-search-from-json/run-geo-search-from-json.zip) 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.

```csharp
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.

## Related recipes

- [Find places within a radius with LINQ](https://recursiveai.org/oneindex/cookbook/geo-radius-search-csharp/)
- [Find places inside a map bounding box](https://recursiveai.org/oneindex/cookbook/geo-bounding-box-search-csharp/)
- [Run a search query from JSON](https://recursiveai.org/oneindex/cookbook/run-search-query-from-json/)

## 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](https://recursiveai.org/oneindex/docs/query-json/) 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](https://recursiveai.org/oneindex/docs/geo/) 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](https://recursiveai.org/oneindex/docs/sorting/).
