使用弹性搜索功能评分查询与地理点的衰减,是否可以设置目标距离? [英] With Elasticsearch function score query with decay against a geo-point, is it possible to set a target distance?

查看:141
本文介绍了使用弹性搜索功能评分查询与地理点的衰减,是否可以设置目标距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎不可能基于 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html ,但我想确认。

It doesn't seem possible based on https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html , but I'd like confirmation.

在简单的英文中,我要求从地理位置得到结果(距离某个纬度,经度来源距离500公里)。

In plain English, I'm asking to score the results (with geo-point locations) by how close they are to 500km from some latitude, longitude origin.

这是令人困惑的,因为有一个名为offset的参数,但根据文档,它似乎不是来自原点的偏移(例如,距离),而是似乎意味着阈值

It's confusing because there is a parameter called "offset" but according to the documentation it doesn't seem to be an offset from origin (eg. distance) but instead seems to mean "threshold" instead.

推荐答案

我看到几种方法来实现这一点:

I see a few ways to accomplish this:

一种。一种方法是以距离原点相反的顺序排列。您可以使用 geo_distance 查询,然后按距离排序。在以下查询中,最遥远的文档将首先出现,即排序值与原点的距离,我们按降序排列。

A. One way would be to simply sort by distance in reverse order from the origin. You'd use a geo_distance query and then sort by distance. In the following query, the most distant documents will come up first, i.e. the sort value is the distance from the origin and we're sorting in decreasing order.

{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "from" : "100km",
          "to" : "200km",
          "location": {
            "lat": 10,
            "lon": 20
          }
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 10,
          "lon": 20
        },
        "order": "desc",
        "unit": "km",
        "distance_type": "plane"
      }
    }
  ]
}

B。第二种方法是使用 geo_distance_range 查询,以便在原点周围定义环。该环的宽度可能以某种方式表示您将在高斯函数中使用的偏移量值(尽管不会有衰减)。在这里,我们定义一个距原点500公里的距离为10公里的戒指,并在该戒指中以距离对文档进行排序。

B. The second way involves using a geo_distance_range query in order to define a "ring" around the origin. The width of that ring could somehow symbolize the offset + scale you'd use in a gauss function (although there would be no decay). Here we define a ring that is 10km wide at 500km distance from the origin point and sort the documents by distance in that ring.

{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance_range": {
          "from": "495km",
          "to": "505km",
          "location": {
            "lat": 10,
            "lon": 20
          }
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 10,
          "lon": 20
        },
        "order": "desc",
        "unit": "km",
        "distance_type": "plane"
      }
    }
  ]
}

C。最后一个方法是更多的参与。我们基本上是一个反高斯的形状,基本上是这个数字(33),但是颠倒了,或者这一个这更好地代表了我们之后的甜甜圈形状。我们可以将上面的解决方案B与仅在该戒指中得分的 gauss 函数相结合。在下面的查询中,我们基本上是说,我们只对距离起点约500公里的位置感兴趣,我们让高斯功能只针对这些文件。这不是完美的,但可能足够接近你需要的。

C. The last way is a bit more involved. We're basically after an "inverse gauss" shape, basically this figure (33), but upside-down, or this one which better represents the donut shape we're after. We can combine solution B above with a gauss function that would only score within that ring. In the query below, we're basically saying that we're only interested in the locations around 500km from the origin and we let a gauss function kick in only for those documents. It's not perfect, though, but might be close enough to what you need.

{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance_range": {
          "from": "495km",
          "to": "505km",
          "location": {
            "lat": 10,
            "lon": 20
          }
        }
      },
      "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "location": {
                  "origin": {
                    "lat": 10,
                    "lon": 20
                  },
                  "offset": "500km",
                  "scale": "5km"
                }
              }
            }
          ]
        }
      }
    }
  },
  "sort": {
    "_score": "desc"
  }
}  

这篇关于使用弹性搜索功能评分查询与地理点的衰减,是否可以设置目标距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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