【问题标题】:Query geo distance on elasticsearch with dynamic distance使用动态距离查询弹性搜索上的地理距离
【发布时间】:2018-03-19 22:34:11
【问题描述】:

我遇到了 Elasticsearch 的问题。 我想使用地理距离功能从给定的本地化中获取最大 N 公里的所有项目。

这是我的数据库架构:

{
    "user_id": "abcde",
    "pin" : {
       "location" : {
           "lat" : 40.12,
           "lon" : -71.34
        }
    },
    "is_active": true,
    "action_zone": 50
}

我有一个很好的查询:

{
    "query": {
        "bool" : {
            "must" :
                [{
                    "term": {
                        "is_active": True
                    }
                }],
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}

现在,我想稍微修改一下这个查询,用数据库中每个项目的值“action_zone”动态替换距离(在我的示例中为 200 公里)。

如果有人可以帮助我,那就太好了。 :)

【问题讨论】:

    标签: elasticsearch geolocation


    【解决方案1】:

    我使用脚本找到了解决方案 :D 还是谢谢!

    {
    "query": {
        "bool" : {
            "must" :
                [{
                    "term": {
                        "is_active": True
                    }
                },{
                    "script" : {
                        "script" : {
                            "params": {
                                "lat": 40.8,
                                "lon": -70.1
                            },
                            "source": "doc['location'].arcDistance(params.lat, params.lon) / 1000 < doc['action_zone'].value",
                            "lang": "painless"
                        }
                    }
                }]
            }
        }
    }
    

    }

    文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.1/query-dsl-script-query.html

    【讨论】:

      【解决方案2】:

      很遗憾,geo_distance 查询不允许使用脚本来指定动态距离。但是,您可以做的是在 action_zone 字段上使用 terms 聚合,以便将所有文档存储在特定操作区域中。

      {
        "query": {
          "bool": {
            "must": [
              {
                "term": {
                  "is_active": True
                }
              }
            ],
            "filter": {
              "geo_distance": {
                "distance": "200km",
                "pin.location": {
                  "lat": 40,
                  "lon": -70
                }
              }
            }
          }
        },
        "aggs": {
          "zones": {
            "terms": {
              "field": "action_zone"
            }
          }
        }
      }
      

      否则,您还可以在 action_zone 字段上使用 range 聚合,并具有一些特定距离:

      {
        "query": {
          "bool": {
            "must": [
              {
                "term": {
                  "is_active": "True"
                }
              }
            ],
            "filter": {
              "geo_distance": {
                "distance": "200km",
                "pin.location": {
                  "lat": 40,
                  "lon": -70
                }
              }
            }
          }
        },
        "aggs": {
          "zones": {
            "range": {
              "field": "action_zone",
              "ranges": [
                {
                  "to": 50
                },
                {
                  "from": 50,
                  "to": 100
                },
                {
                  "from": 100,
                  "to": 150
                },
                {
                  "from": 150
                }
              ]
            }
          }
        }
      }
      

      【讨论】:

      • 这个运气好吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2016-08-08
      • 2011-08-26
      • 2012-07-18
      相关资源
      最近更新 更多