Recursive AI

Geo search query syntax in OneIndex

Write the same geo search query in LINQ, a filter string, or JSON. See what a radius filter excludes and what sorting by distance leaves in.

OneIndex is a free, open-source embedded search library for .NET from Recursive AI, licensed under MIT or Apache-2.0. NuGet · GitHub

Install · .NET 10
dotnet new console
dotnet add package RecursiveAI.OneIndex --version 0.4.0-preview.2

OneIndex 0.4.0-preview.2 is a preview release.

Program.cs · .NET 10
using OneIndex;
using OneIndex.Queries;
 
using var index = SearchIndex.InMemory<Place>();
index.Upsert([
    new() { Id = "far", Location = new(0.02, 0) },
    new() { Id = "near", Location = new(0.005, 0) },
    new() { Id = "center", Location = new(0, 0) }
]);
index.Commit();
 
var linq = index.Search()
    .Where(p => Geo.WithinRadius(p.Location, 0, 0, 1000))
    .OrderByDistance(p => p.Location, 0, 0).Execute();
var filter = FilterString.Parse(
    "_geoRadius(Location, 0, 0, 1000)", index.Schema);
var text = index.SearchBy(filter)
    .OrderByDistance(p => p.Location, 0, 0).Execute();
var json = index.Parse("""
    {"v":1,"filter":{"type":"geoRadius","field":"Location",
                     "lat":0,"lng":0,"meters":1000},
     "sort":["_geoPoint(0, 0):asc"]}
    """).Execute();
var sorted = index.Search()
    .OrderByDistance(p => p.Location, 0, 0).Execute();
 
Console.WriteLine($"LINQ: {string.Join(", ", linq.Select(h => h.Id))}");
Console.WriteLine($"Filter string: {string.Join(", ", text.Select(h => h.Id))}");
Console.WriteLine($"JSON: {string.Join(", ", json.Select(h => h.Id))}");
Console.WriteLine($"Sort only: {string.Join(", ", sorted.Select(h => h.Id))}");
 
public sealed class Place
{
    [SearchId] public string Id { get; set; } = "";
    [Geo] public GeoPoint Location { get; set; }
}
Output from dotnet run
LINQ: center, near
Filter string: center, near
JSON: center, near
Sort only: center, near, far

Each filtered query asks for places within 1,000 meters of the same point and orders them nearest first. The sort-only query keeps far. Sorting changes the order; it doesn’t set a cutoff.

Translate the filter and sort separately

FormFilter and sort
LINQWhere with Geo.WithinRadius on the mapped property. Sort with OrderByDistance on that property.
Filter stringParse _geoRadius against the index schema and pass the result to SearchBy. Chain OrderByDistance; the filter string itself doesn’t set a sort.
Query JSONPut the geoRadius node under filter and name the field. To sort, use _geoPoint with :asc in the top-level sort array.

Use LINQ when the query lives in C# code, a filter string when a caller supplies just the condition, and JSON when the caller needs to send the condition and the order together. These are OneIndex forms, not interchangeable search-engine dialects. The example has one [Geo] field; if the schema has more than one, the filter string must name the field. The JSON distance sort shown here uses the one-field shorthand.

Choose a radius or a bounding box

Use a radius filter for “within 1 km” and a bounding box for “inside the visible map.” A radius takes latitude, then longitude, in degrees and a distance in meters. A box takes corners and includes its edges, even when the box crosses the antimeridian. Neither filter implies nearest-first order.

A document without a point doesn’t match either filter. If a document has several points, any one inside the area makes it a match. An ascending distance sort uses the nearest point and exposes the distance in meters as SearchHit.GeoDistance. See the geo query reference for field rules and additional forms.