Skip to content

Commit

Permalink
Limit 'term' query to use Strings only
Browse files Browse the repository at this point in the history
  • Loading branch information
drmarjanovic committed Apr 25, 2023
1 parent c7bfd44 commit f4b67ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object ElasticQuery {
* @return
* an instance of [[zio.elasticsearch.query.WildcardQuery]] that represents the wildcard query to be performed.
*/
final def contains[S](field: Field[S, _], value: String): WildcardQuery[S] =
final def contains[S](field: Field[S, String], value: String): WildcardQuery[S] =
Wildcard(
field = field.toString,
value = s"*$value*",
Expand Down Expand Up @@ -345,7 +345,7 @@ object ElasticQuery {
* @return
* an instance of [[zio.elasticsearch.query.WildcardQuery]] that represents the wildcard query to be performed.
*/
final def startsWith[S](field: Field[S, _], value: String): WildcardQuery[S] =
final def startsWith[S](field: Field[S, String], value: String): WildcardQuery[S] =
Wildcard(
field = field.toString,
value = s"$value*",
Expand Down Expand Up @@ -384,7 +384,7 @@ object ElasticQuery {
* @return
* an instance of [[zio.elasticsearch.query.TermQuery]] that represents the term query to be performed.
*/
final def term[S, A: ElasticPrimitive](field: Field[S, A], value: A): TermQuery[S] =
final def term[S](field: Field[S, String], value: String): TermQuery[S] =
Term(
field = field.toString,
value = value,
Expand All @@ -406,7 +406,7 @@ object ElasticQuery {
* @return
* an instance of [[zio.elasticsearch.query.TermQuery]] that represents the term query to be performed.
*/
final def term[A: ElasticPrimitive](field: String, value: A): Term[Any, A] =
final def term(field: String, value: String): Term[Any] =
Term(field = field, value = value, boost = None, caseInsensitive = None)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ private[elasticsearch] object Range {

sealed trait TermQuery[S] extends ElasticQuery[S] with HasBoost[TermQuery[S]] with HasCaseInsensitive[TermQuery[S]]

private[elasticsearch] final case class Term[S, A: ElasticPrimitive](
private[elasticsearch] final case class Term[S](
field: String,
value: A,
value: String,
boost: Option[Double],
caseInsensitive: Option[Boolean]
) extends TermQuery[S] { self =>
Expand Down
90 changes: 20 additions & 70 deletions modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -574,52 +574,32 @@ object QueryDSLSpec extends ZIOSpecDefault {
)
},
test("successfully create Term Query") {
val queryInt = term(field = "day_of_week", value = 1)
val queryString = term(field = "day_of_week", value = "Monday")
val queryBool = term(field = "day_of_week", value = true)
val queryLong = term(field = "day_of_week", value = 1L)
val query = term(field = "day_of_week", value = "Monday")

assert(queryInt)(
equalTo(Term[Any, Int](field = "day_of_week", value = 1, boost = None, caseInsensitive = None))
) &&
assert(queryString)(
equalTo(
Term[Any, String](field = "day_of_week", value = "Monday", boost = None, caseInsensitive = None)
)
) &&
assert(queryBool)(
equalTo(Term[Any, Boolean](field = "day_of_week", value = true, boost = None, caseInsensitive = None))
) &&
assert(queryLong)(
equalTo(Term[Any, Long](field = "day_of_week", value = 1L, boost = None, caseInsensitive = None))
assert(query)(
equalTo(Term[Any](field = "day_of_week", value = "Monday", boost = None, caseInsensitive = None))
)
},
test("successfully create type-safe Term Query") {
val queryString = term(field = TestSubDocument.stringField, value = "StringField")
val queryInt = term(field = TestSubDocument.intField, value = 39)
val query = term(field = TestSubDocument.stringField, value = "StringField")

assert(queryString)(
assert(query)(
equalTo(
Term[TestSubDocument, String](
Term[TestSubDocument](
field = "stringField",
value = "StringField",
boost = None,
caseInsensitive = None
)
)
) &&
assert(queryInt)(
equalTo(
Term[TestSubDocument, Int](field = "intField", value = 39, boost = None, caseInsensitive = None)
)
)
},
test("successfully create type-safe Term Query with suffix") {
val query = term(field = TestSubDocument.stringField.keyword, value = "StringField")

assert(query)(
equalTo(
Term[TestSubDocument, String](
Term[TestSubDocument](
field = "stringField.keyword",
value = "StringField",
boost = None,
Expand All @@ -629,54 +609,24 @@ object QueryDSLSpec extends ZIOSpecDefault {
)
},
test("successfully create Term Query with boost") {
val queryInt = term(field = "day_of_week", value = 1).boost(1.0)
val queryString = term(field = "day_of_week", value = "Monday").boost(1.0)
val queryBool = term(field = "day_of_week", value = true).boost(1.0)
val queryLong = term(field = "day_of_week", value = 1L).boost(1.0)
val query = term(field = "day_of_week", value = "Monday").boost(1.0)

assert(queryInt)(
equalTo(Term[Any, Int](field = "day_of_week", value = 1, boost = Some(1.0), caseInsensitive = None))
) &&
assert(queryString)(
equalTo(
Term[Any, String](field = "day_of_week", value = "Monday", boost = Some(1.0), caseInsensitive = None)
)
) &&
assert(queryBool)(
equalTo(
Term[Any, Boolean](field = "day_of_week", value = true, boost = Some(1.0), caseInsensitive = None)
)
) &&
assert(queryLong)(
equalTo(Term[Any, Long](field = "day_of_week", value = 1L, boost = Some(1.0), caseInsensitive = None))
assert(query)(
equalTo(Term[Any](field = "day_of_week", value = "Monday", boost = Some(1.0), caseInsensitive = None))
)
},
test("successfully create case insensitive Term Query") {
val queryString = term(field = "day_of_week", value = "Monday").caseInsensitiveTrue
val query = term(field = "day_of_week", value = "Monday").caseInsensitiveTrue

assert(queryString)(
equalTo(
Term[Any, String](
field = "day_of_week",
value = "Monday",
boost = None,
caseInsensitive = Some(true)
)
)
assert(query)(
equalTo(Term[Any](field = "day_of_week", value = "Monday", boost = None, caseInsensitive = Some(true)))
)
},
test("successfully create case insensitive Term Query with boost") {
val queryString = term(field = "day_of_week", value = "Monday").boost(1.0).caseInsensitiveTrue
val query = term(field = "day_of_week", value = "Monday").boost(1.0).caseInsensitiveTrue

assert(queryString)(
equalTo(
Term[Any, String](
field = "day_of_week",
value = "Monday",
boost = Some(1.0),
caseInsensitive = Some(true)
)
)
assert(query)(
equalTo(Term[Any](field = "day_of_week", value = "Monday", boost = Some(1.0), caseInsensitive = Some(true)))
)
},
test("successfully create Wildcard Query") {
Expand Down Expand Up @@ -1397,14 +1347,14 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Term query") {
val query = term(field = "day_of_week", value = true)
val query = term(field = "day_of_week", value = "Friday")
val expected =
"""
|{
| "query": {
| "term": {
| "day_of_week": {
| "value": true
| "value": "Friday"
| }
| }
| }
Expand All @@ -1414,14 +1364,14 @@ object QueryDSLSpec extends ZIOSpecDefault {
assert(query.toJson)(equalTo(expected.toJson))
},
test("properly encode Term query with boost") {
val query = term(field = "day_of_week", value = true).boost(1.0)
val query = term(field = "day_of_week", value = "Friday").boost(1.0)
val expected =
"""
|{
| "query": {
| "term": {
| "day_of_week": {
| "value": true,
| "value": "Friday",
| "boost": 1.0
| }
| }
Expand Down

0 comments on commit f4b67ad

Please sign in to comment.