针对地理位置查询的条件查询 [英] Conditional query for geo location lookup

查看:205
本文介绍了针对地理位置查询的条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个usecase,我有两种类型的位置(即geo_point类型):

I have a usecase where I have 2 types of locations (i.e. with geo_point type):


  1. 位置1:说出半径= 90英里(它会有所不同),然后输入=输出

  2. 位置2:有一个没有半径的lat / lon,类型=传入

现在,当一个查询进入时:我们预计会发生这种情况:

Now, when a query comes in with: lat/lon and radius=20, I expect this to happen:


  1. 简单的地理查找:如果输入lat / lon在位置2的20m内,返回位置2。

  2. 如果输入lat / lon在90英里的位置1,返回位置1也在结果。如果您注意到,我希望输入半径被特定类型位置的保存的半径覆盖。

这是我拥有的使用脚本:

This is what I have come up with using script:

{
  "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是输入lat / lon,20是输入半径。

Where "37,-121" is input lat/lon and 20 is the input radius.

你对查询有什么看法,这是最好的方法吗?

What do you think about the query, is this the best way to do it?

最初发布了这个ES邮件列表,没有运气。

Originally posted this on ES mailing list, no luck.

推荐答案

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

That seems ok to me. An other way which is a bit more verbose would be to explode your script into various ES filters so that ES can optimize your request as much as possible (you would be able to use the specific geo_distance filter for one of your cases). For instance you could have something like this: (sorry if the json isn't exact, i do not have means to test it right now)

{
  "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"
                        }
                    }
                ]
            }
        ]
    }
  }
}

您可能需要添加一些特定的过滤器来管理文档中可能缺少地理点的事实,以及您希望如何处理此事。例如,您可能希望允许一个文件获取所有没有地理位置的文档,在这种情况下,您可以向应用程序添加一个丢失过滤器。我希望有帮助。

You may have to add some specific filters to manage the fact a geopoint can be missing in your document and how you want to treat this. For instance, you may want to allow one to fetch all the documents without a geopoint in which case you could add a "missing" filter to the "should". I hope that helps.

这篇关于针对地理位置查询的条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆