gpt4 book ai didi

elasticsearch - 如何使用无痛脚本来获取foreach管道指令的当前条目?

转载 作者:行者123 更新时间:2023-12-02 23:17:16 25 4
gpt4 key购买 nike

我正在尝试基于标志来计数JSON对象。
为此,我创建了两个遍历对象的foreach管道。

我想计算documents字段设置为"count"true数组中的所有对象。

POST _ingest/pipeline/_simulate
{
"pipeline":{
"description":"...",
"processors":[
{
"set":{
"field":"specific.docCount",
"value":0
}
},
{
"foreach":{
"field":"data.status.transactions",
"processor":{
"foreach":{
"field":"_ingest._value.documents",
"processor":{
"script":{
"lang":"painless",
"inline":"if (ctx.count) ctx.specific.docCount += 1"
}
}
}
}
}
}
]
},
"docs":[
{
"_source":{
"data":{
"status":{
"transactions":[
{
"id":"123",
"documents":[
{
"count": true
},
{
"count": false
}
]
}
]
}
}
}
}
]
}

我收到以下错误:
{
"docs": [
{
"error": {
"root_cause": [
{
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"header": {
"processor_type": "foreach"
}
}
],
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "ScriptException[runtime error]; nested: NullPointerException;",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if (ctx.count) ",
" ^---- HERE"
],
"script": "if (ctx.count) ctx.stats.docCount += 1",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
}
},
"header": {
"processor_type": "foreach"
}
}
}
]
}

这个 foreach pipeline doc建议使用 ctx来引用摄取文档,但是我不确定如何在我的情况下使用它。

如何在我的脚本中检索当前的“foreach条目”?

最佳答案

我最终用轻松的脚本完成了整个过程。

POST _ingest/pipeline/_simulate
{
"pipeline":{
"description":"...",
"processors":[
{
"set":{
"field":"stats.docCount",
"value":0
}
},
{
"script":{
"lang":"painless",
"inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) {def documents = transaction.documents; for (def document : documents){if (document.count != null && document.count){ctx.stats.docCount += 1}}}"
}
}
]
},
"docs":[
{
"_source":{
"data":{
"status":{
"transactions":[
{
"id":"123",
"documents":[
{
"count":true
},
{
"count":false
}
]
}
]
}
}
}
},
{
"_source":{
"data":{
"status":{
"transactions":[
{
"id":"234",
"documents":[
{
"count":true
},
{
"count":true
}
]
}
]
}
}
}
},
{
"_source":{
"data":{
"status":{
"transactions":[
{
"id":"345",
"documents":[
{

},
{
"count":true
}
]
}
]
}
}
}
}
]
}

输出:
{
"docs": [
{
"doc": {
"_id": "_id",
"_index": "_index",
"_type": "_type",
"_source": {
"data": {
"status": {
"transactions": [
{
"documents": [
{
"count": true
},
{
"count": false
}
],
"id": "123"
}
]
}
},
"stats": {
"docCount": 1
}
},
"_ingest": {
"timestamp": "2018-11-14T13:46:29.963Z"
}
}
},
{
"doc": {
"_id": "_id",
"_index": "_index",
"_type": "_type",
"_source": {
"data": {
"status": {
"transactions": [
{
"documents": [
{
"count": true
},
{
"count": true
}
],
"id": "234"
}
]
}
},
"stats": {
"docCount": 2
}
},
"_ingest": {
"timestamp": "2018-11-14T13:46:29.963Z"
}
}
},
{
"doc": {
"_id": "_id",
"_index": "_index",
"_type": "_type",
"_source": {
"data": {
"status": {
"transactions": [
{
"documents": [
{},
{
"count": true
}
],
"id": "345"
}
]
}
},
"stats": {
"docCount": 1
}
},
"_ingest": {
"timestamp": "2018-11-14T13:46:29.963Z"
}
}
}
]
}

关于elasticsearch - 如何使用无痛脚本来获取foreach管道指令的当前条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53300992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com