【问题标题】:Update Elastic search nested index value更新弹性搜索嵌套索引值
【发布时间】:2020-04-16 11:17:04
【问题描述】:

我在弹性搜索中有一个包含嵌套值的索引,我想用 Id 更新嵌套索引中的值,我该如何实现。我的索引是这样的

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "a",
                "_type": "a1",
                "_id": "my-index1",
                "_score": 1.0,
                "_source": {
                    "id": "my-index1",
                    "name": "firstIndex",
                    "metals": [
                        {
                            "id": "123",
                            "name": "Ronni",   
                        },
                         {
                            "id": "124",
                            "name": "Ross",   
                        }
                    ]
                }
            }
        ]
    }
}

如何在“Id”-124 的帮助下用“Monika”更新“Ross”?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    假设您的metals 属于nested datatype(而不仅仅是一个简单的嵌套数组),您可以首先使用nested 术语查询来限制要更新的文档,然后使用循环的script执行您的更改:

    POST my-index1/_update_by_query
    {
      "query": {
        "nested": {
          "path": "metals",
          "query": {
            "term": {
              "metals.id": "124"
            }
          }
        }
      },
      "script": {
        "source": """
          for (def i = 0; i < ctx._source.metals.length; i++) {
            def group = ctx._source.metals[i];
            if (group['id'] == params['old_metal_id']) {
              ctx._source.metals[i]['name'] = params['new_metal_name']
            }
    
          }
        """,
        "params": {
          "old_metal_id": "124",
          "new_metal_name": "Monika"
        }
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多