我正在使用:
我在ES中有一个索引:
predict_type<-"probability"
现在,该索引中可能有约5万个文档。
对于某些业务逻辑,我需要更新所有满足特定条件的文档:{
"mapping": {
"logi_info_index": {
"properties": {
"area": {
"type": "text"
},
"createdBy": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"createdDate": {
"type": "long"
},
"logiCode": {
"type": "integer"
},
"esId": {
"type": "keyword" -> @Id for ES
},
"geoPoint": {
"type": "geo_point"
},
"isActive": {
"type": "text"
},
"latitude": {
"type": "text"
},
"longitude": {
"type": "text"
},
"storeAddress": {
"type": "text"
},
"storeName": {
"type": "text"
},
"updatedBy": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"updatedDate": {
"type": "long"
}
}
}
}
}
。
示例:
我们有包含isActive=0
的文档。
isActive as 0 or 1
的文档
[=>这可以通过isActive = 1
(deleteAll)<=] DeleteQuery
,所以我们要用isActive = 0
更新其余文档。我有以下问题:
答案 0 :(得分:0)
这在Spring Data Elasticsearch中是不可能的(我假设您使用,因为为此问题加上了标签)。
即使在“普通” Elasticsearch中这也不容易,唯一可能的是将Update By Query API与脚本结合使用(我只是改写了doc示例,没有尝试过):
POST logi_info_index/_update_by_query
{
"script": {
"source": "ctx._source.isActive=1",
"lang": "painless"
},
"query": {
"match_all": {}
}
}
答案 1 :(得分:0)
我是使用ES java客户端和UpdateByQuery做到的
public void updateAll() {
assert elasticsearchOperations != null;
UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE
.newRequestBuilder(elasticsearchOperations.getClient());
updateByQuery.source(((Document) CommonUtility
.getDoc(LogiEntity.class, Document.class)).indexName())
.filter(query("isActive", AppConstants.TEMPORARY_ACTIVE))
.script(script());
BulkByScrollResponse response = updateByQuery.get();
log.debug("process update: {}. Total updated records: {}",
response.getStatus(), response.getUpdated());
}
private Script script() {
String updateCode =
"if (ctx._source.isActive == '" + AppConstants.TEMPORARY_ACTIVE + "') "
+ "{"
+ "ctx._source.isActive = '" + AppConstants.ACTIVE + "';"
+ "}";
return new Script(ScriptType.INLINE, "painless", updateCode,
Collections.emptyMap());
}
private QueryBuilder query(String fieldName, String value) {
return QueryBuilders.matchQuery(fieldName, value);
}