如果存在完整路径,则使用无痛脚本来增加计数,否则添加完整路径并添加计数

6bc51xsx  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(184)

如果存在字段完整路径,我将创建一个脚本来增加字段的计数值,否则我必须动态添加完整路径。例如,在下面的示例中

1.如果记录已经有inner->board1->count,我应该将它的值增加count的值
1.如果我没有innerboard1count,我应该添加它们,并添加count的值。还请注意,这里的inner orboard1orcount`不是固定的。

如果值不是对象,我可以使用ctx._source.myCounts == null进行检查,但我不确定如何检查对象域、子域和子域。

代码

POST test/_update/3
{
  "script": {
        "source": "ctx._source.board_counts = params.myCounts",
    "lang": "painless",
    "params": {
      "myCounts": {
        "inner":{
          "board1":{"count":5},
          "board2":{"count":4},
          "board3":{"temp":1,"temp2":3}
        },
          "outer":{
          "board1":{"count":5},
          "board10":{"temp":1,"temp2":3}
        }
      }
    }
  }
}
pvcm50d1

pvcm50d11#

我能够想出这一点,而且工作得很好。

POST test/_update/3
{
  "script": {
        "source": "{"source": "if (ctx._source['myCounts'] == null) {ctx._source['myCounts'] = [:];} for (mainItem in params.myCounts) { for (accessItemKey in mainItem.keySet()) { if (ctx._source.myCounts[accessItemKey] == null) { ctx._source.myCounts[accessItemKey] = [:];}for (boardItemKey in mainItem[accessItemKey].keySet()) {if (ctx._source.myCounts[accessItemKey][boardItemKey] == null) {ctx._source.myCounts[accessItemKey][boardItemKey] = [:];} for (countItemKey in mainItem[accessItemKey][boardItemKey].keySet()) { if (ctx._source.myCounts[accessItemKey][boardItemKey][countItemKey] == null) { ctx._source.myCounts[accessItemKey][boardItemKey][countItemKey] =mainItem[accessItemKey][boardItemKey][countItemKey]; }else {ctx._source.myCounts[accessItemKey][boardItemKey][countItemKey] += mainItem[accessItemKey][boardItemKey][countItemKey];}}}}}",
    "lang": "painless",
    "params": {
      "myCounts": {
        "inner":{
          "board1":{"count":5},
          "board2":{"count":4},
          "board3":{"temp":1,"temp2":3}
        },
          "outer":{
          "board1":{"count":5},
          "board10":{"temp":1,"temp2":3}
        }
      }
    }
  }
}

相关问题