Skip to content

Commit f9827bb

Browse files
committed
update index, update and delete client documentations
1 parent f3afbb2 commit f9827bb

File tree

11 files changed

+381
-225
lines changed

11 files changed

+381
-225
lines changed

core/src/main/scala/app/softnetwork/elastic/client/DeleteApi.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ trait DeleteApi extends ElasticClientHelpers { _: SettingsApi =>
6565
executeDelete(index, id, waitEnabled) match {
6666
case success @ ElasticSuccess(true) =>
6767
logger.info(s"✅ Successfully deleted document with id '$id' from index '$index'")
68+
if (waitEnabled)
69+
logger.info(s"Waiting for a refresh of index $index to happen")
6870
success
6971
case success @ ElasticSuccess(_) =>
7072
logger.info(s"✅ Document with id '$id' not found in index '$index'")
@@ -115,6 +117,8 @@ trait DeleteApi extends ElasticClientHelpers { _: SettingsApi =>
115117
s match {
116118
case success @ ElasticSuccess(true) =>
117119
logger.info(s"✅ Successfully deleted document with id '$id' from index '$index'")
120+
if (waitEnabled)
121+
logger.info(s"Waiting for a refresh of index $index to happen")
118122
promise.success(success)
119123
case success @ ElasticSuccess(_) =>
120124
logger.warn(s"❌ Document with id '$id' in index '$index' not deleted")

core/src/main/scala/app/softnetwork/elastic/client/ElasticClientDelegator.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import org.slf4j.{Logger, LoggerFactory}
2929

3030
import scala.concurrent.{ExecutionContext, Future}
3131
import scala.language.implicitConversions
32-
import scala.reflect.{classTag, ClassTag}
32+
import scala.reflect.ClassTag
3333

3434
trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
3535

@@ -505,7 +505,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
505505
id: String,
506506
index: Option[String],
507507
maybeType: Option[String],
508-
wait: Boolean
508+
wait: Boolean = false
509509
)(implicit u: ClassTag[U], formats: Formats): ElasticResult[Boolean] =
510510
delegate.indexAs(entity, id, index, maybeType, wait)
511511

@@ -524,9 +524,9 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
524524
index: JSONResults,
525525
id: JSONResults,
526526
source: JSONResults,
527-
wait: Boolean
527+
wait: Boolean = false
528528
): ElasticResult[Boolean] =
529-
delegate.index(index, id, source, false)
529+
delegate.index(index, id, source, wait)
530530

531531
/** Index an entity in the given index asynchronously.
532532
*
@@ -548,7 +548,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
548548
id: String,
549549
index: Option[String],
550550
maybeType: Option[String],
551-
wait: Boolean
551+
wait: Boolean = false
552552
)(implicit
553553
u: ClassTag[U],
554554
ec: ExecutionContext,
@@ -572,6 +572,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
572572
override def indexAsync(index: String, id: String, source: String, wait: Boolean)(implicit
573573
ec: ExecutionContext
574574
): Future[ElasticResult[Boolean]] = delegate.indexAsync(index, id, source, wait)
575+
575576
override private[client] def executeIndex(
576577
index: String,
577578
id: String,
@@ -610,7 +611,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
610611
id: String,
611612
source: String,
612613
upsert: Boolean,
613-
wait: Boolean
614+
wait: Boolean = false
614615
): ElasticResult[Boolean] =
615616
delegate.update(index, id, source, upsert, wait)
616617

@@ -637,7 +638,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
637638
index: Option[String],
638639
maybeType: Option[String],
639640
upsert: Boolean,
640-
wait: Boolean
641+
wait: Boolean = false
641642
)(implicit u: ClassTag[U], formats: Formats): ElasticResult[Boolean] =
642643
delegate.updateAs(entity, id, index, maybeType, upsert, wait)
643644

@@ -661,7 +662,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
661662
id: String,
662663
source: String,
663664
upsert: Boolean,
664-
wait: Boolean
665+
wait: Boolean = false
665666
)(implicit
666667
ec: ExecutionContext
667668
): Future[ElasticResult[Boolean]] =
@@ -690,7 +691,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
690691
index: Option[String],
691692
maybeType: Option[String],
692693
upsert: Boolean,
693-
wait: Boolean
694+
wait: Boolean = false
694695
)(implicit
695696
u: ClassTag[U],
696697
ec: ExecutionContext,
@@ -729,7 +730,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
729730
* @return
730731
* true if the entity was deleted successfully, false otherwise
731732
*/
732-
override def delete(id: String, index: String, wait: Boolean): ElasticResult[Boolean] =
733+
override def delete(id: String, index: String, wait: Boolean = false): ElasticResult[Boolean] =
733734
delegate.delete(id, index, wait)
734735

735736
/** Delete an entity from the given index asynchronously.
@@ -743,7 +744,7 @@ trait ElasticClientDelegator extends ElasticClientApi with BulkTypes {
743744
* @return
744745
* a Future that completes with true if the entity was deleted successfully, false otherwise
745746
*/
746-
override def deleteAsync(id: String, index: String, wait: Boolean)(implicit
747+
override def deleteAsync(id: String, index: String, wait: Boolean = false)(implicit
747748
ec: ExecutionContext
748749
): Future[ElasticResult[Boolean]] =
749750
delegate.deleteAsync(id, index, wait)

core/src/main/scala/app/softnetwork/elastic/client/IndexApi.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ trait IndexApi extends ElasticClientHelpers { _: SettingsApi with SerializationA
106106
executeIndex(index, id, source, waitEnabled) match {
107107
case success @ ElasticSuccess(true) =>
108108
logger.info(s"✅ Document with id '$id' indexed successfully in index '$index'")
109+
if (waitEnabled)
110+
logger.info(s"Waiting for a refresh of index $index to happen")
109111
success
110112
case success @ ElasticSuccess(_) =>
111113
logger.info(s"✅ Document with id '$id' not indexed in index '$index'")
@@ -197,6 +199,8 @@ trait IndexApi extends ElasticClientHelpers { _: SettingsApi with SerializationA
197199
result match {
198200
case success @ ElasticSuccess(true) =>
199201
logger.info(s"✅ Successfully indexed document with id '$id' in index '$index'")
202+
if (waitEnabled)
203+
logger.info(s"Waiting for a refresh of index $index to happen")
200204
promise.success(success)
201205
case success @ ElasticSuccess(_) =>
202206
logger.info(s"✅ Document with id '$id' not indexed in index '$index'")

core/src/main/scala/app/softnetwork/elastic/client/UpdateApi.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ trait UpdateApi extends ElasticClientHelpers { _: SettingsApi with Serialization
9090
executeUpdate(index, id, source, upsert, waitEnabled) match {
9191
case success @ ElasticSuccess(true) =>
9292
logger.info(s"✅ Successfully updated document with id '$id' in index '$index'")
93+
if (waitEnabled)
94+
logger.info(s"Waiting for a refresh of index $index to happen")
9395
success
9496
case ElasticSuccess(false) =>
9597
val error = s"Document with id '$id' in index '$index' not updated"
@@ -208,6 +210,8 @@ trait UpdateApi extends ElasticClientHelpers { _: SettingsApi with Serialization
208210
s match {
209211
case success @ ElasticSuccess(true) =>
210212
logger.info(s"✅ Successfully updated document with id '$id' in index '$index'")
213+
if (waitEnabled)
214+
logger.info(s"Waiting for a refresh of index $index to happen")
211215
promise.success(success)
212216
case success @ ElasticSuccess(_) =>
213217
logger.warn(s"❌ Document with id '$id' in index '$index' not updated")

documentation/client/delete.md

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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
206207
def 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
415418
private[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
424428
private[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
449459
private[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
458469
private[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

Comments
 (0)