如何清除ElasticSearch索引? [英] How to erase ElasticSearch index?

查看:1074
本文介绍了如何清除ElasticSearch索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单元/集成测试包括搜索功能的测试。

My unit/integration tests includes tests for search functionality.

我的想法是在每次测试之前都有空的搜索索引。所以,我试图删除 setup 方法(它是Groovy代码)中的索引中的所有元素:

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):

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()

似乎正在处理所有删除异步,所以我已经添加了 Thread.sleep(5000)。正如你所看到的,我试图打开/关闭连接几次 - 这并没有帮助。

Seems that it's processing all deletions asynchronously, so I've added Thread.sleep(5000) after it. As you see i'm trying to open/close connection few times - it doesn't help there.

这个问题有时候需要更多的时间,有时它需要更多那5秒删除,有时候找不到刚刚添加的数据(从以前的测试)等等。最令人讨厌的集成测试变得不稳定。将 Thread.sleep()在可能的地方看起来不是很好的解决方案。

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.

有什么办法提交最后更改,或进行锁定,直到所有数据都将被写入

It there any way to commit last changes, or make an lock until all data will be written?

推荐答案

找到的解决方案:

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);
}

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

这篇关于如何清除ElasticSearch索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆