【问题标题】:Sort by nested Geo distance properties按嵌套地理距离属性排序
【发布时间】:2021-02-16 22:06:36
【问题描述】:

我正在尝试按地理距离对具有嵌套地理点映射的索引进行排序。

这是我的简化映射:

'organizations' => [
                '_doc' => [
                    'properties' => [
                        'locations' => [
                            'type' => 'nested',
                            'properties' => [
                                'point' => [
                                    'type' => 'geo_point',
                                    'ignore_malformed' => true
                                ]
                            ]
                        ]
                    ]
                ]
            ]

在这里,每个组织可以有多个位置点。

文件:

PUT /organizations , 
[
        {
            name: "A",
            locations: [
                {
                    point: {
                        lat: 1.5,
                        lon: 2
                    }
                },
                {
                    point: {
                        lat: 1,
                        lon: 0
                    }
                }
            ]
        },
        {
            name: "B",
            locations: [
                {
                    point: {
                        lat: 4.2,
                        lon: 3
                    }
                }
            ]
        },
        {
            name: "C",
            locations: [
                {
                    point: {
                        lat: 0.4,
                        lon: 1
                    }
                }
            ]
        }
    ];

这是我的查询(PHP 数组):

[
  "query" =>  [
    "query_string" => [
      "query" => "*"
    ]
  ]
  "sort" =>  [
    0 =>  [
      "_geo_distance" => [
        "locations.point" => "1, 0.1"
        "order" => "asc"
        "unit" => "km"
        "nested_path" => "locations"
      ]
    ]
  ]
]

预期结果:

1 => name : A
2 => name : C
3 => bame : B

我想获取按 geo_point 排序的资源(检查每个位置,然后检查位置是否接近给定的纬度/经度)

【问题讨论】:

  • 你能添加一些示例文档和预期的搜索结果吗?
  • 您得到的结果与您的预期相比如何?
  • 我添加了文档示例。我根本没有排序结果...
  • @TotoNaBendo 您希望在什么基础上将结果排序为A,C,B?如果你想按升序排列,它应该是C,A,B ?
  • @Bhavya 天哪,我的错误,我更新了排序过滤器

标签: elasticsearch


【解决方案1】:

您可以使用function_score 实现您的目标。

例如查询如下:

注意:代码在js

"_geo_distance": {
  "place.location.geo.coordinates": [
    this._geo.lon,
    this._geo.lat
   ],
  "order": "asc",
  "unit": "km",
  "distance_type": "plane"
}

然后在function_score 中添加script_score

function_score["script_score"] = {
"script": {
        "source": `1000/doc['place.location.geo.coordinates'].planeDistance(${this._geo.lat},${this._geo.lon}) `
}

原来的function_score是这样的:

'function_score': {
  "score_mode": "multiply",
  'query': {
    "dis_max": {
       "tie_breaker": 0.7,
       "boost": 1.9,
       "queries": this._query
    },
  },
  'boost': 0.06,
  'boost_mode': 'sum',
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    相关资源
    最近更新 更多