From 3fe763904fd089d701f7ff79fead27b1bfd926d7 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Thu, 31 Oct 2019 21:14:05 -0300 Subject: [PATCH 1/2] adding deleteAllDocuments method to facilite operation --- .../ElasticRestClient.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/ElasticRestClient.java b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/ElasticRestClient.java index 1f2b88a..48d2c9a 100644 --- a/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/ElasticRestClient.java +++ b/core/src/main/java/pl/allegro/tech/embeddedelasticsearch/ElasticRestClient.java @@ -138,10 +138,10 @@ void deleteIndex(String indexName) { void bulkIndex(Collection indexRequests) { String bulkRequestBody = indexRequests.stream() .flatMap(request -> - Stream.of( - indexMetadataJson(request.getIndexName(), request.getIndexType(), request.getId(), request.getRouting()), - request.getJson() - ) + Stream.of( + indexMetadataJson(request.getIndexName(), request.getIndexType(), request.getId(), request.getRouting()), + request.getJson() + ) ) .map((jsonNodes) -> jsonNodes.replace('\n', ' ').replace('\r', ' ')) .collect(joining("\n")) + "\n"; @@ -221,6 +221,16 @@ List fetchAllDocuments(String routing, String... indices) { } } + void deleteAllDocuments(String... indices) { + List documents = fetchAllDocuments(indices); + documents.forEach(this::deleteIndex); + } + + void deleteAllDocuments(String routing, String... indices) { + List documents = fetchAllDocuments(routing, indices); + documents.forEach(this::deleteIndex); + } + private Stream searchForDocuments(Optional indexMaybe) { return searchForDocuments(indexMaybe, Optional.empty()); } From 0ccf24cb0973f836dcdad69c2047945c2b0b6a92 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Thu, 31 Oct 2019 21:19:21 -0300 Subject: [PATCH 2/2] update Readme.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e0ce191..4ef2893 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Availabe `JavaHomeOption` options | `stop()` | stops your Elasticsearch instance and removes all data | | `index` | index your document, comes with variants that take only document, or document and it's id | | `deleteIndex(String indexName)`, `deleteIndices()` | deletes index with name specified during EmbeddedElastic creation | +| `deleteAllDocuments(String indexName)`, `deleteAllDocuments(String routing, String indexName)` | deletes all documents with name specified index name or specified routing and index name during EmbeddedElastic creation | | `createIndex(String indexName)`, `createIndices()` | creates index with name specified during EmbeddedElastic creation; note that this index is created during EmbeddedElastic startup, you will need this method only if you deleted your index using `deleteIndex` method | | `recreateIndex(String indexName)`, `recreateIndices()` | combination of `deleteIndex` and `createIndex` | | `refreshIndices()` | refresh index; useful when you make changes in different thread, and want to check results instantly in tests |