elasticsearch 5.x Delete By Query API(根据条件删除)
2021-07-13 00:08
标签:tps query else lte enc new 根据 war .exe 之前在 2.X版本里 这个Delete By Query功能被去掉了 因为官方认为会引发一些错误 如需使用 需要自己安装插件。 需要使用时 那么在5.x版本中 这个api又被官方重新加回自带功能。新特性里是这么说的。 我查看了官方手册后发现使用也不一样了 见下图 官方地址:https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-delete-by-query.html 即替换以前的 _query 为_delete_by_query 便可达到目的。 通过header插件删除 Java代码实现: 另一种实现方式: 文章参考地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.2/java-docs-delete-by-query.html elasticsearch 5.x Delete By Query API(根据条件删除) 标签:tps query else lte enc new 根据 war .exe 原文地址:https://www.cnblogs.com/a-du/p/9583434.htmlbin/plugin install delete-by-query
DELETE /索引名/需要清空的type/_query
{
"query": {
"match_all": {}
}
}
Delete-by-query 和 Update-by-query 重新回到 core ,以前是插件,现在可以直接使用了,
也是构建在 Reindex 机制之上。
(es1.x版本是直接支持,在es2.x中提取为插件,5.x继续回归直接支持)
使用head插件删除指定数据
http://serverhost:9200/index/type/_delete_by_query post
{
"query": {
"match": {
属性字段: 要删除的属性值
}
}
}
String index = "wareic";
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termQuery("groupID", "42023"));
BulkIndexByScrollResponse response =
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.filter(queryBuilder)
.source(index)
.get();
long deleted = response.getDeleted();
System.out.println(deleted);
String index = "wareic";
String type = "product";
BulkRequestBuilder bulkRequest = client.prepareBulk();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
//分页
searchRequestBuilder.setFrom(0).setSize(1000);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termQuery("groupID", "3445"));
searchRequestBuilder.setQuery(queryBuilder);
SearchResponse response = searchRequestBuilder.execute().get();
for(SearchHit hit : response.getHits()){
String id = hit.getId();
bulkRequest.add(client.prepareDelete(index, type, id).request());
}
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
for(BulkItemResponse item : bulkResponse.getItems()){
System.out.println(item.getFailureMessage());
}
} else {
System.out.println("delete ok");
}
文章标题:elasticsearch 5.x Delete By Query API(根据条件删除)
文章链接:http://soscw.com/index.php/essay/104398.html