Skip to content

Commit

Permalink
Refactoring and cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
kevchuang committed Feb 8, 2023
1 parent 2b11947 commit 0ca53f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
11 changes: 1 addition & 10 deletions modules/library/src/main/scala/zio/elasticsearch/Boost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@

package zio.elasticsearch

import zio.elasticsearch.ElasticQuery.{
BoolQuery,
ElasticPrimitive,
LowerBound,
MatchAllQuery,
RangeQuery,
TermQuery,
UpperBound,
WildcardQuery
}
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticQueryType.{Bool, MatchAll, Range, Term, Wildcard}

object Boost {
Expand Down
69 changes: 46 additions & 23 deletions modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query)(equalTo(MatchQuery(field = "name.keyword", value = "Name")))
},
test("successfully create Bool Query with boost") {
val query = boolQuery().boost(1.0)
val query = boolQuery.boost(1.0)

assert(query)(equalTo(BoolQuery(filter = Nil, must = Nil, should = Nil, boost = Some(1.0))))
},
Expand All @@ -82,7 +82,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
MatchQuery(field = "customer_gender", value = "MALE")
),
must = Nil,
should = Nil
should = Nil,
boost = None
)
)
)
Expand All @@ -99,7 +100,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
),
should = Nil
should = Nil,
boost = None
)
)
)
Expand All @@ -119,7 +121,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
should = List(
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
)
),
boost = None
)
)
)
Expand All @@ -141,7 +144,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
MatchQuery(field = "customer_gender", value = "MALE")
),
must = List(MatchQuery(field = "customer_age", value = 23)),
should = List(MatchQuery(field = "customer_id", value = 1))
should = List(MatchQuery(field = "customer_id", value = 1)),
boost = None
)
)
)
Expand All @@ -160,7 +164,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
),
should = List(MatchQuery(field = "customer_age", value = 23))
should = List(MatchQuery(field = "customer_age", value = 23)),
boost = None
)
)
)
Expand All @@ -182,13 +187,14 @@ object QueryDSLSpec extends ZIOSpecDefault {
should = List(
MatchQuery(field = "day_of_week", value = "Monday"),
MatchQuery(field = "customer_gender", value = "MALE")
)
),
boost = None
)
)
)
},
test("successfully create `Filter/Must/Should` mixed query with boost") {
val query = boolQuery()
val query = boolQuery
.filter(matches(field = "customer_id", value = 1))
.must(matches(field = "customer_age", value = 23))
.should(matches(field = "day_of_week", value = "Monday"))
Expand Down Expand Up @@ -233,7 +239,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Any, Unbounded.type, Unbounded.type](
field = "customer_age",
lower = Unbounded,
upper = Unbounded
upper = Unbounded,
boost = None
)
)
)
Expand All @@ -244,12 +251,22 @@ object QueryDSLSpec extends ZIOSpecDefault {

assert(queryString)(
equalTo(
RangeQuery[String, Unbounded.type, Unbounded.type](field = "name", lower = Unbounded, upper = Unbounded)
RangeQuery[String, Unbounded.type, Unbounded.type](
field = "name",
lower = Unbounded,
upper = Unbounded,
boost = None
)
)
) &&
assert(queryInt)(
equalTo(
RangeQuery[Int, Unbounded.type, Unbounded.type](field = "age", lower = Unbounded, upper = Unbounded)
RangeQuery[Int, Unbounded.type, Unbounded.type](
field = "age",
lower = Unbounded,
upper = Unbounded,
boost = None
)
)
)
},
Expand All @@ -261,7 +278,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[String, Unbounded.type, Unbounded.type](
field = "name.keyword",
lower = Unbounded,
upper = Unbounded
upper = Unbounded,
boost = None
)
)
)
Expand All @@ -274,7 +292,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Int, Unbounded.type, LessThan[Int]](
field = "customer_age",
lower = Unbounded,
upper = LessThan(23)
upper = LessThan(23),
boost = None
)
)
)
Expand All @@ -287,7 +306,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Int, GreaterThan[Int], Unbounded.type](
field = "customer_age",
lower = GreaterThan(23),
upper = Unbounded
upper = Unbounded,
boost = None
)
)
)
Expand All @@ -300,7 +320,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Int, Unbounded.type, LessThanOrEqualTo[Int]](
field = "customer_age",
lower = Unbounded,
upper = LessThanOrEqualTo(23)
upper = LessThanOrEqualTo(23),
boost = None
)
)
)
Expand All @@ -313,7 +334,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Int, GreaterThanOrEqualTo[Int], Unbounded.type](
field = "customer_age",
lower = GreaterThanOrEqualTo(23),
upper = Unbounded
upper = Unbounded,
boost = None
)
)
)
Expand All @@ -326,7 +348,8 @@ object QueryDSLSpec extends ZIOSpecDefault {
RangeQuery[Int, GreaterThanOrEqualTo[Int], LessThan[Int]](
field = "customer_age",
lower = GreaterThanOrEqualTo(23),
upper = LessThan(50)
upper = LessThan(50),
boost = None
)
)
)
Expand Down Expand Up @@ -477,7 +500,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Bool Query with boost") {
val query = boolQuery().boost(1.0)
val query = boolQuery.boost(1.0)
val expected =
"""
|{
Expand All @@ -492,7 +515,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(query.toJsonBody)(equalTo(expected.toJson))
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Bool Query with Filter containing `Match` leaf query") {
val query = boolQuery.filter(matches(field = "day_of_week", value = "Monday"))
Expand Down Expand Up @@ -602,7 +625,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Bool Query with Filter, Must and Should containing `Match` leaf query and with boost") {
val query = boolQuery()
val query = boolQuery
.filter(matches(field = "customer_age", value = 23))
.must(matches(field = "customer_id", value = 1))
.should(matches(field = "day_of_week", value = "Monday"))
Expand Down Expand Up @@ -639,7 +662,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(query.toJsonBody)(equalTo(expected.toJson))
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Exists Query") {
val query = exists(field = "day_of_week")
Expand Down Expand Up @@ -715,7 +738,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(query.toJsonBody)(equalTo(expected.toJson))
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Range Query with Lower Bound") {
val query = range(field = "customer_age").gt(23)
Expand Down Expand Up @@ -820,7 +843,7 @@ object QueryDSLSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(query.toJsonBody)(equalTo(expected.toJson))
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Term query") {
val query = term(field = "day_of_week", value = true)
Expand Down

0 comments on commit 0ca53f5

Please sign in to comment.