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

时间:2018-03-05 12:40:30

标签: elasticsearch kibana

{
   "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"
    }

此更新应该在一个命令中。请指导我们执行此操作。

2 个答案:

答案 0 :(得分:0)

看看Update By Query API。您可以准备a query to match all documents并使用scripting添加所需的媒体资源。

示例:

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

答案 1 :(得分:0)

尝试

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