@@ -8,7 +8,7 @@ The **DeleteApi** trait provides functionality to delete documents from Elastics
88
99** Features:**
1010- Synchronous and asynchronous document deletion
11- - Automatic index refresh after deletion
11+ - Wait for a refresh to happen after deletion to happen if required
1212- Index name validation
1313- Comprehensive error handling and logging
1414- Safe deletion with existence checking
@@ -24,7 +24,7 @@ The **DeleteApi** trait provides functionality to delete documents from Elastics
2424- Deletes a document by its ID from a specific index
2525- Returns ` true ` if document was deleted
2626- Returns ` false ` if document doesn't exist (not an error)
27- - Automatically refreshes index after successful deletion
27+ - Wait for a refresh to happen after deletion if required
2828
2929** Idempotency:**
3030- Delete operations are idempotent
@@ -42,12 +42,13 @@ Deletes a document from an Elasticsearch index by ID.
4242** Signature:**
4343
4444``` scala
45- def delete (id : String , index : String ): ElasticResult [Boolean ]
45+ def delete (id : String , index : String , wait : Boolean ): ElasticResult [Boolean ]
4646```
4747
4848** Parameters:**
4949- ` id ` - The document ID to delete
5050- ` index ` - The index name containing the document
51+ - ` wait ` - If ` true ` , waits for a refresh to happen after deletion (default is ` false ` )
5152
5253** Returns:**
5354- ` ElasticSuccess[Boolean] ` with ` true ` if document was deleted
@@ -58,8 +59,8 @@ def delete(id: String, index: String): ElasticResult[Boolean]
5859- Index name format validation
5960
6061** Behavior:**
61- - Automatically refreshes index after successful deletion
6262- Logs success/failure with appropriate emoji indicators
63+ - Waits for a refresh to happen after successful deletion (disabled by default)
6364- Returns success even if document doesn't exist (idempotent)
6465
6566** Examples:**
@@ -205,13 +206,15 @@ Asynchronously deletes a document from an Elasticsearch index.
205206``` scala
206207def deleteAsync (
207208 id : String ,
208- index : String
209+ index : String ,
210+ wait : Boolean
209211)(implicit ec : ExecutionContext ): Future [ElasticResult [Boolean ]]
210212```
211213
212214** Parameters:**
213215- ` id ` - The document ID to delete
214216- ` index ` - The index name containing the document
217+ - ` wait ` - If ` true ` , waits for a refresh to happen after deletion (default is ` false ` )
215218- ` ec ` - Implicit ExecutionContext for async execution
216219
217220** Returns:**
@@ -414,7 +417,8 @@ val result = Await.result(
414417``` scala
415418private [client] def executeDelete (
416419 index : String ,
417- id : String
420+ id : String ,
421+ wait : boolean
418422): ElasticResult [Boolean ]
419423```
420424
@@ -423,20 +427,26 @@ private[client] def executeDelete(
423427``` scala
424428private [client] def executeDelete (
425429 index : String ,
426- id : String
430+ id : String ,
431+ wait : Boolean
427432): ElasticResult [Boolean ] = {
428- executeRestAction[DeleteResponse , Boolean ](
433+ executeRestAction[DeleteRequest , DeleteResponse , Boolean ](
429434 operation = " delete" ,
430- index = Some (index)
435+ index = Some (index),
436+ retryable = false
431437 )(
432- action = {
433- val request = new DeleteRequest (index, id)
434- client.delete(request, RequestOptions . DEFAULT )
435- }
438+ request = new DeleteRequest (index, id)
439+ .setRefreshPolicy(
440+ if (wait) WriteRequest . RefreshPolicy . WAIT_UNTIL else WriteRequest . RefreshPolicy . NONE
441+ )
436442 )(
437- transformer = resp => {
438- resp.getResult == DocWriteResponse .Result .DELETED
439- }
443+ executor = req => apply().delete(req, RequestOptions .DEFAULT )
444+ )(
445+ transformer = resp =>
446+ resp.getResult match {
447+ case DocWriteResponse .Result .DELETED | DocWriteResponse .Result .NOOP => true
448+ case _ => false
449+ }
440450 )
441451}
442452```
@@ -448,7 +458,8 @@ private[client] def executeDelete(
448458``` scala
449459private [client] def executeDeleteAsync (
450460 index : String ,
451- id : String
461+ id : String ,
462+ wait : Boolean
452463)(implicit ec : ExecutionContext ): Future [ElasticResult [Boolean ]]
453464```
454465
@@ -457,33 +468,27 @@ private[client] def executeDeleteAsync(
457468``` scala
458469private [client] def executeDeleteAsync (
459470 index : String ,
460- id : String
471+ id : String ,
472+ wait : Boolean
461473)(implicit ec : ExecutionContext ): Future [ElasticResult [Boolean ]] = {
462- val promise = Promise [ElasticResult [Boolean ]]()
463-
464- val request = new DeleteRequest (index, id)
465-
466- client.deleteAsync(
467- request,
468- RequestOptions .DEFAULT ,
469- new ActionListener [DeleteResponse ] {
470- override def onResponse (response : DeleteResponse ): Unit = {
471- val deleted = response.getResult == DocWriteResponse .Result .DELETED
472- promise.success(ElasticSuccess (deleted))
473- }
474-
475- override def onFailure (e : Exception ): Unit = {
476- promise.success(ElasticFailure (ElasticError (
477- message = s " Async delete failed: ${e.getMessage}" ,
478- operation = Some (" deleteAsync" ),
479- index = Some (index),
480- cause = Some (e)
481- )))
474+ executeAsyncRestAction[DeleteRequest , DeleteResponse , Boolean ](
475+ operation = " deleteAsync" ,
476+ index = Some (index),
477+ retryable = false
478+ )(
479+ request = new DeleteRequest (index, id)
480+ .setRefreshPolicy(
481+ if (wait) WriteRequest .RefreshPolicy .WAIT_UNTIL else WriteRequest .RefreshPolicy .NONE
482+ )
483+ )(
484+ executor = (req, listener) => apply().deleteAsync(req, RequestOptions .DEFAULT , listener)
485+ )(
486+ transformer = resp =>
487+ resp.getResult match {
488+ case DocWriteResponse .Result .DELETED | DocWriteResponse .Result .NOOP => true
489+ case _ => false
482490 }
483- }
484491 )
485-
486- promise.future
487492}
488493```
489494
@@ -866,10 +871,10 @@ def hardDeleteSession(id: String): ElasticResult[Boolean] = {
866871
867872### Delete vs Update (Soft Delete)
868873
869- | Operation | Data Retained | Recoverable | Performance | Use Case |
870- | -----------| ---------------| -------------| -------------| ----------|
871- | ** Hard Delete** | No | No | Fast | Temporary data, logs |
872- | ** Soft Delete** | Yes | Yes | Slower | User data, orders |
874+ | Operation | Data Retained | Recoverable | Performance | Use Case |
875+ | ----------------- | ----------------- | -------------- | -------------- | ------------ ----------|
876+ | ** Hard Delete** | No | No | Fast | Temporary data, logs |
877+ | ** Soft Delete** | Yes | Yes | Slower | User data, orders |
873878
874879``` scala
875880// Hard delete
0 commit comments