diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index b205c88a5..58221d4f7 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -20,14 +20,16 @@ object ElasticRequest { doc: A, routing: Option[Routing] = None ): ElasticRequest[Unit] = - Create(index, Some(id), Document.from(doc), routing).map(_ => ()) + Create(index = index, id = Some(id), document = Document.from(doc), routing = routing).map(_ => ()) def create[A: Schema]( index: IndexName, doc: A, routing: Option[Routing] ): ElasticRequest[Option[DocumentId]] = - Create(index, None, Document.from(doc), routing) + Create(index = index, id = None, document = Document.from(doc), routing = routing) + + def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] = Exists(index = index, id = id) def getById[A: Schema]( index: IndexName, @@ -45,7 +47,7 @@ object ElasticRequest { doc: A, routing: Option[Routing] = None ): ElasticRequest[Unit] = - CreateOrUpdate(index, id, Document.from(doc), routing) + CreateOrUpdate(index = index, id = id, document = Document.from(doc), routing = routing) def createIndex( name: IndexName, @@ -66,6 +68,12 @@ object ElasticRequest { routing: Option[Routing] = None ) extends ElasticRequest[Unit] + private[elasticsearch] final case class Exists( + index: IndexName, + id: DocumentId, + routing: Option[Routing] = None + ) extends ElasticRequest[Boolean] + private[elasticsearch] final case class GetById( index: IndexName, id: DocumentId, diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index e206d62e6..a8db81fd3 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -3,6 +3,7 @@ package zio.elasticsearch import sttp.client3.ziojson._ import sttp.client3.{SttpBackend, UriContext, basicRequest => request} import sttp.model.MediaType.ApplicationJson +import sttp.model.StatusCode.Ok import sttp.model.Uri import zio.Task import zio.elasticsearch.ElasticRequest._ @@ -17,8 +18,9 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC override def execute[A](request: ElasticRequest[A]): Task[A] = request match { case r: Create => executeCreate(r) - case r: CreateOrUpdate => executeCreateOrUpdate(r) case r: CreateIndex => executeCreateIndex(r) + case r: CreateOrUpdate => executeCreateOrUpdate(r) + case r: Exists => executeExists(r) case r: GetById => executeGetById(r) case map @ Map(_, _) => execute(map.request).map(map.mapper) } @@ -52,6 +54,14 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC .map(_.flatMap(body => Some(DocumentId(body.id)))) } + private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] = + request + .put(uri"$basePath/${createIndex.name}") + .contentType(ApplicationJson) + .body(createIndex.definition.getOrElse("")) + .send(client) + .unit + private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = { val u = uri"$basePath/${r.index}/$Doc/${r.id}" .withParam("routing", r.routing.map(_.value)) @@ -64,13 +74,14 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC .unit } - private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] = + private def executeExists(r: Exists): Task[Boolean] = { + val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(_.value)) request - .put(uri"$basePath/${createIndex.name}") - .contentType(ApplicationJson) - .body(createIndex.definition.getOrElse("")) + .head(uri) .send(client) - .unit + .map(_.code.equals(Ok)) + } + } private[elasticsearch] object HttpElasticExecutor {