From 1d774415a1a708edc24b55416e754c198e2696bb Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 23 Dec 2022 17:46:15 +0100 Subject: [PATCH 1/7] Support refresh parameter --- .../zio/elasticsearch/ElasticExecutor.scala | 2 +- .../zio/elasticsearch/ElasticRequest.scala | 120 +++++++++++++----- .../elasticsearch/HttpElasticExecutor.scala | 2 +- 3 files changed, 90 insertions(+), 34 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticExecutor.scala index 927a36d25..44f104a3d 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticExecutor.scala @@ -4,7 +4,7 @@ import sttp.client3.SttpBackend import zio.{Task, ZIO, ZLayer} trait ElasticExecutor { - def execute[A](request: ElasticRequest[A]): Task[A] + def execute[A](request: ElasticRequest[A, _]): Task[A] } object ElasticExecutor { diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index adc109a76..245c925f8 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -6,101 +6,157 @@ import zio.elasticsearch.ElasticRequest._ import zio.schema.Schema import zio.{RIO, ZIO} -sealed trait ElasticRequest[+A] { self => +sealed trait ElasticRequestType +object ElasticRequestType { + trait CreateIndexType extends ElasticRequestType + trait CreateType extends ElasticRequestType + trait DeleteByIdType extends ElasticRequestType + trait DeleteIndexType extends ElasticRequestType + trait ExistsType extends ElasticRequestType + trait GetByIdType extends ElasticRequestType + trait GetByQueryType extends ElasticRequestType + trait UpsertType extends ElasticRequestType +} + +sealed trait ElasticRequest[+A, RequestType <: ElasticRequestType] { self => final def execute: RIO[ElasticExecutor, A] = ZIO.serviceWithZIO[ElasticExecutor](_.execute(self)) - final def map[B](f: A => B): ElasticRequest[B] = ElasticRequest.Map(self, f) - - final def routing(value: Routing): ElasticRequest[A] = - self match { - case Map(request, mapper) => Map(request.routing(value), mapper) - case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]] - case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]] - case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]] - case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]] - case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]] - case _ => self - } + final def map[B](f: A => B): ElasticRequest[B, RequestType] = ElasticRequest.Map(self, f) + + final def refresh(implicit addRefresh: AddRefresh[RequestType]): ElasticRequest[A, RequestType] = + addRefresh.addRefresh(self) + + final def routing(value: Routing): ElasticRequest[A, RequestType] = self match { + case Map(request, mapper) => Map(request.routing(value), mapper) + case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case _ => self + } } object ElasticRequest { - def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] = + import zio.elasticsearch.ElasticRequestType._ + + def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, CreateType] = Create(index, Some(id), Document.from(doc)).map(_ => ()) - def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] = + def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], CreateType] = Create(index, None, Document.from(doc)) - def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] = + def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndexType] = CreateIndex(name, definition) - def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] = + def deleteById( + index: IndexName, + id: DocumentId + ): ElasticRequest[Either[DocumentNotFound.type, Unit], DeleteByIdType] = DeleteById(index, id).map(deleted => if (deleted) Right(()) else Left(DocumentNotFound)) - def deleteIndex(name: IndexName): ElasticRequest[Boolean] = + def deleteIndex(name: IndexName): ElasticRequest[Boolean, DeleteIndexType] = DeleteIndex(name) - def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] = + def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean, ExistsType] = Exists(index, id) - def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentRetrievingError, A]] = + def getById[A: Schema]( + index: IndexName, + id: DocumentId + ): ElasticRequest[Either[DocumentRetrievingError, A], GetByIdType] = GetById(index, id).map { case Some(document) => document.decode.left.map(err => DecoderError(err.message)) case None => Left(DocumentNotFound) } - def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse]] = + def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] = GetByQuery(index, query) - def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] = + def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, UpsertType] = CreateOrUpdate(index, id, Document.from(doc)) private[elasticsearch] final case class Create( index: IndexName, id: Option[DocumentId], document: Document, + refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Option[DocumentId]] + ) extends ElasticRequest[Option[DocumentId], CreateType] private[elasticsearch] final case class CreateIndex( name: IndexName, definition: Option[String] - ) extends ElasticRequest[Unit] + ) extends ElasticRequest[Unit, CreateIndexType] private[elasticsearch] final case class CreateOrUpdate( index: IndexName, id: DocumentId, document: Document, + refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Unit] + ) extends ElasticRequest[Unit, UpsertType] private[elasticsearch] final case class DeleteById( index: IndexName, id: DocumentId, + refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Boolean] + ) extends ElasticRequest[Boolean, DeleteByIdType] - private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Boolean] + private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Boolean, DeleteIndexType] private[elasticsearch] final case class Exists( index: IndexName, id: DocumentId, routing: Option[Routing] = None - ) extends ElasticRequest[Boolean] + ) extends ElasticRequest[Boolean, ExistsType] private[elasticsearch] final case class GetById( index: IndexName, id: DocumentId, routing: Option[Routing] = None - ) extends ElasticRequest[Option[Document]] + ) extends ElasticRequest[Option[Document], GetByIdType] private[elasticsearch] final case class GetByQuery( index: IndexName, query: ElasticQuery, routing: Option[Routing] = None - ) extends ElasticRequest[Option[ElasticQueryResponse]] + ) extends ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] + + private[elasticsearch] final case class Map[A, B, T <: ElasticRequestType]( + request: ElasticRequest[A, T], + mapper: A => B + ) extends ElasticRequest[B, T] + + trait AddRefresh[T <: ElasticRequestType] { + def addRefresh[A](req: ElasticRequest[A, T]): ElasticRequest[A, T] + } + + object AddRefresh { + implicit val addRefreshToCreate: AddRefresh[CreateType] = new AddRefresh[CreateType] { + override def addRefresh[A](req: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] = + req match { + case Map(r, mapper) => Map(addRefresh(r), mapper) + case r: Create => r.copy(refresh = true) + } + } + implicit val addRefreshToUpsert: AddRefresh[UpsertType] = new AddRefresh[UpsertType] { + override def addRefresh[A](req: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] = + req match { + case Map(r, mapper) => Map(addRefresh(r), mapper) + case r: CreateOrUpdate => r.copy(refresh = true) + } + } - private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B) - extends ElasticRequest[B] + implicit val addRefreshToDeleteById: AddRefresh[DeleteByIdType] = new AddRefresh[DeleteByIdType] { + override def addRefresh[A](req: ElasticRequest[A, DeleteByIdType]): ElasticRequest[A, DeleteByIdType] = + req match { + case Map(r, mapper) => Map(addRefresh(r), mapper) + case r: DeleteById => r.copy(refresh = true) + } + } + } } diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index eef268b9b..38617e1f2 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -13,7 +13,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC import HttpElasticExecutor._ - override def execute[A](request: ElasticRequest[A]): Task[A] = + override def execute[A](request: ElasticRequest[A, _]): Task[A] = request match { case r: Create => executeCreate(r) case r: CreateIndex => executeCreateIndex(r) From 06b99b4948b0453f30b4ae22273446ece3e1cc12 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Fri, 23 Dec 2022 18:48:57 +0100 Subject: [PATCH 2/7] Fix refresh --- .../scala/example/RepositoriesElasticsearch.scala | 6 +++--- .../scala/zio/elasticsearch/HttpElasticExecutor.scala | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index a68c5fdcb..f2d3fb22e 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -16,21 +16,21 @@ final case class RepositoriesElasticsearch(executor: ElasticExecutor) { def create(repository: GitHubRepo): Task[Option[DocumentId]] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.create(Index, repository).routing(routing) + req = ElasticRequest.create(Index, repository).routing(routing).refresh res <- executor.execute(req) } yield res def upsert(id: String, repository: GitHubRepo): Task[Unit] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing) + req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh _ <- executor.execute(req) } yield () def remove(organization: String, id: String): Task[Either[DocumentNotFound.type, Unit]] = for { routing <- routingOf(organization) - req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing) + req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refresh res <- executor.execute(req) } yield res diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 38617e1f2..2fd78d3d5 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -29,9 +29,10 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreate(r: Create): Task[Option[DocumentId]] = { val uri = r.id match { case Some(documentId) => - uri"${config.uri}/${r.index}/$Create/$documentId".withParam("routing", r.routing.map(Routing.unwrap)) + uri"${config.uri}/${r.index}/$Create/$documentId" + .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) case None => - uri"${config.uri}/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap)) + uri"${config.uri}/${r.index}/$Doc".withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) } sendRequestWithCustomResponse[ElasticCreateResponse]( @@ -52,13 +53,15 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC ).unit private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = { - val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" + .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).unit } private def executeDeleteById(r: DeleteById): Task[Boolean] = { - val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" + .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) sendRequestWithCustomResponse( request From 5ffac67aab30584fe912f9788c5438cd30363f21 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Sun, 25 Dec 2022 22:06:19 +0100 Subject: [PATCH 3/7] Fix routing --- .../zio/elasticsearch/HttpElasticExecutor.scala | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 2fd78d3d5..8a3d70bc3 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -30,9 +30,12 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC val uri = r.id match { case Some(documentId) => uri"${config.uri}/${r.index}/$Create/$documentId" - .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) + .withParam("routing", r.routing.map(Routing.unwrap)) + .withParam("refresh", r.refresh.toString) case None => - uri"${config.uri}/${r.index}/$Doc".withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) + uri"${config.uri}/${r.index}/$Doc" + .withParam("routing", r.routing.map(Routing.unwrap)) + .withParam("refresh", r.refresh.toString) } sendRequestWithCustomResponse[ElasticCreateResponse]( @@ -54,14 +57,16 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" - .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) + .withParam("routing", r.routing.map(Routing.unwrap)) + .withParam("refresh", r.refresh.toString) sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).unit } private def executeDeleteById(r: DeleteById): Task[Boolean] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" - .withParams(("routing", r.routing.toString), ("refresh", r.refresh.toString)) + .withParam("routing", r.routing.map(Routing.unwrap)) + .withParam("refresh", r.refresh.toString) sendRequestWithCustomResponse( request From ace4f390e03a2c7822678c88f7d88cd8d2cccdd8 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Mon, 26 Dec 2022 10:37:12 +0100 Subject: [PATCH 4/7] Fix code remarks --- .../example/RepositoriesElasticsearch.scala | 6 +- .../zio/elasticsearch/ElasticRequest.scala | 79 ++++++++++--------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index f2d3fb22e..44fc98b1b 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -16,21 +16,21 @@ final case class RepositoriesElasticsearch(executor: ElasticExecutor) { def create(repository: GitHubRepo): Task[Option[DocumentId]] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.create(Index, repository).routing(routing).refresh + req = ElasticRequest.create(Index, repository).routing(routing).refresh() res <- executor.execute(req) } yield res def upsert(id: String, repository: GitHubRepo): Task[Unit] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh + req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh() _ <- executor.execute(req) } yield () def remove(organization: String, id: String): Task[Either[DocumentNotFound.type, Unit]] = for { routing <- routingOf(organization) - req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refresh + req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refresh() res <- executor.execute(req) } yield res diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 245c925f8..08809c936 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -6,34 +6,22 @@ import zio.elasticsearch.ElasticRequest._ import zio.schema.Schema import zio.{RIO, ZIO} -sealed trait ElasticRequestType -object ElasticRequestType { - trait CreateIndexType extends ElasticRequestType - trait CreateType extends ElasticRequestType - trait DeleteByIdType extends ElasticRequestType - trait DeleteIndexType extends ElasticRequestType - trait ExistsType extends ElasticRequestType - trait GetByIdType extends ElasticRequestType - trait GetByQueryType extends ElasticRequestType - trait UpsertType extends ElasticRequestType -} +sealed trait ElasticRequest[+A, ERT <: ElasticRequestType] { self => -sealed trait ElasticRequest[+A, RequestType <: ElasticRequestType] { self => final def execute: RIO[ElasticExecutor, A] = ZIO.serviceWithZIO[ElasticExecutor](_.execute(self)) - final def map[B](f: A => B): ElasticRequest[B, RequestType] = ElasticRequest.Map(self, f) + final def map[B](f: A => B): ElasticRequest[B, ERT] = ElasticRequest.Map(self, f) - final def refresh(implicit addRefresh: AddRefresh[RequestType]): ElasticRequest[A, RequestType] = - addRefresh.addRefresh(self) + final def refresh()(implicit request: WithRefresh[ERT]): ElasticRequest[A, ERT] = request.withRefresh(self) - final def routing(value: Routing): ElasticRequest[A, RequestType] = self match { + final def routing(value: Routing): ElasticRequest[A, ERT] = self match { case Map(request, mapper) => Map(request.routing(value), mapper) - case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] - case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] - case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] - case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] - case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, RequestType]] + case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] case _ => self } } @@ -126,37 +114,50 @@ object ElasticRequest { routing: Option[Routing] = None ) extends ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] - private[elasticsearch] final case class Map[A, B, T <: ElasticRequestType]( - request: ElasticRequest[A, T], + private[elasticsearch] final case class Map[A, B, ERT <: ElasticRequestType]( + request: ElasticRequest[A, ERT], mapper: A => B - ) extends ElasticRequest[B, T] + ) extends ElasticRequest[B, ERT] - trait AddRefresh[T <: ElasticRequestType] { - def addRefresh[A](req: ElasticRequest[A, T]): ElasticRequest[A, T] + trait WithRefresh[ERT <: ElasticRequestType] { + def withRefresh[A](request: ElasticRequest[A, ERT]): ElasticRequest[A, ERT] } - object AddRefresh { - implicit val addRefreshToCreate: AddRefresh[CreateType] = new AddRefresh[CreateType] { - override def addRefresh[A](req: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] = - req match { - case Map(r, mapper) => Map(addRefresh(r), mapper) + object WithRefresh { + implicit val createWithRefresh: WithRefresh[CreateType] = new WithRefresh[CreateType] { + override def withRefresh[A](request: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] = + request match { + case Map(r, mapper) => Map(withRefresh(r), mapper) case r: Create => r.copy(refresh = true) } } - implicit val addRefreshToUpsert: AddRefresh[UpsertType] = new AddRefresh[UpsertType] { - override def addRefresh[A](req: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] = - req match { - case Map(r, mapper) => Map(addRefresh(r), mapper) + implicit val upsertWithRefresh: WithRefresh[UpsertType] = new WithRefresh[UpsertType] { + override def withRefresh[A](request: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] = + request match { + case Map(r, mapper) => Map(withRefresh(r), mapper) case r: CreateOrUpdate => r.copy(refresh = true) } } - implicit val addRefreshToDeleteById: AddRefresh[DeleteByIdType] = new AddRefresh[DeleteByIdType] { - override def addRefresh[A](req: ElasticRequest[A, DeleteByIdType]): ElasticRequest[A, DeleteByIdType] = - req match { - case Map(r, mapper) => Map(addRefresh(r), mapper) + implicit val deleteByIdWithRefresh: WithRefresh[DeleteByIdType] = new WithRefresh[DeleteByIdType] { + override def withRefresh[A](request: ElasticRequest[A, DeleteByIdType]): ElasticRequest[A, DeleteByIdType] = + request match { + case Map(r, mapper) => Map(withRefresh(r), mapper) case r: DeleteById => r.copy(refresh = true) } } } } + +sealed trait ElasticRequestType + +object ElasticRequestType { + trait CreateIndexType extends ElasticRequestType + trait CreateType extends ElasticRequestType + trait DeleteByIdType extends ElasticRequestType + trait DeleteIndexType extends ElasticRequestType + trait ExistsType extends ElasticRequestType + trait GetByIdType extends ElasticRequestType + trait GetByQueryType extends ElasticRequestType + trait UpsertType extends ElasticRequestType +} From 7dc3a53c149df7726d37ef5e54f575764d385cb0 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Tue, 27 Dec 2022 12:29:28 +0100 Subject: [PATCH 5/7] Refactor code --- .../example/RepositoriesElasticsearch.scala | 6 +- .../zio/elasticsearch/ElasticRequest.scala | 129 ++++++++---------- .../elasticsearch/HttpElasticExecutor.scala | 34 ++--- .../scala/zio/elasticsearch/Refresh.scala | 45 ++++++ 4 files changed, 120 insertions(+), 94 deletions(-) create mode 100644 modules/library/src/main/scala/zio/elasticsearch/Refresh.scala diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index 44fc98b1b..bd62b030c 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -16,21 +16,21 @@ final case class RepositoriesElasticsearch(executor: ElasticExecutor) { def create(repository: GitHubRepo): Task[Option[DocumentId]] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.create(Index, repository).routing(routing).refresh() + req = ElasticRequest.create(Index, repository).routing(routing).refreshTrue res <- executor.execute(req) } yield res def upsert(id: String, repository: GitHubRepo): Task[Unit] = for { routing <- routingOf(repository.organization) - req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh() + req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing).refresh(value = true) _ <- executor.execute(req) } yield () def remove(organization: String, id: String): Task[Either[DocumentNotFound.type, Unit]] = for { routing <- routingOf(organization) - req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refresh() + req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse res <- executor.execute(req) } yield res diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 08809c936..ab45a274a 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -3,6 +3,7 @@ package zio.elasticsearch import zio.elasticsearch.ElasticError.DocumentRetrievingError._ import zio.elasticsearch.ElasticError._ import zio.elasticsearch.ElasticRequest._ +import zio.elasticsearch.Refresh.WithRefresh import zio.schema.Schema import zio.{RIO, ZIO} @@ -13,16 +14,23 @@ sealed trait ElasticRequest[+A, ERT <: ElasticRequestType] { self => final def map[B](f: A => B): ElasticRequest[B, ERT] = ElasticRequest.Map(self, f) - final def refresh()(implicit request: WithRefresh[ERT]): ElasticRequest[A, ERT] = request.withRefresh(self) + final def refresh(value: Boolean)(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = + wr.withRefresh(request = self, value = value) + + final def refreshTrue(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = + wr.withRefresh(request = self, value = true) + + final def refreshFalse(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = + wr.withRefresh(request = self, value = false) final def routing(value: Routing): ElasticRequest[A, ERT] = self match { - case Map(request, mapper) => Map(request.routing(value), mapper) - case r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] - case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] - case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] - case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] - case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] - case _ => self + case Map(request, mapper) => Map(request.routing(value), mapper) + case r: CreateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: CreateOrUpdateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: DeleteByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: ExistsRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case r: GetByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] + case _ => self } } @@ -31,42 +39,42 @@ object ElasticRequest { import zio.elasticsearch.ElasticRequestType._ def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, CreateType] = - Create(index, Some(id), Document.from(doc)).map(_ => ()) + CreateRequest(index, Some(id), Document.from(doc)).map(_ => ()) def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], CreateType] = - Create(index, None, Document.from(doc)) + CreateRequest(index, None, Document.from(doc)) - def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndexType] = - CreateIndex(name, definition) + def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndex] = + CreateIndexRequest(name, definition) def deleteById( index: IndexName, id: DocumentId - ): ElasticRequest[Either[DocumentNotFound.type, Unit], DeleteByIdType] = - DeleteById(index, id).map(deleted => if (deleted) Right(()) else Left(DocumentNotFound)) + ): ElasticRequest[Either[DocumentNotFound.type, Unit], DeleteById] = + DeleteByIdRequest(index, id).map(deleted => if (deleted) Right(()) else Left(DocumentNotFound)) - def deleteIndex(name: IndexName): ElasticRequest[Boolean, DeleteIndexType] = - DeleteIndex(name) + def deleteIndex(name: IndexName): ElasticRequest[Boolean, DeleteIndex] = + DeleteIndexRequest(name) - def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean, ExistsType] = - Exists(index, id) + def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean, Exists] = + ExistsRequest(index, id) def getById[A: Schema]( index: IndexName, id: DocumentId - ): ElasticRequest[Either[DocumentRetrievingError, A], GetByIdType] = - GetById(index, id).map { + ): ElasticRequest[Either[DocumentRetrievingError, A], GetById] = + GetByIdRequest(index, id).map { case Some(document) => document.decode.left.map(err => DecoderError(err.message)) case None => Left(DocumentNotFound) } - def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] = - GetByQuery(index, query) + def search(index: IndexName, query: ElasticQuery): ElasticRequest[Option[ElasticQueryResponse], GetByQuery] = + GetByQueryRequest(index, query) - def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, UpsertType] = - CreateOrUpdate(index, id, Document.from(doc)) + def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Upsert] = + CreateOrUpdateRequest(index, id, Document.from(doc)) - private[elasticsearch] final case class Create( + private[elasticsearch] final case class CreateRequest( index: IndexName, id: Option[DocumentId], document: Document, @@ -74,90 +82,63 @@ object ElasticRequest { routing: Option[Routing] = None ) extends ElasticRequest[Option[DocumentId], CreateType] - private[elasticsearch] final case class CreateIndex( + private[elasticsearch] final case class CreateIndexRequest( name: IndexName, definition: Option[String] - ) extends ElasticRequest[Unit, CreateIndexType] + ) extends ElasticRequest[Unit, CreateIndex] - private[elasticsearch] final case class CreateOrUpdate( + private[elasticsearch] final case class CreateOrUpdateRequest( index: IndexName, id: DocumentId, document: Document, refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Unit, UpsertType] + ) extends ElasticRequest[Unit, Upsert] - private[elasticsearch] final case class DeleteById( + private[elasticsearch] final case class DeleteByIdRequest( index: IndexName, id: DocumentId, refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Boolean, DeleteByIdType] + ) extends ElasticRequest[Boolean, DeleteById] - private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Boolean, DeleteIndexType] + private[elasticsearch] final case class DeleteIndexRequest(name: IndexName) + extends ElasticRequest[Boolean, DeleteIndex] - private[elasticsearch] final case class Exists( + private[elasticsearch] final case class ExistsRequest( index: IndexName, id: DocumentId, routing: Option[Routing] = None - ) extends ElasticRequest[Boolean, ExistsType] + ) extends ElasticRequest[Boolean, Exists] - private[elasticsearch] final case class GetById( + private[elasticsearch] final case class GetByIdRequest( index: IndexName, id: DocumentId, routing: Option[Routing] = None - ) extends ElasticRequest[Option[Document], GetByIdType] + ) extends ElasticRequest[Option[Document], GetById] - private[elasticsearch] final case class GetByQuery( + private[elasticsearch] final case class GetByQueryRequest( index: IndexName, query: ElasticQuery, routing: Option[Routing] = None - ) extends ElasticRequest[Option[ElasticQueryResponse], GetByQueryType] + ) extends ElasticRequest[Option[ElasticQueryResponse], GetByQuery] private[elasticsearch] final case class Map[A, B, ERT <: ElasticRequestType]( request: ElasticRequest[A, ERT], mapper: A => B ) extends ElasticRequest[B, ERT] - trait WithRefresh[ERT <: ElasticRequestType] { - def withRefresh[A](request: ElasticRequest[A, ERT]): ElasticRequest[A, ERT] - } - - object WithRefresh { - implicit val createWithRefresh: WithRefresh[CreateType] = new WithRefresh[CreateType] { - override def withRefresh[A](request: ElasticRequest[A, CreateType]): ElasticRequest[A, CreateType] = - request match { - case Map(r, mapper) => Map(withRefresh(r), mapper) - case r: Create => r.copy(refresh = true) - } - } - implicit val upsertWithRefresh: WithRefresh[UpsertType] = new WithRefresh[UpsertType] { - override def withRefresh[A](request: ElasticRequest[A, UpsertType]): ElasticRequest[A, UpsertType] = - request match { - case Map(r, mapper) => Map(withRefresh(r), mapper) - case r: CreateOrUpdate => r.copy(refresh = true) - } - } - - implicit val deleteByIdWithRefresh: WithRefresh[DeleteByIdType] = new WithRefresh[DeleteByIdType] { - override def withRefresh[A](request: ElasticRequest[A, DeleteByIdType]): ElasticRequest[A, DeleteByIdType] = - request match { - case Map(r, mapper) => Map(withRefresh(r), mapper) - case r: DeleteById => r.copy(refresh = true) - } - } - } } sealed trait ElasticRequestType object ElasticRequestType { - trait CreateIndexType extends ElasticRequestType - trait CreateType extends ElasticRequestType - trait DeleteByIdType extends ElasticRequestType - trait DeleteIndexType extends ElasticRequestType - trait ExistsType extends ElasticRequestType - trait GetByIdType extends ElasticRequestType - trait GetByQueryType extends ElasticRequestType - trait UpsertType extends ElasticRequestType + trait CreateIndex extends ElasticRequestType + trait CreateType extends ElasticRequestType + trait DeleteById extends ElasticRequestType + trait DeleteIndex extends ElasticRequestType + trait Exists extends ElasticRequestType + trait GetById extends ElasticRequestType + trait GetByQuery extends ElasticRequestType + trait Upsert extends ElasticRequestType } diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 8a3d70bc3..9275de17c 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -15,18 +15,18 @@ 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: 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) - case r: GetByQuery => executeGetByQuery(r) - case map @ Map(_, _) => execute(map.request).map(map.mapper) + case r: CreateRequest => executeCreate(r) + case r: CreateIndexRequest => executeCreateIndex(r) + case r: CreateOrUpdateRequest => executeCreateOrUpdate(r) + case r: DeleteByIdRequest => executeDeleteById(r) + case r: DeleteIndexRequest => executeDeleteIndex(r) + case r: ExistsRequest => executeExists(r) + case r: GetByIdRequest => executeGetById(r) + case r: GetByQueryRequest => executeGetByQuery(r) + case map @ Map(_, _) => execute(map.request).map(map.mapper) } - private def executeCreate(r: Create): Task[Option[DocumentId]] = { + private def executeCreate(r: CreateRequest): Task[Option[DocumentId]] = { val uri = r.id match { case Some(documentId) => uri"${config.uri}/${r.index}/$Create/$documentId" @@ -47,7 +47,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC ).map(_.body.toOption).map(_.flatMap(body => DocumentId.make(body.id).toOption)) } - private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] = + private def executeCreateIndex(createIndex: CreateIndexRequest): Task[Unit] = sendRequest( request .put(uri"${config.uri}/${createIndex.name}") @@ -55,7 +55,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC .body(createIndex.definition.getOrElse("")) ).unit - private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = { + private def executeCreateOrUpdate(r: CreateOrUpdateRequest): Task[Unit] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" .withParam("routing", r.routing.map(Routing.unwrap)) .withParam("refresh", r.refresh.toString) @@ -63,7 +63,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).unit } - private def executeDeleteById(r: DeleteById): Task[Boolean] = { + private def executeDeleteById(r: DeleteByIdRequest): Task[Boolean] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" .withParam("routing", r.routing.map(Routing.unwrap)) .withParam("refresh", r.refresh.toString) @@ -75,16 +75,16 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC ).map(_.body.toOption).map(_.exists(_.result == "deleted")) } - private def executeDeleteIndex(r: DeleteIndex): Task[Boolean] = + private def executeDeleteIndex(r: DeleteIndexRequest): Task[Boolean] = sendRequest(request.delete(uri"${config.uri}/${r.name}")).map(_.code.equals(Ok)) - private def executeExists(r: Exists): Task[Boolean] = { + private def executeExists(r: ExistsRequest): Task[Boolean] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequest(request.head(uri)).map(_.code.equals(Ok)) } - private def executeGetById(r: GetById): Task[Option[Document]] = { + private def executeGetById(r: GetByIdRequest): Task[Option[Document]] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequestWithCustomResponse[ElasticGetResponse]( @@ -94,7 +94,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC ).map(_.body.toOption).map(_.flatMap(d => if (d.found) Some(Document.from(d.source)) else None)) } - private def executeGetByQuery(r: GetByQuery): Task[Option[ElasticQueryResponse]] = + private def executeGetByQuery(r: GetByQueryRequest): Task[Option[ElasticQueryResponse]] = sendRequestWithCustomResponse( request .post(uri"${config.uri}/${IndexName.unwrap(r.index)}/_search") diff --git a/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala new file mode 100644 index 000000000..5eb98443b --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala @@ -0,0 +1,45 @@ +package zio.elasticsearch + +import zio.elasticsearch.ElasticRequest.{CreateOrUpdateRequest, CreateRequest, DeleteByIdRequest, Map} +import zio.elasticsearch.ElasticRequestType.{CreateType, DeleteById, Upsert} + +object Refresh { + + trait WithRefresh[ERT <: ElasticRequestType] { + def withRefresh[A](request: ElasticRequest[A, ERT], value: Boolean): ElasticRequest[A, ERT] + } + + object WithRefresh { + implicit val createWithRefresh: WithRefresh[CreateType] = new WithRefresh[CreateType] { + override def withRefresh[A]( + request: ElasticRequest[A, CreateType], + value: Boolean + ): ElasticRequest[A, CreateType] = + request match { + case Map(r, mapper) => Map(withRefresh(r, value), mapper) + case r: CreateRequest => r.copy(refresh = value) + } + } + implicit val upsertWithRefresh: WithRefresh[Upsert] = new WithRefresh[Upsert] { + override def withRefresh[A]( + request: ElasticRequest[A, Upsert], + value: Boolean + ): ElasticRequest[A, Upsert] = + request match { + case Map(r, mapper) => Map(withRefresh(r, value), mapper) + case r: CreateOrUpdateRequest => r.copy(refresh = value) + } + } + + implicit val deleteByIdWithRefresh: WithRefresh[DeleteById] = new WithRefresh[DeleteById] { + override def withRefresh[A]( + request: ElasticRequest[A, DeleteById], + value: Boolean + ): ElasticRequest[A, DeleteById] = + request match { + case Map(r, mapper) => Map(withRefresh(r, value), mapper) + case r: DeleteByIdRequest => r.copy(refresh = value) + } + } + } +} From bdd03ce30684725a13548766724dd5f210cb902c Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Tue, 27 Dec 2022 12:59:41 +0100 Subject: [PATCH 6/7] Fix code remarks --- .../zio/elasticsearch/ElasticRequest.scala | 6 +++--- .../main/scala/zio/elasticsearch/Refresh.scala | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index ab45a274a..60c85d4f1 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -17,12 +17,12 @@ sealed trait ElasticRequest[+A, ERT <: ElasticRequestType] { self => final def refresh(value: Boolean)(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = wr.withRefresh(request = self, value = value) - final def refreshTrue(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = - wr.withRefresh(request = self, value = true) - final def refreshFalse(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = wr.withRefresh(request = self, value = false) + final def refreshTrue(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] = + wr.withRefresh(request = self, value = true) + final def routing(value: Routing): ElasticRequest[A, ERT] = self match { case Map(request, mapper) => Map(request.routing(value), mapper) case r: CreateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]] diff --git a/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala index 5eb98443b..fb781d240 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala @@ -20,16 +20,6 @@ object Refresh { case r: CreateRequest => r.copy(refresh = value) } } - implicit val upsertWithRefresh: WithRefresh[Upsert] = new WithRefresh[Upsert] { - override def withRefresh[A]( - request: ElasticRequest[A, Upsert], - value: Boolean - ): ElasticRequest[A, Upsert] = - request match { - case Map(r, mapper) => Map(withRefresh(r, value), mapper) - case r: CreateOrUpdateRequest => r.copy(refresh = value) - } - } implicit val deleteByIdWithRefresh: WithRefresh[DeleteById] = new WithRefresh[DeleteById] { override def withRefresh[A]( @@ -41,5 +31,13 @@ object Refresh { case r: DeleteByIdRequest => r.copy(refresh = value) } } + + implicit val upsertWithRefresh: WithRefresh[Upsert] = new WithRefresh[Upsert] { + override def withRefresh[A](request: ElasticRequest[A, Upsert], value: Boolean): ElasticRequest[A, Upsert] = + request match { + case Map(r, mapper) => Map(withRefresh(r, value), mapper) + case r: CreateOrUpdateRequest => r.copy(refresh = value) + } + } } } From 2ca5acebc1aebc022b93e4d99b9696e419be47d8 Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Tue, 27 Dec 2022 13:08:03 +0100 Subject: [PATCH 7/7] Refactor code --- .../scala/zio/elasticsearch/ElasticRequest.scala | 8 ++++---- .../main/scala/zio/elasticsearch/Refresh.scala | 16 +++++----------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 60c85d4f1..58b535ea2 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -38,10 +38,10 @@ object ElasticRequest { import zio.elasticsearch.ElasticRequestType._ - def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, CreateType] = + def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Create] = CreateRequest(index, Some(id), Document.from(doc)).map(_ => ()) - def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], CreateType] = + def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId], Create] = CreateRequest(index, None, Document.from(doc)) def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit, CreateIndex] = @@ -80,7 +80,7 @@ object ElasticRequest { document: Document, refresh: Boolean = false, routing: Option[Routing] = None - ) extends ElasticRequest[Option[DocumentId], CreateType] + ) extends ElasticRequest[Option[DocumentId], Create] private[elasticsearch] final case class CreateIndexRequest( name: IndexName, @@ -134,7 +134,7 @@ sealed trait ElasticRequestType object ElasticRequestType { trait CreateIndex extends ElasticRequestType - trait CreateType extends ElasticRequestType + trait Create extends ElasticRequestType trait DeleteById extends ElasticRequestType trait DeleteIndex extends ElasticRequestType trait Exists extends ElasticRequestType diff --git a/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala index fb781d240..4e125afc6 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/Refresh.scala @@ -1,7 +1,7 @@ package zio.elasticsearch import zio.elasticsearch.ElasticRequest.{CreateOrUpdateRequest, CreateRequest, DeleteByIdRequest, Map} -import zio.elasticsearch.ElasticRequestType.{CreateType, DeleteById, Upsert} +import zio.elasticsearch.ElasticRequestType.{Create, DeleteById, Upsert} object Refresh { @@ -10,11 +10,8 @@ object Refresh { } object WithRefresh { - implicit val createWithRefresh: WithRefresh[CreateType] = new WithRefresh[CreateType] { - override def withRefresh[A]( - request: ElasticRequest[A, CreateType], - value: Boolean - ): ElasticRequest[A, CreateType] = + implicit val createWithRefresh: WithRefresh[Create] = new WithRefresh[Create] { + def withRefresh[A](request: ElasticRequest[A, Create], value: Boolean): ElasticRequest[A, Create] = request match { case Map(r, mapper) => Map(withRefresh(r, value), mapper) case r: CreateRequest => r.copy(refresh = value) @@ -22,10 +19,7 @@ object Refresh { } implicit val deleteByIdWithRefresh: WithRefresh[DeleteById] = new WithRefresh[DeleteById] { - override def withRefresh[A]( - request: ElasticRequest[A, DeleteById], - value: Boolean - ): ElasticRequest[A, DeleteById] = + def withRefresh[A](request: ElasticRequest[A, DeleteById], value: Boolean): ElasticRequest[A, DeleteById] = request match { case Map(r, mapper) => Map(withRefresh(r, value), mapper) case r: DeleteByIdRequest => r.copy(refresh = value) @@ -33,7 +27,7 @@ object Refresh { } implicit val upsertWithRefresh: WithRefresh[Upsert] = new WithRefresh[Upsert] { - override def withRefresh[A](request: ElasticRequest[A, Upsert], value: Boolean): ElasticRequest[A, Upsert] = + def withRefresh[A](request: ElasticRequest[A, Upsert], value: Boolean): ElasticRequest[A, Upsert] = request match { case Map(r, mapper) => Map(withRefresh(r, value), mapper) case r: CreateOrUpdateRequest => r.copy(refresh = value)