为每个文档将两个字段复制到对象

k3bvogb1  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(303)

如何为同一文档复制对象中的两个字段。我在elasticsearch中找到了一个对象

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "doc",
        "_id": "AXWTrVr6LIkj1JVvPnDX",
        "_score": 1,
        "_source": {
          "field1": 1,
          "field2": 2
        }
      }
    ]
  }
}

我想抄 field1 以及 field2 每个文档的测试对象。预期结果

{
      "took": 0,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "test_index",
            "_type": "doc",
            "_id": "AXWTrVr6LIkj1JVvPnDX",
            "_score": 1,
            "_source": {
              "field1": 1,
              "field2": 2,
              "test_object":{
                 "field1": 1,
                 "field2": 2
}
            }
          }
        ]
      }
    }

我正试图通过下一个脚本来完成,但我不明白怎么了

POST test_index/doc/update
{
  "query":{
    "match":{
      "field1":1
    }
  },
  "script" : {
      "inline": "ctx._source.test_field.field1 = ctx._source.field1 ctx._source.test_field.field2 = ctx._source.field2"
  }
}
utugiqy6

utugiqy61#

首先你要打电话 _update_by_query 终结点
那么,自从 test_field 文档中不存在,您需要创建它:
这应该适合您:

POST test_index/_update_byquery
{
  "query":{
    "match":{
      "field1":1
    }
  },
  "script" : {
      "inline": "ctx._source.test_field = ['field1': ctx._source.field1, 'field2': ctx._source.field2]"
  }
}

相关问题