diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index e2ee2f4eb..7cb8a8f41 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -37,8 +37,9 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) { def findById(organization: String, id: String): Task[Option[GitHubRepo]] = for { routing <- routingOf(organization) - res <- - elasticsearch.execute(ElasticRequest.getById(Index, DocumentId(id)).routing(routing)).documentAs[GitHubRepo] + res <- elasticsearch + .execute(ElasticRequest.getById(Index, DocumentId(id)).routing(routing)) + .documentAs[GitHubRepo] } yield res def create(repository: GitHubRepo): Task[CreationOutcome] = diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index cb5a0df1f..0a083cc2d 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -41,10 +41,10 @@ object ElasticRequest { Bulk.of(requests = requests: _*) def create[A: Schema](index: IndexName, doc: A): Create = - Create(index = index, document = Document.from(doc), refresh = false, routing = None) + Create(index = index, document = Document.from(doc), refresh = None, routing = None) def create[A: Schema](index: IndexName, id: DocumentId, doc: A): CreateWithId = - CreateWithId(index = index, id = id, document = Document.from(doc), refresh = false, routing = None) + CreateWithId(index = index, id = id, document = Document.from(doc), refresh = None, routing = None) def createIndex(name: IndexName): CreateIndex = CreateIndex(name = name, definition = None) @@ -53,10 +53,10 @@ object ElasticRequest { CreateIndex(name = name, definition = Some(definition)) def deleteById(index: IndexName, id: DocumentId): DeleteById = - DeleteById(index = index, id = id, refresh = false, routing = None) + DeleteById(index = index, id = id, refresh = None, routing = None) def deleteByQuery(index: IndexName, query: ElasticQuery[_]): DeleteByQuery = - DeleteByQuery(index = index, query = query, refresh = false, routing = None) + DeleteByQuery(index = index, query = query, refresh = None, routing = None) def deleteIndex(name: IndexName): DeleteIndex = DeleteIndex(name = name) @@ -65,23 +65,23 @@ object ElasticRequest { Exists(index = index, id = id, routing = None) def getById(index: IndexName, id: DocumentId): GetById = - GetById(index = index, id = id, routing = None) + GetById(index = index, id = id, refresh = None, routing = None) def search(index: IndexName, query: ElasticQuery[_]): Search = Search(index = index, query = query, routing = None) def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): CreateOrUpdate = - CreateOrUpdate(index = index, id = id, document = Document.from(doc), refresh = false, routing = None) + CreateOrUpdate(index = index, id = id, document = Document.from(doc), refresh = None, routing = None) sealed trait BulkRequest extends ElasticRequest[Unit] with HasRefresh[Unit] with HasRouting[Unit] private[elasticsearch] final case class Bulk( requests: List[BulkableRequest[_]], index: Option[IndexName], - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends BulkRequest { self => - def refresh(value: Boolean): Bulk = self.copy(refresh = value) + def refresh(value: Boolean): Bulk = self.copy(refresh = Some(value)) def refreshFalse: Bulk = refresh(false) @@ -111,7 +111,7 @@ object ElasticRequest { object Bulk { def of(requests: BulkableRequest[_]*): Bulk = - Bulk(requests = requests.toList, index = None, refresh = false, routing = None) + Bulk(requests = requests.toList, index = None, refresh = None, routing = None) } sealed trait CreateRequest extends BulkableRequest[DocumentId] with HasRefresh[DocumentId] with HasRouting[DocumentId] @@ -119,10 +119,10 @@ object ElasticRequest { private[elasticsearch] final case class Create( index: IndexName, document: Document, - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends CreateRequest { self => - def refresh(value: Boolean): Create = self.copy(refresh = value) + def refresh(value: Boolean): Create = self.copy(refresh = Some(value)) def refreshFalse: Create = refresh(false) @@ -140,10 +140,10 @@ object ElasticRequest { index: IndexName, id: DocumentId, document: Document, - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends CreateWithIdRequest { self => - def refresh(value: Boolean): CreateWithId = self.copy(refresh = value) + def refresh(value: Boolean): CreateWithId = self.copy(refresh = Some(value)) def refreshFalse: CreateWithId = refresh(false) @@ -165,10 +165,10 @@ object ElasticRequest { index: IndexName, id: DocumentId, document: Document, - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends CreateOrUpdateRequest { self => - def refresh(value: Boolean): CreateOrUpdate = self.copy(refresh = value) + def refresh(value: Boolean): CreateOrUpdate = self.copy(refresh = Some(value)) def refreshFalse: CreateOrUpdate = refresh(false) @@ -185,10 +185,10 @@ object ElasticRequest { private[elasticsearch] final case class DeleteById( index: IndexName, id: DocumentId, - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends DeleteByIdRequest { self => - def refresh(value: Boolean): DeleteById = self.copy(refresh = value) + def refresh(value: Boolean): DeleteById = self.copy(refresh = Some(value)) def refreshFalse: DeleteById = refresh(false) @@ -205,10 +205,10 @@ object ElasticRequest { private[elasticsearch] final case class DeleteByQuery( index: IndexName, query: ElasticQuery[_], - refresh: Boolean, + refresh: Option[Boolean], routing: Option[Routing] ) extends DeleteByQueryRequest { self => - def refresh(value: Boolean): DeleteByQuery = self.copy(refresh = value) + def refresh(value: Boolean): DeleteByQuery = self.copy(refresh = Some(value)) def refreshFalse: DeleteByQuery = refresh(false) @@ -231,13 +231,20 @@ object ElasticRequest { def routing(value: Routing): Exists = self.copy(routing = Some(value)) } - sealed trait GetByIdRequest extends ElasticRequest[GetResult] with HasRouting[GetResult] + sealed trait GetByIdRequest extends ElasticRequest[GetResult] with HasRefresh[GetResult] with HasRouting[GetResult] private[elasticsearch] final case class GetById( index: IndexName, id: DocumentId, + refresh: Option[Boolean], routing: Option[Routing] ) extends GetByIdRequest { self => + def refresh(value: Boolean): GetById = self.copy(refresh = Some(value)) + + def refreshFalse: GetById = refresh(false) + + def refreshTrue: GetById = refresh(true) + def routing(value: Routing): GetById = self.copy(routing = Some(value)) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 4bd79a4e3..eb144f9f1 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -72,7 +72,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC val uri = (r.index match { case Some(index) => uri"${config.uri}/$index/$Bulk" case None => uri"${config.uri}/$Bulk" - }).withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))) + }).withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))) sendRequest( request.post(uri).contentType(ApplicationJson).body(r.body) @@ -86,7 +86,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreate(r: Create): Task[DocumentId] = { val uri = uri"${config.uri}/${r.index}/$Doc" - .withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))) + .withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))) sendRequestWithCustomResponse[ElasticCreateResponse]( request @@ -112,7 +112,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreateWithId(r: CreateWithId): Task[CreationOutcome] = { val uri = uri"${config.uri}/${r.index}/$Create/${r.id}" - .withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))) + .withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))) sendRequest( request @@ -144,7 +144,7 @@ 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(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))) + .withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))) sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).flatMap { response => response.code match { @@ -156,7 +156,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeDeleteById(r: DeleteById): Task[DeletionOutcome] = { val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}" - .withParams(getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing)))) + .withParams(getQueryParams(List(("refresh", r.refresh), ("routing", r.routing)))) sendRequest(request.delete(uri)).flatMap { response => response.code match { @@ -170,7 +170,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeDeleteByQuery(r: DeleteByQuery): Task[DeletionOutcome] = { val uri = uri"${config.uri}/${r.index}/$DeleteByQuery".withParams( - getQueryParams(List(("refresh", Some(r.refresh)), ("routing", r.routing))) + getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))) ) sendRequest( @@ -209,7 +209,9 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC } private def executeGetById(r: GetById): Task[GetResult] = { - val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParams(getQueryParams(List(("routing", r.routing)))) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParams( + getQueryParams(List(("refresh", r.refresh), ("routing", r.routing))) + ) sendRequestWithCustomResponse[ElasticGetResponse]( request