【问题标题】:Calculating distance between two points in elastic search计算弹性搜索中两点之间的距离
【发布时间】:2021-10-29 20:19:35
【问题描述】:

我正在尝试返回作为纬度和经度值提供的两点之间的距离。 这是我尝试做的。

await this.esService.search({
            index: IndexName,
            body: {
                sort: [{ avg_rating: { order: "desc" } }],
                "script_fields" : {
                    "distance" : {
                        "lang": "painless",
                        "params" : {
                           "lat" : 47.1,
                           "lon" : 8.1
                        },
                        "script" : "doc[\u0027user.userAddresses[0].location\u0027].distanceInKm(lat,lon)"
                     }
                 },
                from: offset,
                size: take,
                query: finalQuery
            }
        })

我收到此错误 parsing_exception: [parsing_exception] Reason: Unknown key for a VALUE_STRING in [lang]. 这里有什么问题吗?

【问题讨论】:

    标签: node.js elasticsearch nestjs


    【解决方案1】:

    您就快到了,只是缺少script 声明

                "script_fields" : {
                    "distance": {                  
                      "script" : {             <--- add this
                        "lang": "painless",
                        "params" : {
                           "lat" : 47.1,
                           "lon" : 8.1
                        },
                        "source" : "doc[\u0027user.userAddresses[0].location\u0027].distanceInKm(lat,lon)"
                           ^
                      }    |
                    }      |
                 },  also change this
    

    【讨论】:

    • 实际上在完成上述操作后,我现在收到此错误Unknown key for a START_OBJECT in [script].
    • 你运行的是哪个版本的 ES? Script_fields 指定here