Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(api): Support exists #11

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct implementation I would say. Even if the document does not exist, I think the response will be 200 OK.

However, you should use HEAD instead. Check more info here.

Copy link
Collaborator Author

@dbulaja98 dbulaja98 Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response will be 404 Not Found if the document is not found.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, it suppose to be HEAD, I will change it, I don't know why it is not already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a response is 500? We should create a ticket to handle those kinds of errors.

.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