按嵌套地理距离属性排序

vatpfxk5  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(413)

我正在尝试按地理距离排序带有嵌套地理点Map的索引。
以下是我的简化Map:

'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

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

pokxtpni

pokxtpni1#

你可以使用 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': {
"score_mode": "multiply",
'query': {
"dis_max": {
"tie_breaker": 0.7,
"boost": 1.9,
"queries": this._query
},
},
'boost': 0.06,
'boost_mode': 'sum',
}

相关问题