简体   繁体   English

AWS CloudSearch 中经纬度基于半径的搜索

[英]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.我们公司正在使用 AWS CloudSearch 来搜索和检索用户数据。用户数据由 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 .我们使用 node.js 作为服务器端语言。
Please help .请帮忙 。

In cloudsearch it is not possible to search Latlons within a radius. 在cloudsearch中,无法在半径范围内搜索Latlons。 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 下面是一个示例查询,它执行相同的操作: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。纬度,location.longitude%29&排序=地理%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. 这里:fq =位置%3A%5B%2740.628611,-100.694152%27,%2725.621966,-66.686706%27%5D --->在边界框内搜索Latlons,即左上角和右下角。

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). expr.geo = hasrsin%2838.958687,-77.343149,location.latitude,location.longitude%29&sort = geo%20asc --->创建一个表达式,用于计算搜索文档中LatLon与固定点之间的距离(以此为中心)圈)。 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. 请注意,“hasrsin”距离函数计算LatLons之间的距离,作为完美球体上两点之间的距离。

You want to be ranking results based on the haversin function. 您希望根据hasrsin函数对结果进行排名。 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 ): 以下是使用CloudSearch进行此类查询的示例(来自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. 您选择的服务器端语言无关紧要,因为CloudSearch仅提供REST接口。 Have a look at the Getting Started guide if you haven't yet. 如果您还没有,请查看入门指南。 http://docs.aws.amazon.com/cloudsearch/latest/developerguide/getting-started.html 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 然后循环每个结果并具有if条件

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. 它非常慢并且需要时间,因为节点将更多地取决于数据,但这是您的问题的一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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