使用新的键值对更新弹性搜索数据

Update elastic search data with new key-value pair

{
   "took": 0,
   "timed_out": false,
   "_shards": {
   "total": 5,
   "successful": 5,
   "skipped": 0,
   "failed": 0
},
"hits": {
"total": 25,
"max_score": 1,
"hits": [
  {
    "_index": "surtest1",
    "_type": "catalog",
    "_id": "prod_9876561740",
    "_score": 1,
    "_source": {
      "id": "01",
      "type": "product" 
    }
  },
  {
    "_index": "surtest1",
    "_type": "catalog",
    "_id": "prod_9876543375",
    "_score": 1,
    "_source": {
      "id": "02",
      "type": "product" 
    }
  }
]
}
}

这是弹性搜索中搜索的示例 json 响应。 我们需要在所有 json 对象中再添加一个键值对 ("spec":"4gb"),例如

"_source": {
      "id": "01",
      "type": "product" ,
      "spec": "4gb"
    },
"_source": {
      "id": "02",
      "type": "product" ,
      "spec": "4gb"
    }

这个更新应该在一个单一的 command.Please 指导我们执行这个操作。

查看 Update By Query API. You are able to prepare a query to match all documents and use scripting 添加您想要的 属性。

示例:

POST twitter/_update_by_query
{
  "script": {
    "source": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

尝试

POST /surtest1/_update_by_query?refresh
{
"script": {
    "source": "ctx._source['spec']='4gb'"
 }
}