在查找其他用户时,我使用Elasticsearch来存储用户位置和他们的距离偏好。它存储在location geo_point和distance整数中。
例如,索引包含以下文档:
当Carlos位于0,0,搜索范围在100米以内时,我需要我的查询来返回Alice,而不是Bob (因为Bob只希望用户在50米以内,Carlos在100米之外)。
换句话说,我想返回所有文档D ,以便 D.reach 包含 Carlos.location , Carlos.reach 包含E 229 D.location**.**
据我所见,唯一能做到这一点的方法是将距离与这样的脚本进行比较:
{
"filter": {
"script": {
"script": "min(doc['distance'].value, distance) >= doc['location'].arcDistance(lat, lon)",
"params": {
"distance": 100,
"lat": 0,
"lon": 0
}
}
}
}然而,如果可能的话,我会而是避免编写脚本。是否有其他方法来实现这一点?
发布于 2015-09-21 09:29:38
由于Val的回答为我指明了正确的方向,我使用了以下解决方案。
文档如下图所示,其中包含用户的位置为geo_point,并以geo_shape的形式到达。
{
"name": "Alice",
"location" : [1,0],
"reach" : {
"type": "shape",
"coordinates": [1,0],
"radius": 100
}
}然后,查询包含两个过滤器:一个用于匹配Carlos在用户范围内的位置,另一个用于匹配用户在Carlos的范围内的位置。
{
"filter": {
"and" : [
{
"geo_shape": {
"preferences.reach": {
"shape": {
"type": "Point",
"coordinates": Carlos.location
}
}
}
},
{
"geo_distance": {
"distance": Carlos.distance,
"user.location" : Carlos.location
}
}
]
}
}这可以用两个geo_shape来完成,但是geo_point的性能更好。
发布于 2015-09-17 03:44:15
另一种值得研究的方法是使用geo_shape circle。因此,您可以将这两个值的组合存储为表示用户的location和distance的reach的圆圈,而不是(或者除了)存储离散值。在您的映射中,它将如下所示:
{
"properties": {
"reach": {
"type": "geo_shape",
"tree": "quadtree",
"precision": "10cm"
}
}
}然后,当您对文档进行索引时,将指定如下所示的reach圆圈:
{
"name": "Alice",
"reach" : {
"type" : "circle",
"coordinates" : [0.0, 100.0], <---- Alice's current location field
"radius" : "100m" <---- Alice's current distance field
}
}
{
"name": "Bob",
"reach" : {
"type" : "circle",
"coordinates" : [100.0, 0.0], <---- Bob's current location field
"radius" : "50m" <---- Bob's current distance field
}
}此时,您的所有用户都将有一个与他们相关联的geo_shape,代表他们的触角。现在,您可以释放ES、geo查询和过滤器的功能,以便找到交叉点或您拥有的东西,例如使用过滤器。其想法是在另一个geo_shape上过滤,表示搜索其他用户的用户的访问范围(例如上面的卡洛斯)
{
"query":{
"filtered": {
"filter": {
"geo_shape": {
"location": {
"shape": {
"type": "circle",
"coordinates" : [0.0, 0.0] <--- Carlos location
"radius": "100m" <--- Carlos reach
}
}
}
}
}
}
}上述查询将查找其reach与过滤器中指定的Carlos‘reach相交的所有文档(即用户)。试试看。
https://stackoverflow.com/questions/32610387
复制相似问题