From 1366add2874e85c4c45f8f39827dc8d2d3deed69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20Milinkovi=C4=87?= <81649656+mvelimir@users.noreply.github.com> Date: Fri, 2 Dec 2022 10:24:20 +0100 Subject: [PATCH] (api): Support delete by ID (#19) --- .../zio/elasticsearch/ElasticDeleteResponse.scala | 11 +++++++++++ .../main/scala/zio/elasticsearch/ElasticRequest.scala | 10 ++++++++++ .../scala/zio/elasticsearch/HttpElasticExecutor.scala | 11 +++++++++++ 3 files changed, 32 insertions(+) create mode 100644 modules/library/src/main/scala/zio/elasticsearch/ElasticDeleteResponse.scala diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticDeleteResponse.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticDeleteResponse.scala new file mode 100644 index 000000000..4db0cc5ac --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticDeleteResponse.scala @@ -0,0 +1,11 @@ +package zio.elasticsearch + +import zio.json.{DeriveJsonDecoder, JsonDecoder} + +private[elasticsearch] final case class ElasticDeleteResponse( + result: String +) + +private[elasticsearch] object ElasticDeleteResponse { + implicit val decoder: JsonDecoder[ElasticDeleteResponse] = DeriveJsonDecoder.gen[ElasticDeleteResponse] +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 88f7b62e7..bd863eb70 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -17,6 +17,7 @@ sealed trait ElasticRequest[+A] { self => case Map(request, mapper) => Map(request.routing(value), mapper) case r: Create => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]] case r: CreateOrUpdate => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]] + case r: DeleteById => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]] case r: Exists => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]] case r: GetById => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]] case _ => self @@ -31,6 +32,9 @@ object ElasticRequest { def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] = Create(index, None, Document.from(doc)) + def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] = + DeleteById(index, id).map(_.toRight(DocumentNotFound)) + def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] = Exists(index, id) @@ -68,6 +72,12 @@ object ElasticRequest { routing: Option[Routing] = None ) extends ElasticRequest[Unit] + private[elasticsearch] final case class DeleteById( + index: IndexName, + id: DocumentId, + routing: Option[Routing] = None + ) extends ElasticRequest[Option[Unit]] + private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Unit] private[elasticsearch] final case class Exists( diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 788223b83..affd00577 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -20,6 +20,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC case r: Create => executeCreate(r) case r: CreateIndex => executeCreateIndex(r) case r: CreateOrUpdate => executeCreateOrUpdate(r) + case r: DeleteById => executeDeleteById(r) case r: DeleteIndex => executeDeleteIndex(r) case r: Exists => executeExists(r) case r: GetById => executeGetById(r) @@ -75,6 +76,16 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeDeleteIndex(r: DeleteIndex): Task[Unit] = request.delete(uri"$basePath/${r.name}").send(client).unit + private def executeDeleteById(deleteById: DeleteById): Task[Option[Unit]] = { + val uri = + uri"$basePath/${deleteById.index}/$Doc/${deleteById.id}".withParam("routing", deleteById.routing.map(_.value)) + request + .delete(uri) + .response(asJson[ElasticDeleteResponse]) + .send(client) + .map(_.body.toOption) + .map(_.filter(_.result == "deleted").map(_ => ())) + } } private[elasticsearch] object HttpElasticExecutor {