From 671fe8890716a679d38812fad69c79e0d95f374d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragutin=20Marjanovi=C4=87?= Date: Fri, 28 Apr 2023 09:11:51 +0200 Subject: [PATCH] Restructure tests --- .../zio/elasticsearch/QueryDSLSpec.scala | 985 +++++++++--------- 1 file changed, 492 insertions(+), 493 deletions(-) diff --git a/modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala index df3956677..012e90da3 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/QueryDSLSpec.scala @@ -837,6 +837,324 @@ object QueryDSLSpec extends ZIOSpecDefault { } ), suite("encoding ElasticQuery as JSON")( + suite("bool")( + test("filter") { + val query = filter(matches(field = "day_of_week", value = "Monday")) + val expected = + """ + |{ + | "query": { + | "bool": { + | "filter": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ] + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("filter (boost)") { + val query = filter(matches(field = "day_of_week", value = "Monday")).boost(1.0) + val expected = + """ + |{ + | "query": { + | "bool": { + | "filter": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ], + | "boost": 1.0 + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("must") { + val query = must(matches(field = "day_of_week", value = "Monday")) + val expected = + """ + |{ + | "query": { + | "bool": { + | "must": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ] + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("mustNot") { + val query = mustNot(matches(field = "day_of_week", value = "Monday")) + val expected = + """ + |{ + | "query": { + | "bool": { + | "must_not": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ] + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("should") { + val query = should(matches(field = "day_of_week", value = "Monday")) + val expected = + """ + |{ + | "query": { + | "bool": { + | "should": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ] + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("filter + must + mustNot + should") { + val query = filter(matches(field = "customer_age", value = 23)) + .must(matches(field = "customer_id", value = 1)) + .mustNot(matches(field = "day_of_month", value = 17)) + .should(matches(field = "day_of_week", value = "Monday")) + val expected = + """ + |{ + | "query": { + | "bool": { + | "filter": [ + | { + | "match": { + | "customer_age": 23 + | } + | } + | ], + | "must": [ + | { + | "match": { + | "customer_id": 1 + | } + | } + | ], + | "must_not": [ + | { + | "match": { + | "day_of_month": 17 + | } + | } + | ], + | "should": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ] + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("filter + must + mustNot + should (boost)") { + val query = filter(matches(field = "customer_age", value = 23)) + .must(matches(field = "customer_id", value = 1)) + .mustNot(matches(field = "day_of_month", value = 17)) + .should(matches(field = "day_of_week", value = "Monday")) + .boost(1.0) + val expected = + """ + |{ + | "query": { + | "bool": { + | "filter": [ + | { + | "match": { + | "customer_age": 23 + | } + | } + | ], + | "must": [ + | { + | "match": { + | "customer_id": 1 + | } + | } + | ], + | "must_not": [ + | { + | "match": { + | "day_of_month": 17 + | } + | } + | ], + | "should": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | } + | ], + | "boost": 1.0 + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, + test("filter + must + mustNot + should (boost, minimumShouldMatch") { + val query = filter(matches(field = "customer_age", value = 23)) + .must(matches(field = "customer_id", value = 1)) + .mustNot(matches(field = "day_of_month", value = 17)) + .should( + matches(field = "day_of_week", value = "Monday"), + matches(field = "day_of_week", value = "Tuesday"), + matches(field = "day_of_week", value = "Wednesday") + ) + .boost(1.0) + .minimumShouldMatch(2) + val expected = + """ + |{ + | "query": { + | "bool": { + | "filter": [ + | { + | "match": { + | "customer_age": 23 + | } + | } + | ], + | "must": [ + | { + | "match": { + | "customer_id": 1 + | } + | } + | ], + | "must_not": [ + | { + | "match": { + | "day_of_month": 17 + | } + | } + | ], + | "should": [ + | { + | "match": { + | "day_of_week": "Monday" + | } + | }, + | { + | "match": { + | "day_of_week": "Tuesday" + | } + | }, + | { + | "match": { + | "day_of_week": "Wednesday" + | } + | } + | ], + | "boost": 1.0, + | "minimum_should_match": 2 + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + } + ), + test("bulk") { + val bulkQuery = IndexName.make("users").map { index => + val nestedField = TestNestedField("NestedField", 1) + val subDoc = TestSubDocument( + stringField = "StringField", + nestedField = nestedField, + intField = 100, + intFieldList = Nil + ) + val req1 = + ElasticRequest + .create[TestSubDocument](index, DocumentId("ETux1srpww2ObCx"), subDoc.copy(intField = 65)) + .routing(unsafeWrap(subDoc.stringField)(Routing)) + val req2 = + ElasticRequest.create[TestSubDocument](index, subDoc).routing(unsafeWrap(subDoc.stringField)(Routing)) + val req3 = ElasticRequest + .upsert[TestSubDocument](index, DocumentId("yMyEG8iFL5qx"), subDoc.copy(stringField = "StringField2")) + .routing(unsafeWrap(subDoc.stringField)(Routing)) + val req4 = + ElasticRequest + .deleteById(index, DocumentId("1VNzFt2XUFZfXZheDc")) + .routing(unsafeWrap(subDoc.stringField)(Routing)) + ElasticRequest.bulk(req1, req2, req3, req4) match { + case r: Bulk => Some(r.body) + case _ => None + } + } + + val expectedBody = + """|{ "create" : { "_index" : "users", "_id" : "ETux1srpww2ObCx", "routing" : "StringField" } } + |{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":65,"intFieldList":[]} + |{ "create" : { "_index" : "users", "routing" : "StringField" } } + |{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]} + |{ "index" : { "_index" : "users", "_id" : "yMyEG8iFL5qx", "routing" : "StringField" } } + |{"stringField":"StringField2","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]} + |{ "delete" : { "_index" : "users", "_id" : "1VNzFt2XUFZfXZheDc", "routing" : "StringField" } } + |""".stripMargin + + assert(bulkQuery)(equalTo(Validation.succeed(Some(expectedBody)))) + }, + test("exists") { + val query = exists(field = "day_of_week") + val expected = + """ + |{ + | "query": { + | "exists": { + | "field": "day_of_week" + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) + }, test("hasChild") { hasChild("child", matches("field", "value")) val queryWithIgnoreUnmapped = hasChild("child", matches("field", "value")).ignoreUnmappedTrue @@ -1059,382 +1377,49 @@ object QueryDSLSpec extends ZIOSpecDefault { |} |""".stripMargin - val expectedWithInnerHits = - """ - |{ - | "query": { - | "has_parent": { - | "parent_type": "parent", - | "query": { - | "match": { - | "field" : "value" - | } - | }, - | "inner_hits": {} - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) && - assert(queryWithScore.toJson)(equalTo(expectedWithScore.toJson)) && - assert(queryWithIgnoreUnmapped.toJson)(equalTo(expectedWithIgnoreUnmapped.toJson)) && - assert(queryWithScoreAndIgnoreUnmapped.toJson)(equalTo(expectedWithScoreAndIgnoreUnmapped.toJson)) && - assert(queryWithInnerHits.toJson)(equalTo(expectedWithInnerHits.toJson)) - }, - test("matches") { - val query = matches(field = "day_of_week", value = true) - val expected = - """ - |{ - | "query": { - | "match": { - | "day_of_week": true - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("matchPhrase") { - val querySimple = matchPhrase(field = "stringField", value = "this is a test") - val queryRaw = matchPhrase(field = "stringField.raw", value = "this is a test") - val queryWithBoost = matchPhrase(field = "stringField", value = "this is a test").boost(21.15) - val querySimpleTs = matchPhrase(field = TestDocument.stringField, value = "this is a test") - val queryRawTs = matchPhrase(field = TestDocument.stringField.raw, value = "this is a test") - val queryWithBoostTs = matchPhrase(field = TestDocument.stringField, value = "this is a test").boost(21.15) - - val querySimpleExpectedJson = - """ - |{ - | "query": { - | "match_phrase": { - | "stringField": "this is a test" - | } - | } - |} - |""".stripMargin - - val queryRawExpectedJson = - """ - |{ - | "query": { - | "match_phrase": { - | "stringField.raw": "this is a test" - | } - | } - |} - |""".stripMargin - - val queryWithBoostExpectedJson = - """ - |{ - | "query": { - | "match_phrase": { - | "stringField": "this is a test", - | "boost": 21.15 - | } - | } - |} - |""".stripMargin - - assert(querySimple.toJson)(equalTo(querySimpleExpectedJson.toJson)) && - assert(querySimpleTs.toJson)(equalTo(querySimpleExpectedJson.toJson)) && - assert(queryRaw.toJson)(equalTo(queryRawExpectedJson.toJson)) && - assert(queryRawTs.toJson)(equalTo(queryRawExpectedJson.toJson)) && - assert(queryWithBoost.toJson)(equalTo(queryWithBoostExpectedJson.toJson)) && - assert(queryWithBoostTs.toJson)(equalTo(queryWithBoostExpectedJson.toJson)) - }, - test("term") { - val query = term(field = TestDocument.stringField, value = "test") - val queryWithBoost = term(field = TestDocument.stringField, value = "test").boost(10.21) - val queryWithCaseInsensitive = term(field = TestDocument.stringField, value = "test").caseInsensitiveTrue - val queryWithAllParams = - term(field = TestDocument.stringField, value = "test").boost(3.14).caseInsensitiveFalse - - val expected = - """ - |{ - | "query": { - | "term": { - | "stringField": { - | "value": "test" - | } - | } - | } - |} - |""".stripMargin - - val expectedWithBoost = - """ - |{ - | "query": { - | "term": { - | "stringField": { - | "value": "test", - | "boost": 10.21 - | } - | } - | } - |} - |""".stripMargin - - val expectedWithCaseInsensitive = - """ - |{ - | "query": { - | "term": { - | "stringField": { - | "value": "test", - | "case_insensitive": true - | } - | } - | } - |} - |""".stripMargin - - val expectedWithAllParams = - """ - |{ - | "query": { - | "term": { - | "stringField": { - | "value": "test", - | "boost": 3.14, - | "case_insensitive": false - | } - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) && - assert(queryWithBoost.toJson)(equalTo(expectedWithBoost.toJson)) && - assert(queryWithCaseInsensitive.toJson)(equalTo(expectedWithCaseInsensitive.toJson)) && - assert(queryWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson)) - }, - test("terms") { - val query = terms(field = TestDocument.stringField, values = "a", "b", "c") - val queryWithBoost = terms(field = TestDocument.stringField, values = "a", "b", "c").boost(10.21) - - val expected = - """ - |{ - | "query": { - | "terms": { - | "stringField": [ "a", "b", "c" ] - | } - | } - |} - |""".stripMargin - - val expectedWithBoost = - """ - |{ - | "query": { - | "terms": { - | "stringField": [ "a", "b", "c" ], - | "boost": 10.21 - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) && - assert(queryWithBoost.toJson)(equalTo(expectedWithBoost.toJson)) - }, - - test("properly encode Bool Query with Filter containing `Match` leaf query") { - val query = filter(matches(field = "day_of_week", value = "Monday")) - val expected = - """ - |{ - | "query": { - | "bool": { - | "filter": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ] - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Bool Query with Filter containing `Match` leaf query with boost") { - val query = filter(matches(field = "day_of_week", value = "Monday")).boost(1.0) - val expected = - """ - |{ - | "query": { - | "bool": { - | "filter": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ], - | "boost": 1.0 - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Bool Query with Must containing `Match` leaf query") { - val query = must(matches(field = "day_of_week", value = "Monday")) - val expected = - """ - |{ - | "query": { - | "bool": { - | "must": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ] - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Bool Query with MustNot containing `Match` leaf query") { - val query = mustNot(matches(field = "day_of_week", value = "Monday")) - val expected = - """ - |{ - | "query": { - | "bool": { - | "must_not": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ] - | } - | } - |} - |""".stripMargin - - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Bool Query with Should containing `Match` leaf query") { - val query = should(matches(field = "day_of_week", value = "Monday")) - val expected = + val expectedWithInnerHits = """ |{ | "query": { - | "bool": { - | "should": [ - | { - | "match": { - | "day_of_week": "Monday" - | } + | "has_parent": { + | "parent_type": "parent", + | "query": { + | "match": { + | "field" : "value" | } - | ] + | }, + | "inner_hits": {} | } | } |} |""".stripMargin - assert(query.toJson)(equalTo(expected.toJson)) + assert(query.toJson)(equalTo(expected.toJson)) && + assert(queryWithScore.toJson)(equalTo(expectedWithScore.toJson)) && + assert(queryWithIgnoreUnmapped.toJson)(equalTo(expectedWithIgnoreUnmapped.toJson)) && + assert(queryWithScoreAndIgnoreUnmapped.toJson)(equalTo(expectedWithScoreAndIgnoreUnmapped.toJson)) && + assert(queryWithInnerHits.toJson)(equalTo(expectedWithInnerHits.toJson)) }, - test("properly encode Bool Query with Filter, Must, MustNot and Should containing `Match` leaf query") { - val query = filter(matches(field = "customer_age", value = 23)) - .must(matches(field = "customer_id", value = 1)) - .mustNot(matches(field = "day_of_month", value = 17)) - .should(matches(field = "day_of_week", value = "Monday")) + test("matchAll") { + val query = matchAll val expected = """ |{ | "query": { - | "bool": { - | "filter": [ - | { - | "match": { - | "customer_age": 23 - | } - | } - | ], - | "must": [ - | { - | "match": { - | "customer_id": 1 - | } - | } - | ], - | "must_not": [ - | { - | "match": { - | "day_of_month": 17 - | } - | } - | ], - | "should": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ] - | } + | "match_all": {} | } |} |""".stripMargin assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Bool Query with Filter, Must, MustNot and Should containing `Match` leaf query, boost") { - val query = filter(matches(field = "customer_age", value = 23)) - .must(matches(field = "customer_id", value = 1)) - .mustNot(matches(field = "day_of_month", value = 17)) - .should(matches(field = "day_of_week", value = "Monday")) - .boost(1.0) + test("matchAll (boost)") { + val query = matchAll.boost(1.0) val expected = """ |{ | "query": { - | "bool": { - | "filter": [ - | { - | "match": { - | "customer_age": 23 - | } - | } - | ], - | "must": [ - | { - | "match": { - | "customer_id": 1 - | } - | } - | ], - | "must_not": [ - | { - | "match": { - | "day_of_month": 17 - | } - | } - | ], - | "should": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | } - | ], + | "match_all": { | "boost": 1.0 | } | } @@ -1443,64 +1428,14 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test( - "properly encode Bool Query with Filter, Must, MustNot and Should containing `Match` leaf query and with boost, minimumShouldMatch" - ) { - val query = filter(matches(field = "customer_age", value = 23)) - .must(matches(field = "customer_id", value = 1)) - .mustNot(matches(field = "day_of_month", value = 17)) - .should( - matches(field = "day_of_week", value = "Monday"), - matches(field = "day_of_week", value = "Tuesday"), - matches(field = "day_of_week", value = "Wednesday") - ) - .boost(1.0) - .minimumShouldMatch(2) + test("matches") { + val query = matches(field = "day_of_week", value = true) val expected = """ |{ | "query": { - | "bool": { - | "filter": [ - | { - | "match": { - | "customer_age": 23 - | } - | } - | ], - | "must": [ - | { - | "match": { - | "customer_id": 1 - | } - | } - | ], - | "must_not": [ - | { - | "match": { - | "day_of_month": 17 - | } - | } - | ], - | "should": [ - | { - | "match": { - | "day_of_week": "Monday" - | } - | }, - | { - | "match": { - | "day_of_week": "Tuesday" - | } - | }, - | { - | "match": { - | "day_of_week": "Wednesday" - | } - | } - | ], - | "boost": 1.0, - | "minimum_should_match": 2 + | "match": { + | "day_of_week": true | } | } |} @@ -1508,59 +1443,65 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("exists") { - val query = exists(field = "day_of_week") - val expected = + test("matches (type-safe)") { + val queryString = matches(field = TestSubDocument.stringField, value = "StringField") + val queryInt = matches(field = TestSubDocument.intField, value = 39) + + assert(queryString)( + equalTo(Match[TestSubDocument, String](field = "stringField", value = "StringField", boost = None)) + ) && + assert(queryInt)(equalTo(Match[TestSubDocument, Int](field = "intField", value = 39, boost = None))) + }, + test("matchPhrase") { + val querySimple = matchPhrase(field = "stringField", value = "this is a test") + val queryRaw = matchPhrase(field = "stringField.raw", value = "this is a test") + val queryWithBoost = matchPhrase(field = "stringField", value = "this is a test").boost(21.15) + val querySimpleTs = matchPhrase(field = TestDocument.stringField, value = "this is a test") + val queryRawTs = matchPhrase(field = TestDocument.stringField.raw, value = "this is a test") + val queryWithBoostTs = matchPhrase(field = TestDocument.stringField, value = "this is a test").boost(21.15) + + val querySimpleExpectedJson = """ |{ | "query": { - | "exists": { - | "field": "day_of_week" + | "match_phrase": { + | "stringField": "this is a test" | } | } |} |""".stripMargin - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("matchAll") { - val query = matchAll - val expected = + val queryRawExpectedJson = """ |{ | "query": { - | "match_all": {} + | "match_phrase": { + | "stringField.raw": "this is a test" + | } | } |} |""".stripMargin - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("matchAll (boost)") { - val query = matchAll.boost(1.0) - val expected = + val queryWithBoostExpectedJson = """ |{ | "query": { - | "match_all": { - | "boost": 1.0 + | "match_phrase": { + | "stringField": "this is a test", + | "boost": 21.15 | } | } |} |""".stripMargin - assert(query.toJson)(equalTo(expected.toJson)) - }, - test("matches (type-safe)") { - val queryString = matches(field = TestSubDocument.stringField, value = "StringField") - val queryInt = matches(field = TestSubDocument.intField, value = 39) - - assert(queryString)( - equalTo(Match[TestSubDocument, String](field = "stringField", value = "StringField", boost = None)) - ) && - assert(queryInt)(equalTo(Match[TestSubDocument, Int](field = "intField", value = 39, boost = None))) + assert(querySimple.toJson)(equalTo(querySimpleExpectedJson.toJson)) && + assert(querySimpleTs.toJson)(equalTo(querySimpleExpectedJson.toJson)) && + assert(queryRaw.toJson)(equalTo(queryRawExpectedJson.toJson)) && + assert(queryRawTs.toJson)(equalTo(queryRawExpectedJson.toJson)) && + assert(queryWithBoost.toJson)(equalTo(queryWithBoostExpectedJson.toJson)) && + assert(queryWithBoostTs.toJson)(equalTo(queryWithBoostExpectedJson.toJson)) }, - test("properly encode Nested Query with MatchAll Query") { + test("nested") { val query = nested(path = "customer", query = matchAll) val expected = """ @@ -1578,7 +1519,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode nested Nested Queries with Term Query") { + test("nested 2") { val query = nested(path = "customer", query = nested(path = "items", query = term("type", "clothing"))) val expected = """ @@ -1605,7 +1546,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Nested Query with MatchAll Query and score_mode") { + test("nested (scoreMode)") { val query = nested(path = "customer", query = matchAll).scoreMode(ScoreMode.Avg) val expected = """ @@ -1624,7 +1565,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Nested Query with MatchAll Query and ignore_unmapped") { + test("nested (ignoreUnmapped)") { val query = nested(path = "customer", query = matchAll).ignoreUnmappedFalse val expected = """ @@ -1643,7 +1584,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Nested Query with MatchAll Query, score_mode and ignore_unmapped") { + test("nested (scoreMode, ignoreUnmapped)") { val query = nested(path = "customer", query = matchAll).scoreMode(ScoreMode.Avg).ignoreUnmappedFalse val expected = """ @@ -1663,7 +1604,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Nested Query with MatchAll Query and inner hits with empty body") { + test("nested (empty innerHits)") { val query = nested(path = "customer", query = matchAll).innerHits val expected = """ @@ -1682,7 +1623,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Nested Query with MatchAll Query and inner hits with from, size and name fields") { + test("nested (innerHits)") { val query = nested(path = "customer", query = matchAll).innerHits( InnerHits.from(0).size(3).name("name") ) @@ -1707,7 +1648,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Unbounded Range Query") { + test("range") { val query = range(field = "field") val expected = """ @@ -1723,7 +1664,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Unbounded Range Query with boost") { + test("range (boost)") { val query = range(field = "field").boost(1.0) val expected = """ @@ -1740,7 +1681,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with Lower Bound") { + test("range (lower bound)") { val query = range(field = "customer_age").gt(23) val expected = """ @@ -1757,7 +1698,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with Upper Bound") { + test("range (upper bound)") { val query = range(field = "customer_age").lt(23) val expected = """ @@ -1774,7 +1715,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with Inclusive Lower Bound") { + test("range (inclusive lower bound)") { val query = range(field = "expiry_date").gte("now") val expected = """ @@ -1791,7 +1732,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with inclusive Upper Bound") { + test("range (inclusive upper bound") { val query = range(field = "customer_age").lte(100L) val expected = """ @@ -1808,7 +1749,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with both Upper and Lower Bound") { + test("range (bounds)") { val query = range(field = "customer_age").gte(10).lt(100) val expected = """ @@ -1826,7 +1767,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Range Query with both Upper and Lower Bound with boost") { + test("range (bounds, boost)") { val query = range(field = "customer_age").gte(10).lt(100).boost(1.0) val expected = """ @@ -1845,7 +1786,105 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query.toJson)(equalTo(expected.toJson)) }, - test("properly encode Wildcard query") { + test("term") { + val query = term(field = TestDocument.stringField, value = "test") + val queryWithBoost = term(field = TestDocument.stringField, value = "test").boost(10.21) + val queryWithCaseInsensitive = term(field = TestDocument.stringField, value = "test").caseInsensitiveTrue + val queryWithAllParams = + term(field = TestDocument.stringField, value = "test").boost(3.14).caseInsensitiveFalse + + val expected = + """ + |{ + | "query": { + | "term": { + | "stringField": { + | "value": "test" + | } + | } + | } + |} + |""".stripMargin + + val expectedWithBoost = + """ + |{ + | "query": { + | "term": { + | "stringField": { + | "value": "test", + | "boost": 10.21 + | } + | } + | } + |} + |""".stripMargin + + val expectedWithCaseInsensitive = + """ + |{ + | "query": { + | "term": { + | "stringField": { + | "value": "test", + | "case_insensitive": true + | } + | } + | } + |} + |""".stripMargin + + val expectedWithAllParams = + """ + |{ + | "query": { + | "term": { + | "stringField": { + | "value": "test", + | "boost": 3.14, + | "case_insensitive": false + | } + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) && + assert(queryWithBoost.toJson)(equalTo(expectedWithBoost.toJson)) && + assert(queryWithCaseInsensitive.toJson)(equalTo(expectedWithCaseInsensitive.toJson)) && + assert(queryWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson)) + }, + test("terms") { + val query = terms(field = TestDocument.stringField, values = "a", "b", "c") + val queryWithBoost = terms(field = TestDocument.stringField, values = "a", "b", "c").boost(10.21) + + val expected = + """ + |{ + | "query": { + | "terms": { + | "stringField": [ "a", "b", "c" ] + | } + | } + |} + |""".stripMargin + + val expectedWithBoost = + """ + |{ + | "query": { + | "terms": { + | "stringField": [ "a", "b", "c" ], + | "boost": 10.21 + | } + | } + |} + |""".stripMargin + + assert(query.toJson)(equalTo(expected.toJson)) && + assert(queryWithBoost.toJson)(equalTo(expectedWithBoost.toJson)) + }, + test("wildcard") { val query1 = contains(field = "day_of_week", value = "M") val query2 = startsWith(field = "day_of_week", value = "M") val query3 = wildcard(field = "day_of_week", value = "M*") @@ -1878,7 +1917,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query2.toJson)(equalTo(expected23.toJson)) && assert(query3.toJson)(equalTo(expected23.toJson)) }, - test("properly encode Wildcard query with boost") { + test("wildcard (boost)") { val query1 = contains(field = "day_of_week", value = "M").boost(1.0) val query2 = startsWith(field = "day_of_week", value = "M").boost(1.0) val query3 = wildcard(field = "day_of_week", value = "M*").boost(1.0) @@ -1913,7 +1952,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query2.toJson)(equalTo(expected23.toJson)) && assert(query3.toJson)(equalTo(expected23.toJson)) }, - test("properly encode case insensitive Wildcard query") { + test("wildcard (case insensitive)") { val query1 = contains(field = "day_of_week", value = "M").caseInsensitiveTrue val query2 = startsWith(field = "day_of_week", value = "M").caseInsensitiveTrue val query3 = wildcard(field = "day_of_week", value = "M*").caseInsensitiveTrue @@ -1948,7 +1987,7 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query2.toJson)(equalTo(expected23.toJson)) && assert(query3.toJson)(equalTo(expected23.toJson)) }, - test("properly encode case insensitive Wildcard query with boost") { + test("wildcard (case insensitive, boost)") { val query1 = contains(field = "day_of_week", value = "M").boost(1.0).caseInsensitiveTrue val query2 = startsWith(field = "day_of_week", value = "M").boost(1.0).caseInsensitiveTrue val query3 = wildcard(field = "day_of_week", value = "M*").boost(1.0).caseInsensitiveTrue @@ -1984,46 +2023,6 @@ object QueryDSLSpec extends ZIOSpecDefault { assert(query1.toJson)(equalTo(expected1.toJson)) && assert(query2.toJson)(equalTo(expected23.toJson)) && assert(query3.toJson)(equalTo(expected23.toJson)) - }, - test("bulk") { - val bulkQuery = IndexName.make("users").map { index => - val nestedField = TestNestedField("NestedField", 1) - val subDoc = TestSubDocument( - stringField = "StringField", - nestedField = nestedField, - intField = 100, - intFieldList = Nil - ) - val req1 = - ElasticRequest - .create[TestSubDocument](index, DocumentId("ETux1srpww2ObCx"), subDoc.copy(intField = 65)) - .routing(unsafeWrap(subDoc.stringField)(Routing)) - val req2 = - ElasticRequest.create[TestSubDocument](index, subDoc).routing(unsafeWrap(subDoc.stringField)(Routing)) - val req3 = ElasticRequest - .upsert[TestSubDocument](index, DocumentId("yMyEG8iFL5qx"), subDoc.copy(stringField = "StringField2")) - .routing(unsafeWrap(subDoc.stringField)(Routing)) - val req4 = - ElasticRequest - .deleteById(index, DocumentId("1VNzFt2XUFZfXZheDc")) - .routing(unsafeWrap(subDoc.stringField)(Routing)) - ElasticRequest.bulk(req1, req2, req3, req4) match { - case r: Bulk => Some(r.body) - case _ => None - } - } - - val expectedBody = - """|{ "create" : { "_index" : "users", "_id" : "ETux1srpww2ObCx", "routing" : "StringField" } } - |{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":65,"intFieldList":[]} - |{ "create" : { "_index" : "users", "routing" : "StringField" } } - |{"stringField":"StringField","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]} - |{ "index" : { "_index" : "users", "_id" : "yMyEG8iFL5qx", "routing" : "StringField" } } - |{"stringField":"StringField2","nestedField":{"stringField":"NestedField","longField":1},"intField":100,"intFieldList":[]} - |{ "delete" : { "_index" : "users", "_id" : "1VNzFt2XUFZfXZheDc", "routing" : "StringField" } } - |""".stripMargin - - assert(bulkQuery)(equalTo(Validation.succeed(Some(expectedBody)))) } ) )