如何擦除ElasticSearch索引?

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

我的想法是在每次测试之前都有空的搜索索引。 所以,我试图删除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 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) 。 如你所见,我试图打开/关闭连接几次 – 它没有帮助。

有时需要更多时间的问题,有时它需要超过5秒才能删除,有时它无法找到刚刚添加的数据(来自之前的测试)等等。最烦人的是集成测试变得不稳定。 将Thread.sleep()放在可能看起来不太好的解决方案的任何地方。

有没有办法提交最后的更改,或者锁定直到所有数据都被写入?

找到解决方案

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

将新数据放入索引后。

首先,您不必通过对每个文档ID发出删除来清除所有数据。 您可以通过匹配所有文档的查询删除所有数据删除http://www.elasticsearch.org/guide/reference/api/delete-by-query.html这样说我也不建议这样做,因为它不是建议经常在大型文档集上执行此操作(请参阅docs)。

你真正想做的是删除整个索引(它很快) http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html ,重新创建它,输入数据, 这是重要的是刷新索引以“提交”更改并使其可见。 http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html

我在测试中这样做,从来没有遇到过问题。

  1. 它不是异步调用(您可以添加侦听器并避免actionGet来获取异步调用)
  2. 删除所有项目:

     client.prepareDeleteByQuery(indexName). setQuery(QueryBuilders.matchAllQuery()). setTypes(indexType). execute().actionGet(); 
  3. 刷新索引以查看更改(仅在unit testing中需要)

我的想法是在每次测试之前都有空的搜索索引

因此,在测试开始时创建一个新索引,不要重复使用旧索引。 那你就保证空了。 在测试的拆解中,您可以删除测试索引。

有没有办法提交最后的更改,或者锁定直到所有数据都被写入?

不,ElasticSearch没有交易或锁定。

如果您不想每次都创建新索引,那么尝试添加一个循环来检查索引是否为空,然后等待并再次尝试,直到它为止。

您还可以使用curl命令删除弹性搜索索引

 curl -XDELETE 'localhost:9200/index_name'