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?
Yes, deleting the index, deletes all the data in that index.
2. Delete all documents from the index. We can delete all the documents from the index using _delete_by_query.
If you no longer need an index, you can use the delete index API operation to delete it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With