George C
03/31/2026, 6:03 PMcoordinates and geohash. What I’m trying to do is query around a coordinate with a given radius, and return around 100 documents that are reasonably well distributed across the whole search area, rather than having the results cluster too heavily near the center or in one part of the radius.
Each record also has a rating field, so ideally I’d like to use that to influence ordering within whatever distance buckets or area buckets make sense.
I’ve been looking at precision in sort_by, but my understanding is that it buckets by distance for sorting only, rather than actually ensuring an even spread of results.
Would love to get the team’s thoughts on the best way to approach this, and whether anyone has any general advice or patterns they’d recommend for this kind of use case!Elliot Sawyer
03/31/2026, 10:44 PMAlan Martini
04/01/2026, 5:48 PMprecision is correct. Typesense has no built-in spatial sampling mechanism today.
The rand() idea idea above is worth a shot:
sort_by: coordinates(lat, lng, precision: 5km):asc, _rand:desc
This groups results into 5km rings and shuffles within each ring, which gives you more spread than pure distance sorting. The tradeoff is you lose rating-based ordering within each band.
A few other approaches worth testing:
1. Precision + rating: sort_by: coordinates(lat, lng, precision: 5km):asc, rating:desc. Same distance buckets, but ordered by rating within each. Less spatial diversity than _rand but results are quality-ranked.
2. concentric ring multi-search: Use multi_search to query increasing radii (10km, 20km, 30km) with per_page: 33 and sort_by: rating:desc on each, then deduplicate client-side.
3. Geohash `group_by`: If you store a geohash field truncated to the right precision, you can use group_by with group_limit to cap results per spatial tile. This is the closest to true spatial distribution but you'll need to pre-computing geohash prefixes.