Our company is using AWS CloudSearch to search and retrieve user data.User data consists of a field position of type lat,lon. So for a given radius and position we should find all the users in the range of radius. How to write search query to retrieve required data ?
We are using node.js as server side language .
Please help .
Radius Based Search on lat,lon in AWS CloudSearch
2.3k views Asked by Sri Harsha At
2
There are 2 answers
0
On
You want to be ranking results based on the haversin function. That is equivalent to "searching within a radius" except it accounts for the fact that you're actually interested in the distance on the surface of a sphere.
Here is an example of such a query with CloudSearch (from http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching-locations.html):
q=restaurant&expr.distance=haversin(35.621966,-120.686706,location.latitude,location.longitude)&sort=distance asc
Your choice of server-side language is irrelevant, as CloudSearch provides only a REST interface. Have a look at the Getting Started guide if you haven't yet. http://docs.aws.amazon.com/cloudsearch/latest/developerguide/getting-started.html
In cloudsearch it is not possible to search Latlons within a radius. You can order by distance, but you can search within a radius.
Since you want all the results within a radius. Instead create a rectangle such that it's four side touches the circle you want to search in. Now, you can search inside a bounding box rectangle and return the results sorted by distance.
The disadvantage is there will be some results which are on the corners of bounding box, so they are not in circle but they will come up in cloudsearch result. You can either use those as well as a approximation or filter further based on distance.
Here is a example query which does the same : q=user*&fq=location%3A%5B%2740.628611,-100.694152%27,%2725.621966,-66.686706%27%5D&expr.geo=haversin%2838.958687,-77.343149,location.latitude,location.longitude%29&sort=geo%20asc
here : fq=location%3A%5B%2740.628611,-100.694152%27,%2725.621966,-66.686706%27%5D ---> searches within the bounding box Latlons i.e. upper-left corner and lower right corner.
expr.geo=haversin%2838.958687,-77.343149,location.latitude,location.longitude%29&sort=geo%20asc ---> creates a expression which computes distance between LatLon in search document to a fixed point(give this as center of your circle). and returns result sorted by distance.
Note that the "haversin" distance function computes distance between LatLons as distance between two points on a perfect sphere.