更新所有文档中特定字段的弹性搜索文档字段值

时间:2018-10-24 10:50:01

标签: node.js elasticsearch kibana

我有这样的文件。

{
"a":"test",
"b":"harry"
},
{
"a":""
"b":"jack"
}

我需要在给定索引的所有文档中将字段a==""(空字符串)的文档更新为默认值,例如null。 任何帮助表示赞赏。谢谢

2 个答案:

答案 0 :(得分:1)

使用Update by query with ingest

_update_by_query也可以通过指定如下管道来使用“摄取节点”功能:

定义管道

PUT _ingest/pipeline/set-foo
{
  "description" : "sets foo",
  "processors" : [ {
      "set" : {
        "field": "a",
        "value": null
      }
  } ]
}

然后您可以像使用它一样

 POST myindex/_update_by_query?pipeline=set-foo
{
 "query": {
   "filtered": {
     "filter": {
       "script": {
         "script": "_source._content.length() == 0"
       }
     }
   }
 }
}'

OR

    POST myindex/_update_by_query?pipeline=set-foo
    {
        "query": {
            "bool" : {
                "must" : {
                    "script" : {
                        "script" : {
                            "inline": "doc['a'].empty",
                            "lang": "painless"
                         }
                    }
                }
            }
        }
    }

答案 1 :(得分:0)

要查询具有空字符串字段值的文档,即= ''
我做到了,

"query": {
  "bool": {
    "must": [
      {
        "exists": {
          "field": "a"
        }
      }
    ],
    "must_not": [
      {
        "wildcard": {
          "a": "*"
        }
      }
      ]
    }
  }

因此用于更新所有具有字段a==""的所有文档的查询是

POST test11/_update_by_query
{
  "script": {
    "inline": "ctx._source.a=null",
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "a"
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "a": "*"
          }
        }
      ]
    }
  }
}