Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 committed May 12, 2023
1 parent 2745f22 commit 3e21d1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ object ElasticRequest {
sealed trait AggregateRequest extends ElasticRequest[AggregationResult]

private[elasticsearch] final case class Aggregate(index: IndexName, aggregation: ElasticAggregation)
extends AggregateRequest
extends AggregateRequest {
def toJson: Json = Obj("aggs" -> aggregation.toJson)
}

sealed trait BulkRequest
extends ElasticRequest[BulkResponse]
Expand Down Expand Up @@ -423,6 +425,8 @@ object ElasticRequest {
) extends CountRequest { self =>
def routing(value: Routing): CountRequest =
self.copy(routing = Some(value))

def toJson: Json = query.fold(Obj())(q => Obj("query" -> q.toJson(fieldPath = None)))
}

sealed trait CreateRequest
Expand Down Expand Up @@ -522,6 +526,8 @@ object ElasticRequest {

def routing(value: Routing): DeleteByQueryRequest =
self.copy(routing = Some(value))

def toJson: Json = Obj("query" -> query.toJson(fieldPath = None))
}

sealed trait DeleteIndexRequest extends ElasticRequest[DeletionOutcome]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
.post(uri"${esConfig.uri}/${r.index}/$Search?typed_keys")
.response(asJson[SearchWithAggregationsResponse])
.contentType(ApplicationJson)
.body(Obj("aggs" -> r.aggregation.toJson))
.body(r.toJson)
).flatMap { response =>
response.code match {
case HttpOk =>
Expand Down Expand Up @@ -156,10 +156,9 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
.get(uri"${esConfig.uri}/${r.index}/$Count".withParams(getQueryParams(List(("routing", r.routing)))))
.contentType(ApplicationJson)
.response(asJson[CountResponse])
.body()

sendRequestWithCustomResponse(
r.query.fold(req)(query => req.body(Obj("query" -> query.toJson(fieldPath = None))))
).flatMap { response =>
sendRequestWithCustomResponse(r.query.fold(req)(_ => req.body(r.toJson))).flatMap { response =>
response.code match {
case HttpOk =>
response.body
Expand Down Expand Up @@ -288,7 +287,7 @@ private[elasticsearch] final class HttpExecutor private (esConfig: ElasticConfig
baseRequest
.post(uri)
.contentType(ApplicationJson)
.body(Obj("query" -> r.query.toJson(fieldPath = None)))
.body(r.toJson)
).flatMap { response =>
response.code match {
case HttpOk => ZIO.succeed(DeletionOutcome.Deleted)
Expand Down

0 comments on commit 3e21d1d

Please sign in to comment.