From 4dfcf7b0224d0111aa7f5b1ab950cc322d12e7ac Mon Sep 17 00:00:00 2001 From: markaya Date: Fri, 26 May 2023 11:43:29 +0200 Subject: [PATCH 1/3] Minor fixes --- .../elasticsearch/executor/HttpExecutor.scala | 21 +++++++++---------- .../zio/elasticsearch/query/Distance.scala | 18 ++++++++-------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala index 0c3f6cd3b..44cea5605 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala @@ -44,7 +44,7 @@ import zio.elasticsearch.executor.response.{ import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome, UpdateOutcome} import zio.elasticsearch.result._ import zio.json.ast.Json -import zio.json.ast.Json.{Arr, Obj, Str} +import zio.json.ast.Json.{Arr, Num, Obj, Str} import zio.json.{DeriveJsonDecoder, JsonDecoder} import zio.schema.Schema import zio.stream.{Stream, ZStream} @@ -403,32 +403,31 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig searchAfter: Option[Json] ): Task[(Chunk[Item], Option[(String, Option[Json])])] = { val pointInTimeJson = - Json.Obj( - "pit" -> Json.Obj( - "id" -> Json.Str(pitId), - KeepAlive -> Json.Str(config.keepAlive) + Obj( + "pit" -> Obj( + "id" -> Str(pitId), + KeepAlive -> Str(config.keepAlive) ) ) val sortsJson = if (r.sortBy.isEmpty) { - Obj("sort" -> Json.Arr(Json.Str(ShardDoc))) + Obj("sort" -> Arr(Str(ShardDoc))) } else { Obj("sort" -> Arr(r.sortBy.map(_.toJson))) } - val searchAfterJson = searchAfter.fold(Obj())(sa => Obj("search_after" -> sa)) - sendRequestWithCustomResponse( baseRequest .get(uri"${esConfig.uri}/$Search") .response(asJson[SearchWithAggregationsResponse]) .contentType(ApplicationJson) .body( - Obj("query" -> r.query.toJson(fieldPath = None)) merge + pointInTimeJson merge sortsJson merge - pointInTimeJson merge - searchAfterJson + Obj("query" -> r.query.toJson(fieldPath = None)) merge + searchAfter.fold(Obj())(sa => Obj("search_after" -> sa)) merge + config.pageSize.fold(Obj())(ps => Obj("size" -> Num(ps))) ) ).flatMap { response => response.code match { diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala b/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala index e3fdb057b..804dc3105 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/Distance.scala @@ -26,15 +26,15 @@ sealed trait DistanceUnit { } object DistanceUnit { - case object Centimeter extends DistanceUnit { def symbol: String = "cm" } - case object Feet extends DistanceUnit { def symbol: String = "ft" } - case object Inch extends DistanceUnit { def symbol: String = "in" } - case object Kilometers extends DistanceUnit { def symbol: String = "km" } - case object Mile extends DistanceUnit { def symbol: String = "mi" } - case object Meter extends DistanceUnit { def symbol: String = "m" } - case object Milimeter extends DistanceUnit { def symbol: String = "mm" } - case object NauticalMile extends DistanceUnit { def symbol: String = "nmi" } - case object Yard extends DistanceUnit { def symbol: String = "yd" } + case object Centimeters extends DistanceUnit { def symbol: String = "cm" } + case object Feet extends DistanceUnit { def symbol: String = "ft" } + case object Inches extends DistanceUnit { def symbol: String = "in" } + case object Kilometers extends DistanceUnit { def symbol: String = "km" } + case object Miles extends DistanceUnit { def symbol: String = "mi" } + case object Meters extends DistanceUnit { def symbol: String = "m" } + case object Millimeters extends DistanceUnit { def symbol: String = "mm" } + case object NauticalMiles extends DistanceUnit { def symbol: String = "nmi" } + case object Yards extends DistanceUnit { def symbol: String = "yd" } } sealed trait DistanceType { From e55882ab766d86dcafbbed97327872ae4d191e9f Mon Sep 17 00:00:00 2001 From: markaya Date: Fri, 26 May 2023 14:52:38 +0200 Subject: [PATCH 2/3] Use implicit conversion where possible --- .../elasticsearch/executor/HttpExecutor.scala | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala index 44cea5605..f051599b4 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala @@ -19,28 +19,13 @@ package zio.elasticsearch.executor import sttp.client3.ziojson._ import sttp.client3.{Identity, RequestT, Response, ResponseException, SttpBackend, UriContext, basicRequest => request} import sttp.model.MediaType.ApplicationJson -import sttp.model.StatusCode.{ - BadRequest => HttpBadRequest, - Conflict => HttpConflict, - Created => HttpCreated, - Forbidden => HttpForbidden, - NotFound => HttpNotFound, - Ok => HttpOk, - Unauthorized => HttpUnauthorized -} +import sttp.model.StatusCode.{BadRequest => HttpBadRequest, Conflict => HttpConflict, Created => HttpCreated, Forbidden => HttpForbidden, NotFound => HttpNotFound, Ok => HttpOk, Unauthorized => HttpUnauthorized} import sttp.model.Uri.QuerySegment import zio.ZIO.logDebug +import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps import zio.elasticsearch.ElasticRequest._ import zio.elasticsearch._ -import zio.elasticsearch.executor.response.{ - BulkResponse, - CountResponse, - CreateResponse, - DocumentWithHighlightsAndSort, - GetResponse, - SearchWithAggregationsResponse, - UpdateByQueryResponse -} +import zio.elasticsearch.executor.response.{BulkResponse, CountResponse, CreateResponse, DocumentWithHighlightsAndSort, GetResponse, SearchWithAggregationsResponse, UpdateByQueryResponse} import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome, UpdateOutcome} import zio.elasticsearch.result._ import zio.json.ast.Json @@ -341,7 +326,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig .post(uri"${esConfig.uri}/$Search/$Scroll".withParams((Scroll, config.keepAlive))) .response(asJson[SearchWithAggregationsResponse]) .contentType(ApplicationJson) - .body(Obj(ScrollId -> Str(scrollId))) + .body(Obj(ScrollId -> scrollId.toJson)) ).flatMap { response => response.code match { case HttpOk => @@ -405,14 +390,14 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig val pointInTimeJson = Obj( "pit" -> Obj( - "id" -> Str(pitId), - KeepAlive -> Str(config.keepAlive) + "id" -> pitId.toJson, + KeepAlive -> config.keepAlive.toJson ) ) val sortsJson = if (r.sortBy.isEmpty) { - Obj("sort" -> Arr(Str(ShardDoc))) + Obj("sort" -> Arr(ShardDoc.toJson)) } else { Obj("sort" -> Arr(r.sortBy.map(_.toJson))) } @@ -427,7 +412,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig sortsJson merge Obj("query" -> r.query.toJson(fieldPath = None)) merge searchAfter.fold(Obj())(sa => Obj("search_after" -> sa)) merge - config.pageSize.fold(Obj())(ps => Obj("size" -> Num(ps))) + config.pageSize.fold(Obj())(ps => Obj("size" -> ps.toJson)) ) ).flatMap { response => response.code match { From 7fef950fffba4ee0d4f6780676f23fe10bf0a9be Mon Sep 17 00:00:00 2001 From: markaya Date: Fri, 26 May 2023 14:53:33 +0200 Subject: [PATCH 3/3] Fix linter --- .../elasticsearch/executor/HttpExecutor.scala | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala index f051599b4..f3af52ad5 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/executor/HttpExecutor.scala @@ -19,17 +19,33 @@ package zio.elasticsearch.executor import sttp.client3.ziojson._ import sttp.client3.{Identity, RequestT, Response, ResponseException, SttpBackend, UriContext, basicRequest => request} import sttp.model.MediaType.ApplicationJson -import sttp.model.StatusCode.{BadRequest => HttpBadRequest, Conflict => HttpConflict, Created => HttpCreated, Forbidden => HttpForbidden, NotFound => HttpNotFound, Ok => HttpOk, Unauthorized => HttpUnauthorized} +import sttp.model.StatusCode.{ + BadRequest => HttpBadRequest, + Conflict => HttpConflict, + Created => HttpCreated, + Forbidden => HttpForbidden, + NotFound => HttpNotFound, + Ok => HttpOk, + Unauthorized => HttpUnauthorized +} import sttp.model.Uri.QuerySegment import zio.ZIO.logDebug import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps import zio.elasticsearch.ElasticRequest._ import zio.elasticsearch._ -import zio.elasticsearch.executor.response.{BulkResponse, CountResponse, CreateResponse, DocumentWithHighlightsAndSort, GetResponse, SearchWithAggregationsResponse, UpdateByQueryResponse} +import zio.elasticsearch.executor.response.{ + BulkResponse, + CountResponse, + CreateResponse, + DocumentWithHighlightsAndSort, + GetResponse, + SearchWithAggregationsResponse, + UpdateByQueryResponse +} import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome, UpdateOutcome} import zio.elasticsearch.result._ import zio.json.ast.Json -import zio.json.ast.Json.{Arr, Num, Obj, Str} +import zio.json.ast.Json.{Arr, Obj} import zio.json.{DeriveJsonDecoder, JsonDecoder} import zio.schema.Schema import zio.stream.{Stream, ZStream}