Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. 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.

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

like image 442
Igor Artamonov Avatar asked Nov 05 '11 09:11

Igor Artamonov


People also ask

Does deleting index in Elasticsearch delete data?

Yes, deleting the index, deletes all the data in that index.

How do I delete all index Elasticsearch files?

2. Delete all documents from the index. We can delete all the documents from the index using _delete_by_query.

How do I remove indices in Opensearch?

If you no longer need an index, you can use the delete index API operation to delete it.


1 Answers

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.

like image 115
Igor Artamonov Avatar answered Oct 23 '22 19:10

Igor Artamonov