From 1170da44a06bb5194a53c4a4c1be3c2973a77771 Mon Sep 17 00:00:00 2001 From: markaya Date: Wed, 17 May 2023 10:41:14 +0200 Subject: [PATCH 1/5] Rework Script And Inner Hits creating --- .../zio/elasticsearch/HttpExecutorSpec.scala | 40 ++++++++++--------- .../zio/elasticsearch/query/InnerHits.scala | 18 +++++---- .../query/options/HasInnerHits.scala | 2 +- .../zio/elasticsearch/script/Script.scala | 10 ++--- .../zio/elasticsearch/ElasticQuerySpec.scala | 8 ++-- .../elasticsearch/ElasticRequestDSLSpec.scala | 4 +- .../HttpElasticExecutorSpec.scala | 6 +-- .../scala/zio/elasticsearch/ScriptSpec.scala | 26 +++++++----- 8 files changed, 61 insertions(+), 53 deletions(-) diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 895172881..976c8e296 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -1152,13 +1152,14 @@ object HttpExecutorSpec extends IntegrationSpec { .refreshTrue ) query = range(TestDocument.intField).gte(20) - res <- Executor - .execute( - ElasticRequest - .search(firstSearchIndex, query) - .sort(sortBy(Script("doc['intField'].value").lang("painless"), NumberType).order(Asc)) - ) - .documentAs[TestDocument] + res <- + Executor + .execute( + ElasticRequest + .search(firstSearchIndex, query) + .sort(sortBy(Script.from("doc['intField'].value").lang("painless"), NumberType).order(Asc)) + ) + .documentAs[TestDocument] } yield assert(res)( equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField)) ) @@ -1513,7 +1514,7 @@ object HttpExecutorSpec extends IntegrationSpec { req6 = ElasticRequest.updateByScript( index, firstDocumentId, - Script("ctx._source.intField = params['factor']").params("factor" -> 100) + Script.from("ctx._source.intField = params['factor']").withParams("factor" -> 100) ) req7 = ElasticRequest @@ -1547,7 +1548,7 @@ object HttpExecutorSpec extends IntegrationSpec { ElasticRequest.updateByScript( index, documentId, - Script("ctx._source.intField += params['factor']").params("factor" -> factor) + Script.from("ctx._source.intField += params['factor']").withParams("factor" -> factor) ) ) doc <- Executor.execute(ElasticRequest.getById(index, documentId)).documentAs[TestDocument] @@ -1562,7 +1563,7 @@ object HttpExecutorSpec extends IntegrationSpec { .updateByScript( index, documentId, - Script("ctx._source.intField += params['factor']").params("factor" -> 2) + Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) ) .orCreate(document) ) @@ -1589,14 +1590,15 @@ object HttpExecutorSpec extends IntegrationSpec { _ <- Executor.execute( ElasticRequest.upsert[TestDocument](updateByQueryIndex, documentId, document).refreshTrue ) - updateRes <- Executor.execute( - ElasticRequest - .updateAllByQuery( - updateByQueryIndex, - Script("ctx._source['stringField'] = params['str']").params("str" -> stringField) - ) - .refreshTrue - ) + updateRes <- + Executor.execute( + ElasticRequest + .updateAllByQuery( + updateByQueryIndex, + Script.from("ctx._source['stringField'] = params['str']").withParams("str" -> stringField) + ) + .refreshTrue + ) doc <- Executor.execute(ElasticRequest.getById(updateByQueryIndex, documentId)).documentAs[TestDocument] } yield assert(updateRes)( equalTo( @@ -1619,7 +1621,7 @@ object HttpExecutorSpec extends IntegrationSpec { .updateByQuery( index = updateByQueryIndex, query = term(field = TestDocument.stringField.keyword, value = "StringField"), - script = Script("ctx._source['intField']++") + script = Script.from("ctx._source['intField']++") ) .refreshTrue ) diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala index b022098f7..6f5baf0e5 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala @@ -20,10 +20,10 @@ import zio.Chunk import zio.json.ast.Json import zio.json.ast.Json.{Num, Obj, Str} -private[elasticsearch] final case class InnerHits( - from: Option[Int] = None, - name: Option[String] = None, - size: Option[Int] = None +final case class InnerHits private ( + private val from: Option[Int], + private val name: Option[String], + private val size: Option[Int] ) { self => def from(value: Int): InnerHits = self.copy(from = Some(value)) @@ -41,12 +41,14 @@ private[elasticsearch] final case class InnerHits( } object InnerHits { + def empty: InnerHits = InnerHits(from = None, name = None, size = None) + def from(value: Int): InnerHits = - InnerHits(from = Some(value)) + InnerHits(from = Some(value), name = None, size = None) - def name(value: String): InnerHits = - InnerHits(name = Some(value)) + def withName(value: String): InnerHits = + InnerHits(from = None, name = Some(value), size = None) def size(value: Int): InnerHits = - InnerHits(size = Some(value)) + InnerHits(from = None, name = None, size = Some(value)) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala index 5e836f9b1..9e10e263d 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala @@ -26,7 +26,7 @@ private[elasticsearch] trait HasInnerHits[Q <: HasInnerHits[Q]] { * @return * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the default inner hits. */ - final def innerHits: Q = innerHits(InnerHits()) + final def innerHits: Q = innerHits(InnerHits.empty) /** * Sets the inner hits configuration for the [[zio.elasticsearch.query.NestedQuery]]. diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala index 0c3f6bfcb..8cbea69be 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala @@ -22,10 +22,10 @@ import zio.elasticsearch.script.options._ import zio.json.ast.Json import zio.json.ast.Json.Obj -private[elasticsearch] final case class Script( - source: String, - params: Map[String, Any], - lang: Option[String] +final case class Script private ( + private val source: String, + private val params: Map[String, Any], + private val lang: Option[String] ) extends HasLang[Script] with HasParams[Script] { self => def lang(value: String): Script = @@ -57,6 +57,6 @@ private[elasticsearch] final case class Script( } object Script { - def apply(source: String): Script = + def from(source: String): Script = Script(source = source, params = Map.empty, lang = None) } diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 7a9542de0..73f92ed43 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -533,7 +533,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { childType = "child", query = matchAll, ignoreUnmapped = None, - innerHitsField = Some(InnerHits()), + innerHitsField = Some(InnerHits.empty), maxChildren = None, minChildren = None, scoreMode = None @@ -581,7 +581,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { childType = "child", query = matchAll, ignoreUnmapped = Some(true), - innerHitsField = Some(InnerHits()), + innerHitsField = Some(InnerHits.empty), maxChildren = Some(5), minChildren = Some(1), scoreMode = Some(ScoreMode.Avg) @@ -720,7 +720,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { val queryWithInnerHitsEmpty = nested(TestDocument.subDocumentList, matchAll).innerHits val queryWithScoreMode = nested(TestDocument.subDocumentList, matchAll).scoreMode(ScoreMode.Avg) val queryWithAllParams = nested(TestDocument.subDocumentList, matchAll).ignoreUnmappedFalse - .innerHits(InnerHits.name("innerHitName")) + .innerHits(InnerHits.withName("innerHitName")) .scoreMode(ScoreMode.Max) assert(query)( @@ -796,7 +796,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { query = MatchAll(boost = None), scoreMode = Some(ScoreMode.Max), ignoreUnmapped = Some(false), - innerHitsField = Some(InnerHits(name = Some("innerHitName"))) + innerHitsField = Some(InnerHits.withName("innerHitName")) ) ) ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala index b4434cad4..68c2541fa 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala @@ -301,7 +301,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault { val jsonRequest = updateByQuery( index = Index, query = term(TestDocument.stringField.keyword, "StringField"), - script = Script("ctx._source['intField']++") + script = Script.from("ctx._source['intField']++") ) match { case r: UpdateByQuery => r.toJson } val expected = @@ -326,7 +326,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault { val jsonRequest = updateByScript( index = Index, id = DocId, - script = Script("ctx._source.intField += params['factor']").params("factor" -> 2) + script = Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) ).orCreate[TestDocument]( TestDocument( stringField = "stringField", diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 810066662..8c5401f3b 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -211,7 +211,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .updateByScript( index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), - script = Script("ctx._source.intField += params['factor']").params("factor" -> 2) + script = Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) ) .orCreate(doc = secondDoc) .routing(Routing("routing")) @@ -234,7 +234,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { assertZIO( Executor.execute( ElasticRequest - .updateAllByQuery(index = index, script = Script("ctx._source['intField']++")) + .updateAllByQuery(index = index, script = Script.from("ctx._source['intField']++")) .conflicts(Proceed) .routing(Routing("routing")) .refreshTrue @@ -248,7 +248,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .updateByQuery( index = index, query = term(field = TestDocument.stringField.keyword, value = "StringField"), - script = Script("ctx._source['intField']++") + script = Script.from("ctx._source['intField']++") ) .conflicts(Proceed) .routing(Routing("routing")) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala index c92a75e39..be7edf196 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala @@ -11,27 +11,30 @@ object ScriptSpec extends ZIOSpecDefault { suite("Script")( suite("Creating script")( test("successfully create Script with only source") { - assert(Script("doc['day_of_week'].value"))(equalTo(Script("doc['day_of_week'].value", Map.empty, None))) + assert(Script.from("doc['day_of_week'].value"))(equalTo(Script("doc['day_of_week'].value", Map.empty, None))) }, test("successfully create Script with source and params") { - assert(Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2))( - equalTo(Script("doc['day_of_week'].value * params['factor']", Map("factor" -> 2), None)) + assert(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2))( + equalTo(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2)) ) }, test("successfully create Script with source and lang") { - assert(Script("doc['day_of_week'].value").lang("painless"))( - equalTo(Script("doc['day_of_week'].value", Map.empty, Some("painless"))) + assert(Script.from("doc['day_of_week'].value").lang("painless"))( + equalTo(Script.from("doc['day_of_week'].value").lang("painless")) ) }, test("successfully create Script with source, params and lang") { - assert(Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"))( - equalTo(Script("doc['day_of_week'].value * params['factor']", Map("factor" -> 2), Some("painless"))) + assert(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless"))( + equalTo( + Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless") + ) + HttpExecutorSpec.scala ) } ), suite("encoding Script as JSON")( test("properly encode Script with only source") { - val script = Script("doc['day_of_week'].value") + val script = Script.from("doc['day_of_week'].value") val expected = """ |{ @@ -44,7 +47,7 @@ object ScriptSpec extends ZIOSpecDefault { ), suite("encoding Script as JSON")( test("properly encode Script with source and params") { - val script = Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2) + val script = Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2) val expected = """ |{ @@ -58,7 +61,7 @@ object ScriptSpec extends ZIOSpecDefault { assert(script.toJson)(equalTo(expected.toJson)) }, test("properly encode Script with source and lang") { - val script = Script("doc['day_of_week'].value").lang("painless") + val script = Script.from("doc['day_of_week'].value").lang("painless") val expected = """ |{ @@ -70,7 +73,8 @@ object ScriptSpec extends ZIOSpecDefault { assert(script.toJson)(equalTo(expected.toJson)) }, test("properly encode Script with source, params and lang") { - val script = Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless") + val script = + Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless") val expected = """ |{ From 7d3812523853835ceaf19c217ee6925ccd7d6d67 Mon Sep 17 00:00:00 2001 From: markaya Date: Wed, 17 May 2023 11:11:57 +0200 Subject: [PATCH 2/5] Rebase and resolve conflicts --- .../zio/elasticsearch/HttpExecutorSpec.scala | 10 +++++----- .../elasticsearch/ElasticAggregationSpec.scala | 12 ++++++------ .../elasticsearch/ElasticRequestDSLSpec.scala | 2 +- .../zio/elasticsearch/ElasticSortSpec.scala | 17 +++++++++-------- .../elasticsearch/HttpElasticExecutorSpec.scala | 2 +- .../scala/zio/elasticsearch/ScriptSpec.scala | 13 ++++++------- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 976c8e296..0046bd0f8 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -348,7 +348,7 @@ object HttpExecutorSpec extends IntegrationSpec { .withSubAgg( bucketSelectorAggregation( name = "aggregationSelector", - script = Script("params.aggregation_int > 10"), + script = Script.from("params.aggregation_int > 10"), bucketsPath = Map("aggregation_int" -> "aggregationInt") ) ) @@ -1514,7 +1514,7 @@ object HttpExecutorSpec extends IntegrationSpec { req6 = ElasticRequest.updateByScript( index, firstDocumentId, - Script.from("ctx._source.intField = params['factor']").withParams("factor" -> 100) + Script.from("ctx._source.intField = params['factor']").params("factor" -> 100) ) req7 = ElasticRequest @@ -1548,7 +1548,7 @@ object HttpExecutorSpec extends IntegrationSpec { ElasticRequest.updateByScript( index, documentId, - Script.from("ctx._source.intField += params['factor']").withParams("factor" -> factor) + Script.from("ctx._source.intField += params['factor']").params("factor" -> factor) ) ) doc <- Executor.execute(ElasticRequest.getById(index, documentId)).documentAs[TestDocument] @@ -1563,7 +1563,7 @@ object HttpExecutorSpec extends IntegrationSpec { .updateByScript( index, documentId, - Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) + Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) ) .orCreate(document) ) @@ -1595,7 +1595,7 @@ object HttpExecutorSpec extends IntegrationSpec { ElasticRequest .updateAllByQuery( updateByQueryIndex, - Script.from("ctx._source['stringField'] = params['str']").withParams("str" -> stringField) + Script.from("ctx._source['stringField'] = params['str']").params("str" -> stringField) ) .refreshTrue ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala index 47599d1d2..b96df3ecf 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala @@ -18,12 +18,12 @@ object ElasticAggregationSpec extends ZIOSpecDefault { test("bucketSelector") { val aggregation1 = bucketSelectorAggregation( name = "aggregation", - script = Script("params.agg1 > 10"), + script = Script.from("params.agg1 > 10"), bucketsPath = Map("agg1" -> "aggregation1") ) val aggregation2 = bucketSelectorAggregation( name = "aggregation", - script = Script("params.agg1 + params.agg2 > 10"), + script = Script.from("params.agg1 + params.agg2 > 10"), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) @@ -31,7 +31,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault { equalTo( BucketSelector( name = "aggregation", - script = Script("params.agg1 > 10"), + script = Script.from("params.agg1 > 10"), bucketsPath = Map("agg1" -> "aggregation1") ) ) @@ -40,7 +40,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault { equalTo( BucketSelector( name = "aggregation", - script = Script("params.agg1 + params.agg2 > 10"), + script = Script.from("params.agg1 + params.agg2 > 10"), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) ) @@ -332,12 +332,12 @@ object ElasticAggregationSpec extends ZIOSpecDefault { test("bucketSelector") { val aggregation1 = bucketSelectorAggregation( name = "aggregation", - script = Script("params.agg1 > 10"), + script = Script.from("params.agg1 > 10"), bucketsPath = Map("agg1" -> "aggregation1") ) val aggregation2 = bucketSelectorAggregation( name = "aggregation", - script = Script("params.agg1 + params.agg2 > 10"), + script = Script.from("params.agg1 + params.agg2 > 10"), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala index 68c2541fa..a6973ad1f 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala @@ -326,7 +326,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault { val jsonRequest = updateByScript( index = Index, id = DocId, - script = Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) + script = Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) ).orCreate[TestDocument]( TestDocument( stringField = "stringField", diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala index f512adc2e..bfc2a8f14 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala @@ -199,14 +199,14 @@ object ElasticSortSpec extends ZIOSpecDefault { ) }, test("sortByScript") { - val sort = sortBy(script = Script("doc['day_of_week'].value"), sourceType = NumberType) + val sort = sortBy(script = Script.from("doc['day_of_week'].value"), sourceType = NumberType) val sortWithMode = - sortBy(Script(source = "doc['day_of_week'].value * params['factor']").params("factor" -> 2), NumberType) + sortBy(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2), NumberType) .mode(Avg) val sortWithOrder = - sortBy(Script(source = "doc['day_of_week'].value").lang("painless"), NumberType).order(Desc) + sortBy(Script.from("doc['day_of_week'].value").lang("painless"), NumberType).order(Desc) val sortWithModeAndOrder = sortBy( - Script(source = "doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), + Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), NumberType ).mode(Avg).order(Asc) @@ -352,15 +352,16 @@ object ElasticSortSpec extends ZIOSpecDefault { assert(sortWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson)) }, test("sortByScript") { - val sort = sortBy(script = Script("doc['day_of_week'].value"), sourceType = NumberType) + val sort = sortBy(script = Script.from("doc['day_of_week'].value"), sourceType = NumberType) val sortWithMode = sortBy( - script = Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2), + script = Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2), sourceType = NumberType ).mode(Avg) val sortWithOrder = - sortBy(script = Script("doc['day_of_week'].value").lang("painless"), sourceType = NumberType).order(Desc) + sortBy(script = Script.from("doc['day_of_week'].value").lang("painless"), sourceType = NumberType) + .order(Desc) val sortWithModeAndOrder = sortBy( - Script(source = "doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), + Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), NumberType ).mode(Avg).order(Asc) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 8c5401f3b..d78f0f5d3 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -211,7 +211,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .updateByScript( index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), - script = Script.from("ctx._source.intField += params['factor']").withParams("factor" -> 2) + script = Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) ) .orCreate(doc = secondDoc) .routing(Routing("routing")) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala index be7edf196..2ec582fe8 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala @@ -14,8 +14,8 @@ object ScriptSpec extends ZIOSpecDefault { assert(Script.from("doc['day_of_week'].value"))(equalTo(Script("doc['day_of_week'].value", Map.empty, None))) }, test("successfully create Script with source and params") { - assert(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2))( - equalTo(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2)) + assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2))( + equalTo(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2)) ) }, test("successfully create Script with source and lang") { @@ -24,11 +24,10 @@ object ScriptSpec extends ZIOSpecDefault { ) }, test("successfully create Script with source, params and lang") { - assert(Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless"))( + assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"))( equalTo( - Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless") + Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless") ) - HttpExecutorSpec.scala ) } ), @@ -47,7 +46,7 @@ object ScriptSpec extends ZIOSpecDefault { ), suite("encoding Script as JSON")( test("properly encode Script with source and params") { - val script = Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2) + val script = Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2) val expected = """ |{ @@ -74,7 +73,7 @@ object ScriptSpec extends ZIOSpecDefault { }, test("properly encode Script with source, params and lang") { val script = - Script.from("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless") + Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless") val expected = """ |{ From 27ab4a58fecefb56e3e0ebbadc712773fec28674 Mon Sep 17 00:00:00 2001 From: markaya Date: Wed, 17 May 2023 11:35:24 +0200 Subject: [PATCH 3/5] Fix tests --- .../main/scala/zio/elasticsearch/query/InnerHits.scala | 2 +- .../main/scala/zio/elasticsearch/script/Script.scala | 2 +- .../zio/elasticsearch/ElasticAggregationSpec.scala | 4 ++-- .../scala/zio/elasticsearch/ElasticQuerySpec.scala | 2 +- .../src/test/scala/zio/elasticsearch/ScriptSpec.scala | 10 ++++++++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala index 6f5baf0e5..c5c872f06 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala @@ -20,7 +20,7 @@ import zio.Chunk import zio.json.ast.Json import zio.json.ast.Json.{Num, Obj, Str} -final case class InnerHits private ( +final case class InnerHits private[elasticsearch] ( private val from: Option[Int], private val name: Option[String], private val size: Option[Int] diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala index 8cbea69be..d901ec205 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala @@ -22,7 +22,7 @@ import zio.elasticsearch.script.options._ import zio.json.ast.Json import zio.json.ast.Json.Obj -final case class Script private ( +final case class Script private[elasticsearch] ( private val source: String, private val params: Map[String, Any], private val lang: Option[String] diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala index b96df3ecf..cf9dc71d3 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala @@ -31,7 +31,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault { equalTo( BucketSelector( name = "aggregation", - script = Script.from("params.agg1 > 10"), + script = Script(source = "params.agg1 > 10", params = Map.empty, lang = None), bucketsPath = Map("agg1" -> "aggregation1") ) ) @@ -40,7 +40,7 @@ object ElasticAggregationSpec extends ZIOSpecDefault { equalTo( BucketSelector( name = "aggregation", - script = Script.from("params.agg1 + params.agg2 > 10"), + script = Script(source = "params.agg1 + params.agg2 > 10", params = Map.empty, lang = None), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 73f92ed43..24c622eb7 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -796,7 +796,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { query = MatchAll(boost = None), scoreMode = Some(ScoreMode.Max), ignoreUnmapped = Some(false), - innerHitsField = Some(InnerHits.withName("innerHitName")) + innerHitsField = Some(InnerHits(None, Some("innerHitName"), None)) ) ) ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala index 2ec582fe8..c3e2b7885 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala @@ -15,7 +15,9 @@ object ScriptSpec extends ZIOSpecDefault { }, test("successfully create Script with source and params") { assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2))( - equalTo(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2)) + equalTo( + Script(source = "doc['day_of_week'].value * params['factor']", params = Map("factor" -> 2), lang = None) + ) ) }, test("successfully create Script with source and lang") { @@ -26,7 +28,11 @@ object ScriptSpec extends ZIOSpecDefault { test("successfully create Script with source, params and lang") { assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"))( equalTo( - Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless") + Script( + source = "doc['day_of_week'].value * params['factor']", + params = Map("factor" -> 2), + lang = Some("painless") + ) ) ) } From 5a97d9fff42dff1e97386955ca69710c2fafacbf Mon Sep 17 00:00:00 2001 From: markaya Date: Thu, 18 May 2023 08:36:58 +0200 Subject: [PATCH 4/5] Fix code remarks --- .../zio/elasticsearch/HttpExecutorSpec.scala | 16 +++++++------- .../zio/elasticsearch/script/Script.scala | 17 +++++++++----- .../script/options/HasLang.scala | 4 +++- .../ElasticAggregationSpec.scala | 8 +++---- .../elasticsearch/ElasticRequestDSLSpec.scala | 4 ++-- .../zio/elasticsearch/ElasticSortSpec.scala | 22 +++++++++---------- .../HttpElasticExecutorSpec.scala | 6 ++--- .../scala/zio/elasticsearch/ScriptSpec.scala | 22 +++++++++---------- 8 files changed, 54 insertions(+), 45 deletions(-) diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 0046bd0f8..77abe2a1f 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -35,7 +35,7 @@ import zio.elasticsearch.query.sort.SortOrder._ import zio.elasticsearch.query.sort.SourceType.NumberType import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome} import zio.elasticsearch.result.{Item, UpdateByQueryResult} -import zio.elasticsearch.script.Script +import zio.elasticsearch.script.{Painless, Script} import zio.json.ast.Json.{Arr, Str} import zio.schema.codec.JsonCodec import zio.stream.{Sink, ZSink} @@ -348,7 +348,7 @@ object HttpExecutorSpec extends IntegrationSpec { .withSubAgg( bucketSelectorAggregation( name = "aggregationSelector", - script = Script.from("params.aggregation_int > 10"), + script = Script("params.aggregation_int > 10"), bucketsPath = Map("aggregation_int" -> "aggregationInt") ) ) @@ -1157,7 +1157,7 @@ object HttpExecutorSpec extends IntegrationSpec { .execute( ElasticRequest .search(firstSearchIndex, query) - .sort(sortBy(Script.from("doc['intField'].value").lang("painless"), NumberType).order(Asc)) + .sort(sortBy(Script("doc['intField'].value").lang(Painless), NumberType).order(Asc)) ) .documentAs[TestDocument] } yield assert(res)( @@ -1514,7 +1514,7 @@ object HttpExecutorSpec extends IntegrationSpec { req6 = ElasticRequest.updateByScript( index, firstDocumentId, - Script.from("ctx._source.intField = params['factor']").params("factor" -> 100) + Script("ctx._source.intField = params['factor']").params("factor" -> 100) ) req7 = ElasticRequest @@ -1548,7 +1548,7 @@ object HttpExecutorSpec extends IntegrationSpec { ElasticRequest.updateByScript( index, documentId, - Script.from("ctx._source.intField += params['factor']").params("factor" -> factor) + Script("ctx._source.intField += params['factor']").params("factor" -> factor) ) ) doc <- Executor.execute(ElasticRequest.getById(index, documentId)).documentAs[TestDocument] @@ -1563,7 +1563,7 @@ object HttpExecutorSpec extends IntegrationSpec { .updateByScript( index, documentId, - Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) + Script("ctx._source.intField += params['factor']").params("factor" -> 2) ) .orCreate(document) ) @@ -1595,7 +1595,7 @@ object HttpExecutorSpec extends IntegrationSpec { ElasticRequest .updateAllByQuery( updateByQueryIndex, - Script.from("ctx._source['stringField'] = params['str']").params("str" -> stringField) + Script("ctx._source['stringField'] = params['str']").params("str" -> stringField) ) .refreshTrue ) @@ -1621,7 +1621,7 @@ object HttpExecutorSpec extends IntegrationSpec { .updateByQuery( index = updateByQueryIndex, query = term(field = TestDocument.stringField.keyword, value = "StringField"), - script = Script.from("ctx._source['intField']++") + script = Script("ctx._source['intField']++") ) .refreshTrue ) diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala index d901ec205..c65a19bc8 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/Script.scala @@ -20,15 +20,15 @@ import zio.Chunk import zio.elasticsearch.ElasticPrimitive.ElasticPrimitiveOps import zio.elasticsearch.script.options._ import zio.json.ast.Json -import zio.json.ast.Json.Obj +import zio.json.ast.Json.{Obj, Str} final case class Script private[elasticsearch] ( private val source: String, private val params: Map[String, Any], - private val lang: Option[String] + private val lang: Option[ScriptLang] ) extends HasLang[Script] with HasParams[Script] { self => - def lang(value: String): Script = + def lang(value: ScriptLang): Script = self.copy(lang = Some(value)) def params(values: (String, Any)*): Script = @@ -37,7 +37,7 @@ final case class Script private[elasticsearch] ( private[elasticsearch] def toJson: Json = Obj( Chunk( - self.lang.map(lang => "lang" -> lang.toJson), + self.lang.map(lang => "lang" -> Str(lang.toString.toLowerCase)), Some("source" -> source.toJson), if (params.nonEmpty) { Some("params" -> Obj(Chunk.fromIterable(params).map { case (key, value) => @@ -57,6 +57,13 @@ final case class Script private[elasticsearch] ( } object Script { - def from(source: String): Script = + def apply(source: String): Script = Script(source = source, params = Map.empty, lang = None) } + +sealed trait ScriptLang + +case object Painless extends ScriptLang +case object Expression extends ScriptLang +case object Mustache extends ScriptLang +case object Java extends ScriptLang diff --git a/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala index 92700e754..3a2784431 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/script/options/HasLang.scala @@ -16,6 +16,8 @@ package zio.elasticsearch.script.options +import zio.elasticsearch.script.ScriptLang + private[elasticsearch] trait HasLang[S <: HasLang[S]] { /** @@ -26,5 +28,5 @@ private[elasticsearch] trait HasLang[S <: HasLang[S]] { * @return * an instance of the [[zio.elasticsearch.script.Script]] enriched with the `lang` parameter. */ - def lang(value: String): S + def lang(value: ScriptLang): S } diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala index cf9dc71d3..f43b304fd 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala @@ -18,12 +18,12 @@ object ElasticAggregationSpec extends ZIOSpecDefault { test("bucketSelector") { val aggregation1 = bucketSelectorAggregation( name = "aggregation", - script = Script.from("params.agg1 > 10"), + script = Script("params.agg1 > 10"), bucketsPath = Map("agg1" -> "aggregation1") ) val aggregation2 = bucketSelectorAggregation( name = "aggregation", - script = Script.from("params.agg1 + params.agg2 > 10"), + script = Script("params.agg1 + params.agg2 > 10"), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) @@ -332,12 +332,12 @@ object ElasticAggregationSpec extends ZIOSpecDefault { test("bucketSelector") { val aggregation1 = bucketSelectorAggregation( name = "aggregation", - script = Script.from("params.agg1 > 10"), + script = Script("params.agg1 > 10"), bucketsPath = Map("agg1" -> "aggregation1") ) val aggregation2 = bucketSelectorAggregation( name = "aggregation", - script = Script.from("params.agg1 + params.agg2 > 10"), + script = Script("params.agg1 + params.agg2 > 10"), bucketsPath = Map("agg1" -> "aggregation1", "agg2" -> "aggregation2") ) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala index a6973ad1f..b4434cad4 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticRequestDSLSpec.scala @@ -301,7 +301,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault { val jsonRequest = updateByQuery( index = Index, query = term(TestDocument.stringField.keyword, "StringField"), - script = Script.from("ctx._source['intField']++") + script = Script("ctx._source['intField']++") ) match { case r: UpdateByQuery => r.toJson } val expected = @@ -326,7 +326,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault { val jsonRequest = updateByScript( index = Index, id = DocId, - script = Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) + script = Script("ctx._source.intField += params['factor']").params("factor" -> 2) ).orCreate[TestDocument]( TestDocument( stringField = "stringField", diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala index bfc2a8f14..35a4e1f71 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala @@ -9,7 +9,7 @@ import zio.elasticsearch.query.sort.SortMode._ import zio.elasticsearch.query.sort.SortOrder._ import zio.elasticsearch.query.sort.SourceType.NumberType import zio.elasticsearch.query.sort._ -import zio.elasticsearch.script.Script +import zio.elasticsearch.script.{Painless, Script} import zio.elasticsearch.utils._ import zio.test.Assertion.equalTo import zio.test._ @@ -199,14 +199,14 @@ object ElasticSortSpec extends ZIOSpecDefault { ) }, test("sortByScript") { - val sort = sortBy(script = Script.from("doc['day_of_week'].value"), sourceType = NumberType) + val sort = sortBy(script = Script("doc['day_of_week'].value"), sourceType = NumberType) val sortWithMode = - sortBy(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2), NumberType) + sortBy(Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2), NumberType) .mode(Avg) val sortWithOrder = - sortBy(Script.from("doc['day_of_week'].value").lang("painless"), NumberType).order(Desc) + sortBy(Script("doc['day_of_week'].value").lang(Painless), NumberType).order(Desc) val sortWithModeAndOrder = sortBy( - Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), + Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang(Painless), NumberType ).mode(Avg).order(Asc) @@ -235,7 +235,7 @@ object ElasticSortSpec extends ZIOSpecDefault { ) && assert(sortWithOrder)( equalTo( SortByScriptOptions( - script = Script(source = "doc['day_of_week'].value", params = Map.empty, lang = Some("painless")), + script = Script(source = "doc['day_of_week'].value", params = Map.empty, lang = Some(Painless)), sourceType = NumberType, mode = None, order = Some(Desc) @@ -247,7 +247,7 @@ object ElasticSortSpec extends ZIOSpecDefault { script = Script( source = "doc['day_of_week'].value * params['factor']", params = Map("factor" -> 2), - lang = Some("painless") + lang = Some(Painless) ), sourceType = NumberType, mode = Some(Avg), @@ -352,16 +352,16 @@ object ElasticSortSpec extends ZIOSpecDefault { assert(sortWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson)) }, test("sortByScript") { - val sort = sortBy(script = Script.from("doc['day_of_week'].value"), sourceType = NumberType) + val sort = sortBy(script = Script("doc['day_of_week'].value"), sourceType = NumberType) val sortWithMode = sortBy( - script = Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2), + script = Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2), sourceType = NumberType ).mode(Avg) val sortWithOrder = - sortBy(script = Script.from("doc['day_of_week'].value").lang("painless"), sourceType = NumberType) + sortBy(script = Script("doc['day_of_week'].value").lang(Painless), sourceType = NumberType) .order(Desc) val sortWithModeAndOrder = sortBy( - Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"), + Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang(Painless), NumberType ).mode(Avg).order(Asc) diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index d78f0f5d3..810066662 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -211,7 +211,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .updateByScript( index = index, id = DocumentId("V4x8q4UB3agN0z75fv5r"), - script = Script.from("ctx._source.intField += params['factor']").params("factor" -> 2) + script = Script("ctx._source.intField += params['factor']").params("factor" -> 2) ) .orCreate(doc = secondDoc) .routing(Routing("routing")) @@ -234,7 +234,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { assertZIO( Executor.execute( ElasticRequest - .updateAllByQuery(index = index, script = Script.from("ctx._source['intField']++")) + .updateAllByQuery(index = index, script = Script("ctx._source['intField']++")) .conflicts(Proceed) .routing(Routing("routing")) .refreshTrue @@ -248,7 +248,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec { .updateByQuery( index = index, query = term(field = TestDocument.stringField.keyword, value = "StringField"), - script = Script.from("ctx._source['intField']++") + script = Script("ctx._source['intField']++") ) .conflicts(Proceed) .routing(Routing("routing")) diff --git a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala index c3e2b7885..92b68c6c6 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala @@ -1,7 +1,7 @@ package zio.elasticsearch import zio.Scope -import zio.elasticsearch.script.Script +import zio.elasticsearch.script.{Painless, Script} import zio.elasticsearch.utils.RichString import zio.test.Assertion.equalTo import zio.test._ @@ -11,27 +11,27 @@ object ScriptSpec extends ZIOSpecDefault { suite("Script")( suite("Creating script")( test("successfully create Script with only source") { - assert(Script.from("doc['day_of_week'].value"))(equalTo(Script("doc['day_of_week'].value", Map.empty, None))) + assert(Script("doc['day_of_week'].value"))(equalTo(Script("doc['day_of_week'].value", Map.empty, None))) }, test("successfully create Script with source and params") { - assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2))( + assert(Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2))( equalTo( Script(source = "doc['day_of_week'].value * params['factor']", params = Map("factor" -> 2), lang = None) ) ) }, test("successfully create Script with source and lang") { - assert(Script.from("doc['day_of_week'].value").lang("painless"))( - equalTo(Script.from("doc['day_of_week'].value").lang("painless")) + assert(Script("doc['day_of_week'].value").lang(Painless))( + equalTo(Script("doc['day_of_week'].value").lang(Painless)) ) }, test("successfully create Script with source, params and lang") { - assert(Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless"))( + assert(Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang(Painless))( equalTo( Script( source = "doc['day_of_week'].value * params['factor']", params = Map("factor" -> 2), - lang = Some("painless") + lang = Some(Painless) ) ) ) @@ -39,7 +39,7 @@ object ScriptSpec extends ZIOSpecDefault { ), suite("encoding Script as JSON")( test("properly encode Script with only source") { - val script = Script.from("doc['day_of_week'].value") + val script = Script("doc['day_of_week'].value") val expected = """ |{ @@ -52,7 +52,7 @@ object ScriptSpec extends ZIOSpecDefault { ), suite("encoding Script as JSON")( test("properly encode Script with source and params") { - val script = Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2) + val script = Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2) val expected = """ |{ @@ -66,7 +66,7 @@ object ScriptSpec extends ZIOSpecDefault { assert(script.toJson)(equalTo(expected.toJson)) }, test("properly encode Script with source and lang") { - val script = Script.from("doc['day_of_week'].value").lang("painless") + val script = Script("doc['day_of_week'].value").lang(Painless) val expected = """ |{ @@ -79,7 +79,7 @@ object ScriptSpec extends ZIOSpecDefault { }, test("properly encode Script with source, params and lang") { val script = - Script.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless") + Script("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang(Painless) val expected = """ |{ From 97e21433b8a2880334c23a34d19e786086c289c0 Mon Sep 17 00:00:00 2001 From: markaya Date: Thu, 18 May 2023 15:12:01 +0200 Subject: [PATCH 5/5] Use apply instead of empty --- .../scala/zio/elasticsearch/query/InnerHits.scala | 11 +---------- .../elasticsearch/query/options/HasInnerHits.scala | 2 +- .../scala/zio/elasticsearch/ElasticQuerySpec.scala | 12 ++++++------ 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala index c5c872f06..be59f2217 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/InnerHits.scala @@ -41,14 +41,5 @@ final case class InnerHits private[elasticsearch] ( } object InnerHits { - def empty: InnerHits = InnerHits(from = None, name = None, size = None) - - def from(value: Int): InnerHits = - InnerHits(from = Some(value), name = None, size = None) - - def withName(value: String): InnerHits = - InnerHits(from = None, name = Some(value), size = None) - - def size(value: Int): InnerHits = - InnerHits(from = None, name = None, size = Some(value)) + def apply(): InnerHits = InnerHits(from = None, name = None, size = None) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala index 9e10e263d..5e836f9b1 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/query/options/HasInnerHits.scala @@ -26,7 +26,7 @@ private[elasticsearch] trait HasInnerHits[Q <: HasInnerHits[Q]] { * @return * a new instance of the [[zio.elasticsearch.query.ElasticQuery]] with the default inner hits. */ - final def innerHits: Q = innerHits(InnerHits.empty) + final def innerHits: Q = innerHits(InnerHits()) /** * Sets the inner hits configuration for the [[zio.elasticsearch.query.NestedQuery]]. diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 24c622eb7..680d6f713 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -533,7 +533,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { childType = "child", query = matchAll, ignoreUnmapped = None, - innerHitsField = Some(InnerHits.empty), + innerHitsField = Some(InnerHits()), maxChildren = None, minChildren = None, scoreMode = None @@ -581,7 +581,7 @@ object ElasticQuerySpec extends ZIOSpecDefault { childType = "child", query = matchAll, ignoreUnmapped = Some(true), - innerHitsField = Some(InnerHits.empty), + innerHitsField = Some(InnerHits()), maxChildren = Some(5), minChildren = Some(1), scoreMode = Some(ScoreMode.Avg) @@ -716,11 +716,11 @@ object ElasticQuerySpec extends ZIOSpecDefault { val queryTs = nested(TestDocument.subDocumentList, matchAll) val queryWithIgnoreUnmapped = nested(TestDocument.subDocumentList, matchAll).ignoreUnmappedTrue val queryWithInnerHits = - nested(TestDocument.subDocumentList, matchAll).innerHits(InnerHits.from(0).name("innerHitName").size(3)) + nested(TestDocument.subDocumentList, matchAll).innerHits(InnerHits().from(0).name("innerHitName").size(3)) val queryWithInnerHitsEmpty = nested(TestDocument.subDocumentList, matchAll).innerHits val queryWithScoreMode = nested(TestDocument.subDocumentList, matchAll).scoreMode(ScoreMode.Avg) val queryWithAllParams = nested(TestDocument.subDocumentList, matchAll).ignoreUnmappedFalse - .innerHits(InnerHits.withName("innerHitName")) + .innerHits(InnerHits().name("innerHitName")) .scoreMode(ScoreMode.Max) assert(query)( @@ -2069,11 +2069,11 @@ object ElasticQuerySpec extends ZIOSpecDefault { val queryWithNested = nested(TestDocument.subDocumentList, nested("items", term("testField", "test"))) val queryWithIgnoreUnmapped = nested(TestDocument.subDocumentList, matchAll).ignoreUnmappedTrue val queryWithInnerHits = - nested(TestDocument.subDocumentList, matchAll).innerHits(InnerHits.from(0).size(3).name("innerHitName")) + nested(TestDocument.subDocumentList, matchAll).innerHits(InnerHits().from(0).size(3).name("innerHitName")) val queryWithInnerHitsEmpty = nested(TestDocument.subDocumentList, matchAll).innerHits val queryWithScoreMode = nested(TestDocument.subDocumentList, matchAll).scoreMode(ScoreMode.Avg) val queryWithAllParams = nested(TestDocument.subDocumentList, matchAll).ignoreUnmappedFalse - .innerHits(InnerHits.from(10).size(20).name("innerHitName")) + .innerHits(InnerHits().from(10).size(20).name("innerHitName")) .scoreMode(ScoreMode.Min) val expected =