Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dsl): Implement exists query #30

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ object ElasticQuery {

def boolQuery(): BoolQuery = BoolQuery.empty

def exists(field: String): Exists = Exists(field)

def range(field: String): Range[Unbounded.type, Unbounded.type] = Range.empty(field)

private[elasticsearch] final case class BoolQuery(must: List[ElasticQuery], should: List[ElasticQuery])
Expand All @@ -62,6 +64,10 @@ object ElasticQuery {
def empty: BoolQuery = BoolQuery(Nil, Nil)
}

private[elasticsearch] final case class Exists private (field: String) extends ElasticQuery {
override def toJson: Json = Obj("exists" -> Obj("field" -> field.toJson))
}

private[elasticsearch] final case class Match[A: ElasticPrimitive](field: String, value: A) extends ElasticQuery {
override def toJson: Json = Obj("match" -> Obj(field -> value.toJson))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ object QueryDSLSpec extends ZIOSpecDefault {
)
)
},
test("successfully create Exists Query") {
val query = exists(field = "day_of_week")

assert(query)(equalTo(Exists(field = "day_of_week")))
},
test("successfully create empty Range Query") {
val query = range(field = "customer_age")

Expand Down Expand Up @@ -210,6 +215,21 @@ object QueryDSLSpec extends ZIOSpecDefault {

assert(query.toJsonBody)(equalTo(expected.toJson))
},
test("properly encode Exists Query") {
val query = exists(field = "day_of_week")
val expected =
"""
|{
| "query": {
| "exists": {
| "field": "day_of_week"
| }
| }
|}
|""".stripMargin

assert(query.toJsonBody)(equalTo(expected.toJson))
},
test("properly encode Unbounded Range Query") {
val query = range(field = "field")
val expected =
Expand Down