ElasticSearch:从无痛脚本中的嵌套字段计算 arcDistance [英] ElasticSearch: compute arcDistance from nested field in Painless script

查看:81
本文介绍了ElasticSearch:从无痛脚本中的嵌套字段计算 arcDistance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Painless 脚本中计算弧距,但在这种情况下还没有找到访问地理 API 的方法,即:

I need to compute an arc distance inside a Painless script, but have not found a way of accessing the geo APIs in this case, i.e:

  • 第一个点作为参数传递给脚本 - 这意味着我只获得原始值
  • 第二点是从嵌套文档中读取的 - 这意味着我无法使用 doc[myGeoField].value API 读取该字段
  • the first point is passed to the script as a param - which means I only get primitive values
  • the second point is read from a nested document - which means I can't read the field using the doc[myGeoField].value API

在这两种情况下,我都无法实例化一个 org.elasticsearch.index.fielddata.ScriptDocValues.GeoPoints 这将使我能够访问 .arcDistance() 方法.

In both cases, I am unable to instantiate a org.elasticsearch.index.fielddata.ScriptDocValues.GeoPoints which would give me access to the .arcDistance() method.

我也无法使用不是 已在 Painless 中列入白名单.

I am also unable to use the org.elasticsearch.common.geo.GeoUtils class which is not whitelisted in Painless.

在脚本中计算弧距还有其他选择吗?

Is there any other option for computing an arc-distance in a script ?

注意:由于在这里解释太长的原因,我必须为此用例使用脚本 - 在查询/过滤器中执行它不是一种选择.

Note: for reasons that would be too long to explain here, I have to use a script for this use case - doing it in a query / filter is not an option.

推荐答案

我用inner_hits解决了这个问题,请看下面的查询:

I solve this issue with inner_hits, see the query below:

{
    "query": {
        "nested": {
            "path": "campi",
            "query": {
                "query_string": {
                    "query": "*"
                }
            },
            "inner_hits": {
                "script_fields": {
                    "distanceInMeters": {
                        "script": {
                            "inline": "!doc['campi.location'].empty ? doc['campi.location'].arcDistance(params.lat, params.lon) : 0",
                            "lang": "painless",
                            "params": {
                                "lon": -43.9207766,
                                "lat": -19.910621
                            }
                        }
                    }
                }
            }
        }
    }
}

下面是响应示例:

{
    "took": 42,
    "timed_out": false,
    "terminated_early": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 42,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search",
                "_type": "baseCourse",
                "_id": "1813-67-14-2309",
                "_score": 1.0,
                "_source": {
                    "cityName": "Belo Horizonte",
                    "institutionName": "CENTRO DE EDUCAÇÃO SUPERIOR",
                    "campi": [
                        {
                            "address_zipCode": null,
                            "address_street": "Avenida Contorno, 6.475 - São Pedro",
                            "stateCode": "MG",
                            "id": 33,
                            "campus_name": "Unidade SEDE"
                        }
                    ],
                    "baseCourseName": "ADMINISTRAÇÃO"
                },
                "inner_hits": {
                    "campi": {
                        "hits": {
                            "total": 1,
                            "max_score": null,
                            "hits": [
                                {
                                    "_nested": {
                                        "field": "campi",
                                        "offset": 0
                                    },
                                    "_score": null,
                                    "fields": {
                                        "distanceInMeters": [
                                            3593.558492923913
                                        ]
                                    },
                                    "sort": [
                                        3593.558492923913
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

这篇关于ElasticSearch:从无痛脚本中的嵌套字段计算 arcDistance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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