简体   繁体   English

如何擦除ElasticSearch索引?

[英]How to erase ElasticSearch index?

My unit/integration tests includes tests for search functionality. 我的单元/集成测试包括搜索功能的测试。

My idea is to have empty search index before each test. 我的想法是在每次测试之前都有空搜索索引。 So, I'm trying to remove all elements in index on setup method (it's Groovy code): 所以,我试图删除setup方法索引中的所有元素(它是Groovy代码):

Client client = searchConnection.client

SearchResponse response = client.prepareSearch("item")
    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
    .setQuery(termQuery('name', 'test')) //tried also matchAllQuery()
    .setFrom(0).setSize(100).setExplain(false).execute().actionGet()

List<String> ids = response.hits.hits.collect {
    return it.id
}
client.close()

client = searchConnection.client

ids.each {
    DeleteResponse delete = client.prepareDelete("item", "item", it)
        .setOperationThreaded(false)
        .execute().actionGet()
}

client.close()

Seems that it's processing all deletions asynchronously, so I've added Thread.sleep(5000) after it. 似乎它正在异步处理所有删除,所以我在它之后添加了Thread.sleep(5000) As you see i'm trying to open/close connection few times - it doesn't help there. 如你所见,我试图打开/关闭连接几次 - 它没有帮助。

The problem that sometimes it requires more time, sometimes it needs more that 5 seconds to delete, sometimes it can't find just added data (from previous test), etc, etc. And most annoying that integration tests becomes unstable. 有时需要更多时间的问题,有时需要更多的时间来删除,有时它无法找到刚刚添加的数据(来自之前的测试)等等。最令人讨厌的是集成测试变得不稳定。 Putting Thread.sleep() everywhere where it's possible looks as not so good solution. Thread.sleep()放在任何可能的地方看起来都不是那么好的解决方案。

It there any way to commit last changes, or make an lock until all data will be written? 有没有办法提交最后的更改,或者锁定直到所有数据都被写入?

Found solution: 找到解决方案

IndicesAdminClient adminClient = searchConnection.client.admin().indices();
String indexName = "location";
DeleteIndexResponse delete = adminClient.delete(new DeleteIndexRequest(indexName)).actionGet()
if (!delete.isAcknowledged()) {
    log.error("Index {} wasn't deleted", indexName);
}

and

client.admin().indices().flush(new FlushRequest('location')).actionGet();

after putting new data into index. 将新数据放入索引后。

First of all you don't have to clear all data by issuing a delete on each doc id. 首先,您不必通过对每个文档ID发出删除来清除所有数据。 You can just delete all data with delete by query matching all documents http://www.elasticsearch.org/guide/reference/api/delete-by-query.html Having that said I don't recommend that either, because it's not recommended to do this often on large doc collections (see docs). 您可以通过查询匹配所有文档删除所有数据删除所有文件http://www.elasticsearch.org/guide/reference/api/delete-by-query.html说完之后我也不建议这样做,因为它不是建议经常在大型文档集上执行此操作(请参阅docs)。

What you really want to do is delete the whole index (it's fast) http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html , recreate it, put in data and this is important refresh the index to "commit" the changes and make them visible. 你真正想做的是删除整个索引(它很快) http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html ,重新创建它,输入数据, 这是重要的是刷新索引以“提交”更改并使其可见。 http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html

I do this in my tests and never had a problem. 我在测试中这样做,从来没有遇到过问题。

  1. it is not the async call (you can add a listener and avoid actionGet to get the async call) 它不是异步调用(您可以添加侦听器并避免actionGet来获取异步调用)
  2. delete all items via: 删除所有项目:

     client.prepareDeleteByQuery(indexName). setQuery(QueryBuilders.matchAllQuery()). setTypes(indexType). execute().actionGet(); 
  3. refresh your index to see the changes (only required in unit tests) 刷新索引以查看更改(仅在单元测试中需要)

My idea is to have empty search index before each test 我的想法是在每次测试之前都有空搜索索引

So create a new index at the start of the test, don't re-use the old one. 因此,在测试开始时创建一个新索引,不要重复使用旧索引。 You're guaranteed an empty one then. 那你就保证空了。 In the tests's teardown, you can then delete the test index. 在测试的拆解中,您可以删除测试索引。

It there any way to commit last changes, or make an lock until all data will be written? 有没有办法提交最后的更改,或者锁定直到所有数据都被写入?

No, ElasticSearch has no transactions or locking. 不,ElasticSearch没有交易或锁定。

If you don't want to create new index each time, then try adding a loop which checks to see if the index is empty, then waits and tries again, until it is. 如果您不想每次都创建新索引,那么尝试添加一个循环来检查索引是否为空,然后等待并再次尝试,直到它为止。

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

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