我尝试过下面的代码,它工作正常,但它使用 TransportClient 删除所有文档。
DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = DeleteByQueryAction.INSTANCE.newRequestBuilder(transportClient)
.filter(QueryBuilders.matchAllQuery())
.source(indexName);
BulkByScrollResponse response = deleteByQueryRequestBuilder
.filter(QueryBuilders.matchAllQuery()).get()
我正在使用 elasticsearch 6.1.4。 使用RestHighLevelClient从索引中删除所有文档的方法是什么。
我使用的是elasticsearch 6.5.4。在此代码片段中,我使用客户端 RestHighLevelClient。
DeleteByQueryRequest request = new DeleteByQueryRequest(indexName);
request.setQuery(QueryBuilders.matchAllQuery());
BulkByScrollResponse response = client.deleteByQuery(request, RequestOptions.DEFAULT);
类
RestHighLevelClient
有需要请求的方法 deleteByQuery。在此请求中您可以定义您需要的过滤器。
使用
BulkRequestpublic void deleteIndexes() {
List<String> websitePageList = new ArrayList<>();
websitePageList.add("3123123123"); //Test Data
websitePageList.add("8756785678");
websitePageList.add("9673563456");
if (websitePageList != null && !websitePageList.isEmpty()) {
BulkRequest request = new BulkRequest();
websitePageList.forEach(pageId -> request.add(new DeleteRequest("content", pageId))); //Where content is index & pageId is Document Id
ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkItemResponses) {
long deleted = bulkItemResponses.getItems().length;
LOG.info("Deleted documents : " + deleted);
}
@Override
public void onFailure(Exception e) {
LOG.error("Index has not been correctly removed");
}
};
client.bulkAsync(request, RequestOptions.DEFAULT, listener);
}
}
esClient.deleteByQuery(new DeleteByQueryRequest.Builder()
.index("my-index")
.query(
new Query.Builder()
.matchAll(QueryBuilders
.matchAll()
.build())
.build())
.build());