【问题标题】:Conditional query for geo location lookup地理位置查询的条件查询
【发布时间】:2014-07-02 22:30:51
【问题描述】:

我有一个用例,其中有 2 种类型的位置(即 geo_point 类型):

  1. 位置 1:有纬度/经度,半径 = 90 英里(会有所不同),类型 = 外出
  2. 位置 2:有一个纬度/经度,没有半径并且类型=传入

现在,当查询带有:lat/lon 和 radius=20 时,我预计会发生这种情况:

  1. 简单的地理查找:如果输入的纬度/经度在位置 2 的 20 英里范围内,则返回位置 2。
  2. 如果输入的纬度/经度在位置 1 的 90 英里范围内,则在结果中也返回位置 1。如果您注意到,我希望输入半径被保存的特定类型位置的半径覆盖。

这是我使用脚本想出的:

{
  "query": {
    "match_all": {}
  },
  "filter": {
    "script": {
      "script": "!doc['geopoint'].empty && doc['coverage_type'].value == 'outgoing' ? doc['geopoint'].distanceInMiles(37,-121) <= doc['radius'].value : doc['geopoint'].distanceInMiles(37,-121) <= 20"
    }
  }
}

其中“37,-121”是输入纬度/经度,20是输入半径。

您对查询有何看法,这是最好的方法吗?

原贴this on ES mailing list,不走运。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这对我来说似乎没问题。另一种更冗长的方法是将您的脚本分解为各种 ES 过滤器,以便 ES 可以尽可能地优化您的请求(您可以将特定的 geo_distance 过滤器用于您的一个案例)。 例如你可以有这样的东西:(对不起,如果 json 不准确,我现在没有办法测试它)

    {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
            "should": [
                {
                    "and": [
                        {
                            "term": {
                                "coverage_type": "outgoing"
                            }
                        },
                        {
                            "script": {
                              "script": "!doc['geopoint'].empty && doc['geopoint'].distanceInMiles(37,-121) <= doc['radius'].value"
                            }
                        }
                    ]
                },
                {
                    "and": [
                        {
                            "term": {
                                "coverage_type": "incoming"
                            }
                        },
                        {
                            "geo_distance" : {
                                "geopoint" : [ 37, -121 ],
                                "distance" : "20km"
                            }
                        }
                    ]
                }
            ]
        }
      }
    }
    

    您可能必须添加一些特定的过滤器来管理您的文档中可能缺少地理点的事实以及您希望如何处理它。例如,您可能希望允许在没有地理点的情况下获取所有文档,在这种情况下,您可以在“应该”中添加“缺失”过滤器。我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 2018-09-28
      • 2013-05-27
      • 2014-08-29
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2011-06-09
      相关资源
      最近更新 更多