elasticsearch---批量修改,批量更新某個欄位
阿新 • • 發佈:2018-11-19
1.源生API
在這裡沒有用官方提供的bulk
API,而是用的另外一種方式。
POST /infomations/infomations/_update_by_query
JSON請求格式
{ "query": { "match": { "status": "UP_SHELF" } }, "script": { "inline": "ctx._source['status'] = 'DOWN_SHELF'" } }
POST請求/索引/文件名/_update_by_query 主要看一下下面的script ctx._source[欄位名]= “值”;ctx._source[欄位名] = “值”;
多個的話就用分號隔開。
2.JAVA API操作
//叢集模式,獲取連結
Client client = elasticsearchTemplate.getClient();
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client); String name = "修改數值"; updateByQuery.source("索引")
//查詢要修改的結果集 .filter(QueryBuilders.termQuery("field", 412))
//修改操作 .script(new Script( "ctx._source['field']='"+ name+"';ctx._source['field']='"+name+"'")); //響應結果集
BulkByScrollResponse response = updateByQuery.get(); long updated = response.getUpdated();