• -------------------------------------------------------------
  • ====================================

ElasticSearh更新nested字段(Array数组)。怎么根据查询条件(query)复制一个(index)到新的Index how to update by query a nested fields data for elasticsearch

elasticsearch dewbay 6年前 (2019-04-12) 4792次浏览 已收录 0个评论 扫描二维码
ElasticSearh更新nested字段(Array数组)。怎么根据查询条件(query)复制一个(index)到新的Index how to update by query a nested fields data for elasticsearch
GET usernested/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "tags.brandid": "93a8296c-5b64-49ea-bd92-b19192def2e9"
              }
            },
            {
              "term": {
                "tags.site": "163"
              }
            }
          ]
        }
      }
    }
  }
}


//按照条件新建一个 index 作为测试数据使用
POST _reindex
{
  "source": {
    "index": "usernested",
    "query": {
      "nested": {
        "path": "tags",
        "query": {
          "bool": {
            "must": [
              {
                "term": {
                  "tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"
                }
              },
              {
                "term": {
                  "tags.site": "163"
                }
              }
            ]
          }
        }
      }
    }
  },
  "dest": {
    "index": "new_usernested"
  }
}


//查询
GET new_usernested/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"
              }
            },
            {
              "term": {
                "tags.site": "163"
              }
            }
          ]
        }
      }
    }
  }
}

//根据条件更新一个 nested 的文档
GET usernested/_update_by_query
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"
              }
            },
            {
              "term": {
                "tags.site": "163"
              }
            }
          ]
        }
      }
    }
  },
  "script": {
    "inline": "for(e in ctx._source.tags){e.brand = 'test2';}" //更新 nested 字段
//"inline":"ctx._source.userid = 'testid'" //更新普通字段 } }

喜欢 (0)
[]
分享 (0)
关于作者:
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址