elasticsearch 如何累计超过';ip';使用脚本的字段
我尝试使用以下内联脚本在“ip”类型的字段上执行术语聚合:
elasticsearch 如何累计超过';ip';使用脚本的字段,
elasticsearch,aggregate,
elasticsearch,Aggregate,我尝试使用以下内联脚本在“ip”类型的字段上执行术语聚合: { "aggs": { "by_ipaddress": { "terms": { "script": { "inline": "doc['ipAddressFrom'].value", "lang": "painless" }
{
"aggs": {
"by_ipaddress": {
"terms": {
"script": {
"inline": "doc['ipAddressFrom'].value",
"lang": "painless"
}
}
}
}
}
它引发以下异常:
"reason": {
"type": "script_exception",
"reason": "runtime error",
"caused_by": {
"type": "array_index_out_of_bounds_exception",
"reason": "16"
},
"script_stack": [
"org.apache.lucene.util.UnicodeUtil.UTF8toUTF16(UnicodeUtil.java:602)",
"org.apache.lucene.util.BytesRef.utf8ToString(BytesRef.java:152)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:83)",
"doc['ipAddressFrom'].value",
" ^---- HERE"
],
"script": "doc['ipAddressFrom'].value",
"lang": "painless"
}
但当我在同一个字段上聚合时:
{
"aggs": {
"by_ipaddress": {
"terms": {
"field": "ipAddressFrom"
}
}
}
}
它起作用了
“ipAddressFrom”字段的映射为:
请让我知道如何在脚本中使用ip字段。对于elasticsearch 6.x,在无痛脚本中使用
ip
键入没有任何问题
您与inline
的聚合不起作用,因为对于某些文档,字段ipAddressFrom
不存在
您可以使用以下方法修复聚合:
"script": {
"inline": "if (doc.containsKey('ipAddressFrom') && !doc['ipAddressFrom'].empty){ return doc['ipAddressFrom'].value} else {return '0'}",
"lang": "painless"
}
我们的方案是,此字段将始终具有非空值。我也通过运行“exists”查询确认了这一点。您正在运行哪个版本的Elasticsearch?我已经用6.2.3版本测试了您的脚本,结果与简单聚合相同。(在我的例子中,所有文档都有该ip字段)我使用的是5.1.1。在es 5.2之前,他们在脚本中处理ip时都有一个bug。见ohhk,这很有帮助。目前,我正在考虑将字段的映射更改为使用多个字段:“{”类型“:”关键字“,”字段“:{”关键字“:{”类型“:”ip“}}”
"script": {
"inline": "if (doc.containsKey('ipAddressFrom') && !doc['ipAddressFrom'].empty){ return doc['ipAddressFrom'].value} else {return '0'}",
"lang": "painless"
}