简体   繁体   English

Elasticsearch 1.5 curl删除而无需queryapi

[英]Elasticsearch 1.5 curl delete without queryapi

On my elasticsearch 1.4 I used to delete documents using the DeleteByQuery API like this : 在我的elasticsearch 1.4上,我曾经使用DeleteByQuery API删除文档,如下所示:

curl -XDELETE http://my_elasticsearch:9200/_all/_query?q=some_field:some_value

This wasn't perfect (because of regular OutOfMemoryError) but this works enough for my needs (at this time). 这不是完美的(由于常规的OutOfMemoryError),但这足以满足我的需求(此时)。

But now I use the new elasticsearch 1.5 and in the documentation I have read that : 但是现在我使用了新的elasticsearch 1.5,并且在文档中我已经读到:

Deprecated in 1.5.0. 在1.5.0中已弃用。

"Delete by Query will be removed in 2.0: it is problematic since it silently forces a refresh which can quickly cause OutOfMemoryError during concurrent indexing, and can also cause primary and replica to become inconsistent. Instead, use the scroll/scan API to find all matching ids and then issue a bulk request to delete them.. “按查询删除将在2.0版中删除:这是有问题的,因为它无提示地强制刷新,刷新可能会在并发索引期间迅速导致OutOfMemoryError,还会导致主数据库和副本数据库变得不一致。相反,请使用滚动/扫描API查找所有匹配的ID,然后发出批量请求将其删除。

So I would like to do the same using scroll/scan API. 因此,我想使用滚动/扫描API进行相同的操作。 But how to delete using this? 但是如何使用这个删除呢? I don't understand how. 我不明白 The documentation API and documentation Java API doesn't seems complete for me (missing example of deleting). 对我来说, 文档API文档Java API似乎并不完整(缺少删除示例)。

PS: I'm looking for understand with java or curl (no matter for me in final I need the both). PS:我正在寻找Java或curl的理解(无论对我而言,最终我都需要这两者)。

I ran into this issue as well and could not find a good code example. 我也遇到了这个问题,找不到合适的代码示例。 I'll show you what I came up with. 我会告诉你我的想法。 I'm not sure if this is the best way to do it, so please feel free to comment about how this could be refined. 我不确定这是否是最好的方法,所以请随时评论如何改进。 Note that I set the size of the results for the query to Integer.MAX_VALUE so that the query will return all (or as many as possible) of the results that need to be deleted. 请注意,我将查询结果的大小设置为Integer.MAX_VALUE,以便查询将返回所有(或尽可能多)需要删除的结果。

  1. Run query to get all IDs to be deleted 运行查询以获取所有要删除的ID
  2. Add delete requests for all IDs to a bulk request 将所有ID的删除请求添加到批量请求中
  3. Run bulk request 运行批量请求
  4. Re-run query to see if any more records need to be deleted 重新运行查询以查看是否需要删除更多记录
  5. Repeat if necessary 必要时重复

     private void deleteAllByQuery(final String index, final String type, final QueryBuilder query) { SearchResponse response = elasticSearchClient.prepareSearch(index) .setTypes(type) .setQuery(query) .setSize(Integer.MAX_VALUE) .execute().actionGet(); SearchHit[] searchHits = response.getHits().getHits(); while (searchHits.length > 0) { LOGGER.debug("Need to delete " + searchHits.length + " records"); // Create bulk request final BulkRequestBuilder bulkRequest = elasticSearchClient.prepareBulk().setRefresh(true); // Add search results to bulk request for (final SearchHit searchHit : searchHits) { final DeleteRequest deleteRequest = new DeleteRequest(index, type, searchHit.getId()); bulkRequest.add(deleteRequest); } // Run bulk request final BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { LOGGER.error(bulkResponse.buildFailureMessage()); } // After deleting, we should check for more records response = elasticSearchClient.prepareSearch(index) .setTypes(type) .setQuery(query) .setSize(Integer.MAX_VALUE) .execute().actionGet(); searchHits = response.getHits().getHits(); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM