Hey everyone, I wanted some advice on the best ap...
# community-help
g
Hey everyone, I wanted some advice on the best approach for a geo-searching problem. I have a collection of locations, where each record stores
coordinates
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!
👀 1
e
Try ordering by rand() to get a sample of the whole radius, then by precision desc
a
Hey George, Your understanding of
precision
is correct. Typesense has no built-in spatial sampling mechanism today. The
rand()
idea idea above is worth a shot:
Copy code
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.