elasticsearch 用于计算对象数组统计信息的ElasticSearch脚本(作为重新索引或查询更新的一部分)
我将以下文档存储在ES中 映射:
elasticsearch 用于计算对象数组统计信息的ElasticSearch脚本(作为重新索引或查询更新的一部分),
elasticsearch,
elasticsearch,我将以下文档存储在ES中 映射: "sessions_v4": { "mappings": { "perfData": { "properties": { "perfSamples": { "properties": { "perfValue": { "type": "long" }, "timestamp": { "type": "long"
"sessions_v4": {
"mappings": {
"perfData": {
"properties": {
"perfSamples": {
"properties": {
"perfValue": {
"type": "long"
},
"timestamp": {
"type": "long"
}
}
},
"sessionId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"version": {
"type": "long"
}
}
}
}
这是具有上述映射的示例文档
"hits": [
{
"_index": "sessions_v4",
"_type": "perfData",
"_id": "AVraIIvbQlFV0tfF1gDd",
"_score": 1,
"_source": {
"perfSamples": [
{
"perfValue": 19,
"timestamp": 72
},
{
"perfValue": 29,
"timestamp": 1072
},
{
"perfValue": 30,
"timestamp": 2072
},
{
"perfValue": 30,
"timestamp": 3072
},
...
]
}
存储了数千个文档,我正在寻找一种方法来计算这些文档上的一些自定义度量,并使用新计算的度量添加一个新字段。例如,我想为每个文档添加“total”perfValue,作为一个新字段“perfTotal”和“perfSamples”
我试过下面的脚本
GET sessions_v4/perfData/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_perf": {
"script": {
"lang": "painless",
"inline": "int total = 0; for (int i = 0; i < doc['perfSamples'].length; ++i) { total += i; } return total;"
}
}
}
}
GET sessions\u v4/perfData/\u search
{
“查询”:{
“全部匹配”:{}
},
“脚本_字段”:{
“总性能”:{
“脚本”:{
“郎”:“无痛”,
“inline”:“inttotal=0;for(inti=0;i
我基本上修改了下面提供的脚本
但是我得到了以下错误:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 4,
"failed": 1,
"failures": [
{
"shard": 0,
"index": "sessions_v4",
"node": "PG8xNDX3Q2u-vK6EmRI7Ng",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [perfSamples] in mapping with types [perfData]"
},
"script_stack": [
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)",
"i = 0; i < doc[‘perfSamples'].length; ++i) { ",
" ^---- HERE"
],
"script": "int total = 0; for (int i = 0; i < doc[‘perfSamples'].length; ++i) { total += i; } return total;",
"lang": "painless"
}
}
]
},
"hits": {
"total": 56127,
"max_score": 1,
"hits": []
}
}
{
"take":3,,
“超时”:false,
“_碎片”:{
“总数”:5,
“成功”:4,
“失败”:1,
“失败”:[
{
“碎片”:0,
“索引”:“会话4”,
“节点”:“PG8xNDX3Q2u-vK6EmRI7Ng”,
“理由”:{
“类型”:“脚本异常”,
“原因”:“运行时错误”,
“原因”:{
“类型”:“非法参数\异常”,
“原因”:“在与[perfData]类型的映射中找不到[perfSamples]的字段”
},
“脚本堆栈”:[
“org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:80)”,
“i=0;i
有什么想法吗?我们才刚刚开始使用ES,所以可能是个小问题:)
更新:将脚本更改为以下内容,可以正常工作
GET sessions_v4/perfData/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"total_perf": {
"script": {
"lang": "painless",
"inline": "int total = 0; for (int i = 0; i < doc['perfSamples.perfValue'].values.size(); ++i) { total += i; } return total;"
}
}
}
}
GET sessions\u v4/perfData/\u search
{
“查询”:{
“全部匹配”:{}
},
“脚本_字段”:{
“总性能”:{
“脚本”:{
“郎”:“无痛”,
“内联”:“inttotal=0;for(inti=0;i
可能是您没有使用正确的勾号吗?它应该是'
而不是'
@Val-我不这么认为。我将Kibana控制台输出复制到一个文本编辑器,编辑器将其从“改为”。如果您查看查询,它会使用正确的tick@Val.使用以下文档['perfSamples.perfValue']解决.values.size()可能是您没有使用正确的勾号吗?它应该是'
而不是'
@Val-我不这么认为。我将Kibana控制台输出复制到一个文本编辑器,编辑器将其从“改为”。如果查看查询,它会使用正确的tick@Val.使用以下文档解决['perfSamples.perfValue'].values.size()