包含GeoPoint的位置的列表-(geo_spatial_filter_fields,geo_distance) [英] A list of locations that contain a GeoPoint - (geo_spatial_filter_fields, geo_distance)

查看:42
本文介绍了包含GeoPoint的位置的列表-(geo_spatial_filter_fields,geo_distance)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用elasticsearch-dsl-drf,我只是将文档上的单个 location 字段转换为具有以下定义的 NestedField :

I'm using elasticsearch-dsl-drf, and I just converted a single location field on my document, to a NestedField with this definition:

location = fields.NestedField(properties = {"point":fields.GeoPointField()})

然后在我看来(我添加了 path 并更改了 field 值以使其起作用):

Then on my view I have (I added path and changed the field value to try and make it work):

    geo_spatial_filter_fields = {
        'location': {
            'path': 'location',
            'field': 'point',
            'lookups': [constants.LOOKUP_FILTER_GEO_DISTANCE]
        }
    }

    geo_spatial_ordering_fields = {'location': None}

我想知道如何实现?我想根据每个文档的位置列表中最接近的位置对所有文档进行排序.

I'm wondering how this can be achieved? I want to order all the documents based on the closest location from a list of locations for each document.

修改

当前正在试验(已更改的elasticsearch dsl drf以使用此功能):

Currently experimenting with (changed elasticsearch dsl drf to use this):

{
   "query":{
      "nested":{
         "path":"location",
         "query":{
            "geo_distance":{
               "distance":"16090km",
               "location.point":{
                  "lat":"52.240995",
                  "lon":"0.751156"
               },
               "distance_type":"arc"
            }
         }
      }
   },
   "sort":[
      {
         "_geo_distance":{
            "location.point":{
               "lat":"52.240995",
               "lon":"0.751156"
            },
            "unit":"km",
            "distance_type":"plane",
            "order":"asc"
         }
      },
      {
         "date_first_registered":{
            "order":"desc"
         }
      }
   ]
}

这似乎已执行,但排序已关闭.

This seems to execute but the sorting is off.

感谢您的宝贵时间,谢谢

I appreciate your time, Thanks

elasticsearch-dsl-drf的解决方案

def attach_nested_path_to_queryset(queryset: Search):
    """
    Attach nested path to the query and sort keys in the queryset
    and update the queryset using `update_from_dict`

    **The updating is done by reference**

    :param queryset: the original queryset
    :type queryset: Search

    :return:
    """
    queryset_dict = queryset.to_dict()

    attach_nested_path_to_query(queryset_dict)
    attach_nested_path_to_sort(queryset_dict)

    # update queryset
    queryset.update_from_dict(queryset_dict)


def attach_nested_path_to_query(queryset_dict: dict):
    """
    Looks for geo_distance in the queryset dict, if it's at the top level
    we modify the top level query, meaning that there's only one query, otherwise
    we loop over the list of `must` queries and try to find `geo_distance`

    **The updating is done by reference**

    :param queryset_dict: the queryset in dict format
    :type queryset_dict: dict

    :return:
    """
    query = queryset_dict["query"]

    if "geo_distance" in query:
        queryset_dict["query"] = {"nested": {"path": "location", "query": query}}
    elif "bool" in query and "must" in query["bool"]:
        for index, must_query in enumerate(query["bool"]["must"]):
            if "geo_distance" in must_query:
                queryset_dict["query"]["bool"]["must"][index] = {"nested": {"path": "location", "query": must_query}}

                break


def attach_nested_path_to_sort(queryset_dict: dict):
    """
    This function loops over the `sort` queries, and
    looks for `_geo_distance` in order to add the `nested_path` key/value

    **The updating is done by reference**

    :param queryset_dict: the queryset in dict format
    :type queryset_dict: dict

    :return:
    """
    sort = queryset_dict["sort"]

    if isinstance(sort, list):
        for index, sorting in enumerate(sort):
            if "_geo_distance" in sorting:
                queryset_dict["sort"][index]["_geo_distance"]["nested_path"] = "location"

推荐答案

在地理距离排序中包括"nested_pa​​th":"location" :

Include "nested_path": "location" in the geo distance sort:

{
   "query":{
      "nested":{
         "path":"location",
         "query":{
            "geo_distance":{
               "distance":"16090km",
               "location.point":{
                  "lat":52.240995,
                  "lon":0.751156
               },
               "distance_type":"arc"
            }
         }
      }
   },
   "sort":[
      {
         "_geo_distance":{
            "nested_path": "location",
            "location.point":{
               "lat":52.240995,
               "lon":0.751156
            },
            "unit":"km",
            "distance_type":"plane",
            "order":"asc"
         }
      },
      {
         "date_first_registered":{
            "order":"desc"
         }
      }
   ]
}

这篇关于包含GeoPoint的位置的列表-(geo_spatial_filter_fields,geo_distance)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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