简体   繁体   中英

Radius Based Search on lat,lon in AWS CloudSearch

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 .

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 ie 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.

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

You can use the following query:

q=nikhil&expr.distance=haversin(35.621966,-120.686706,latlong.latitude,latlong.longitude)&sort=distance%20asc&return=distance

Then loop each results and have an if condition

It is very slow and takes time as the nodes will be more depending on the data but this is one answer to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM