Elasticsearch脚本查询-按键获取地图参数
Elasticsearch script query - get map parameter by key
在 params 中找到 field1 值的值后,我尝试与找到的字段值进行聚合。
GET /_search
{
"size" : 0,
"query" : {
"ids" : {
"types" : [ ],
"values" : [ "docId1", "docId2" .... ]
}
},
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "def syn = params['field1']; doc[syn].value"
}
}
}
}
}
失败并显示以下消息。
"failures": [
{
"reason": {
"type": "script_exception",
"reason": "failed to run inline script [def syn = params['field1']; doc[syn].value] using lang [groovy]",
"caused_by": {
"type": "missing_property_exception",
"reason": "No such property: params for class: 8f7af04189385c7d3c546861d4817e4ae8ca75f5"
}
}
}
]
如果我直接输入,没有从params中导入值,则正常聚合。
GET /_search
{
"size" : 0,
"query" : {
"ids" : {
"types" : [ ],
"values" : [ "docId1", "docId2" .... ]
}
},
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "doc['country'].value"
}
}
}
}
}
像这样:
{
...
"aggregations": {
"how_to_merge": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "KR",
"doc_count": 90
},
{
"key": "JP",
"doc_count": 83
},
{
"key": "US",
"doc_count": 50
},
{
"key": "BE",
"doc_count": 9
}
]
}
}
}
我正在使用 groovy 和 elasticsearch 版本 2.2
我无法使用 Python 或 JavaScript,这需要安装额外的插件。
为什么我无法获取参数中的值?
如果我没记错的话,在 2.2 中,您不需要在脚本源中为参数添加前缀 params.
。像这样尝试:
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "def syn = field1; doc[syn].value"
}
}
}
}
在 params 中找到 field1 值的值后,我尝试与找到的字段值进行聚合。
GET /_search
{
"size" : 0,
"query" : {
"ids" : {
"types" : [ ],
"values" : [ "docId1", "docId2" .... ]
}
},
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "def syn = params['field1']; doc[syn].value"
}
}
}
}
}
失败并显示以下消息。
"failures": [
{
"reason": {
"type": "script_exception",
"reason": "failed to run inline script [def syn = params['field1']; doc[syn].value] using lang [groovy]",
"caused_by": {
"type": "missing_property_exception",
"reason": "No such property: params for class: 8f7af04189385c7d3c546861d4817e4ae8ca75f5"
}
}
}
]
如果我直接输入,没有从params中导入值,则正常聚合。
GET /_search
{
"size" : 0,
"query" : {
"ids" : {
"types" : [ ],
"values" : [ "docId1", "docId2" .... ]
}
},
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "doc['country'].value"
}
}
}
}
}
像这样:
{
...
"aggregations": {
"how_to_merge": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "KR",
"doc_count": 90
},
{
"key": "JP",
"doc_count": 83
},
{
"key": "US",
"doc_count": 50
},
{
"key": "BE",
"doc_count": 9
}
]
}
}
}
我正在使用 groovy 和 elasticsearch 版本 2.2
我无法使用 Python 或 JavaScript,这需要安装额外的插件。
为什么我无法获取参数中的值?
如果我没记错的话,在 2.2 中,您不需要在脚本源中为参数添加前缀 params.
。像这样尝试:
"aggs": {
"countries": {
"terms": {
"script": {
"params": {
"field1": "country",
"field2": "test",
"field3": "test"
},
"inline": "def syn = field1; doc[syn].value"
}
}
}
}