Geo search - distance in record

Hi Guys,

I’ve just got a problem with the geo search. Normally you store a lat and lng in your record and then you query them with ‘aroundradius’(+ lat and lng). For my use case i have to flip this around.
I would like to store the radius also in the record and query the records afterwards with a lat and lng value only. If they are in the radius of the given record i would like to display them.

Do any of you know a solution to that problem?

Hello @mb1,

That’s an interesting use case, though I am not sure to get the rationale behind it. If I understood correctly, you wish to have a per-record radius. That would be a kind of measurement of the “geographical importance” of the record? A kind of “area of influence”? For example, you could retrieve small towns around you, as well as bigger cities farther away, but not both small and far away towns?

Whatever the reason, there is no way to achieve that out of the box with Algolia. I can think of two workarounds, however:

  1. Query the maximum radius, then filter out records based on their radius. You can know what is each record’s distance to the target location by passing getRankingInfo=true and examining the _rankingInfo.geoDistance field of each hit. The problem with this approach is that, if there are many hits with a small radius, you would need to retrieve a lot of them. That’s why I would recommend the second approach:

  2. Perform multiple queries with varying aroundRadius then merge them. Each query would have a numeric filter on the custom radius field to ensure that only records with that radius are retrieved. Again, getRankingInfo should allow you to meaningfully compare ranking across the various queries (provided that the only varying parameter is aroundRadius). It’s a bit more work, but would probably be a lot more efficient. That would be my recommendation.

What do you think?

Hi Clement,

thanks a lot for your response :). To get a little bit more in detail. Our use case is something like this:

  • Person A registers on our platform and is willing to drive 20 miles for an event
  • We create an event at a specific location and see how many people would be willing to travel to that location

I really like your second approach since we would only lose a minor fraction of people but we could still provide awesome performance.
Maybe we could even tune this a bit by using heuristics and query only once with a radius where 95% of the people would fall into.

You saved my day - I’ve never looked on the problem from this side. Looking forward to use your platform extensively.

Greets
Mario