Skip to content

Commit

Permalink
(api): Support exists (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 authored Nov 30, 2022
1 parent a4e2478 commit 58c280d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down

0 comments on commit 58c280d

Please sign in to comment.