Skip to content

Commit

Permalink
Implement additional test and make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed Mar 9, 2023
1 parent 4d2e02f commit ac5b1e4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
29 changes: 15 additions & 14 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ object ElasticQuery {

private[elasticsearch] final case class ExistsQuery[S](field: String) extends ElasticQuery[S, Exists] {
def paramsToJson(fieldPath: Option[String]): Json = Obj(
"exists" -> Obj("field" -> (fieldPath.getOrElse("") ++ field).toJson)
"exists" -> Obj("field" -> (fieldPath.map(_ + ".").getOrElse("") + field).toJson)
)
}

private[elasticsearch] final case class MatchQuery[S, A: ElasticPrimitive](field: String, value: A)
extends ElasticQuery[S, Match] {
def paramsToJson(fieldPath: Option[String]): Json = Obj(
"match" -> Obj(fieldPath.getOrElse("") ++ field -> value.toJson)
"match" -> Obj(fieldPath.map(_ + ".").getOrElse("") + field -> value.toJson)
)
}

Expand All @@ -196,16 +196,17 @@ object ElasticQuery {
scoreMode: Option[ScoreMode],
ignoreUnmapped: Option[Boolean]
) extends ElasticQuery[S, Nested] {
def paramsToJson(fieldPath: Option[String]): Json = Obj(
"nested" -> Obj(
List(
"path" -> Str(path),
"query" -> query.paramsToJson(fieldPath.map(_ + path).orElse(Some(path)))
) ++ scoreMode.map(scoreMode => "score_mode" -> Str(scoreMode.toString.toLowerCase)) ++ ignoreUnmapped.map(
"ignore_unmapped" -> Json.Bool(_)
): _*
def paramsToJson(fieldPath: Option[String]): Json =
Obj(
"nested" -> Obj(
List(
"path" -> fieldPath.map(fieldPath => Str(fieldPath + "." + path)).getOrElse(Str(path)),
"query" -> query.paramsToJson(fieldPath.map(_ + "." + path).orElse(Some(path)))
) ++ scoreMode.map(scoreMode => "score_mode" -> Str(scoreMode.toString.toLowerCase)) ++ ignoreUnmapped.map(
"ignore_unmapped" -> Json.Bool(_)
): _*
)
)
)
}

sealed trait LowerBound {
Expand Down Expand Up @@ -265,7 +266,7 @@ object ElasticQuery {

def paramsToJson(fieldPath: Option[String]): Json = {
val rangeFields = Some(
fieldPath.getOrElse("") ++ field -> Obj(List(lower.toJson, upper.toJson).flatten: _*)
fieldPath.map(_ + ".").getOrElse("") + field -> Obj(List(lower.toJson, upper.toJson).flatten: _*)
) ++ boost.map("boost" -> Num(_))
Obj("range" -> Obj(rangeFields.toList: _*))
}
Expand All @@ -291,7 +292,7 @@ object ElasticQuery {
val termFields = Some("value" -> value.toJson) ++ boost.map("boost" -> Num(_)) ++ caseInsensitive.map(
"case_insensitive" -> Json.Bool(_)
)
Obj("term" -> Obj(fieldPath.getOrElse("") ++ field -> Obj(termFields.toList: _*)))
Obj("term" -> Obj(fieldPath.map(_ + ".").getOrElse("") + field -> Obj(termFields.toList: _*)))
}
}

Expand All @@ -305,7 +306,7 @@ object ElasticQuery {
val wildcardFields = Some("value" -> value.toJson) ++ boost.map("boost" -> Num(_)) ++ caseInsensitive.map(
"case_insensitive" -> Json.Bool(_)
)
Obj("wildcard" -> Obj(fieldPath.getOrElse("") ++ field -> Obj(wildcardFields.toList: _*)))
Obj("wildcard" -> Obj(fieldPath.map(_ + ".").getOrElse("") + field -> Obj(wildcardFields.toList: _*)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,33 @@ object QueryDSLSpec extends ZIOSpecDefault {

assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode nested Nested Queries with Term Query") {
val query = nested(path = "customer", query = nested(path = "items", query = term("type", "clothing")))
val expected =
"""
|{
| "query": {
| "nested": {
| "path": "customer",
| "query": {
| "nested": {
| "path": "customer.items",
| "query": {
| "term": {
| "customer.items.type": {
| "value": "clothing"
| }
| }
| }
| }
| }
| }
| }
|}
|""".stripMargin

assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Nested Query with MatchAll Query and score_mode") {
val query = nested(path = "customer", query = matchAll).scoreMode(ScoreMode.Avg)
val expected =
Expand Down

0 comments on commit ac5b1e4

Please sign in to comment.