diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 895172881..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} @@ -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("doc['intField'].value").lang(Painless), NumberType).order(Asc)) + ) + .documentAs[TestDocument] } yield assert(res)( equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField)) ) @@ -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("ctx._source['stringField'] = params['str']").params("str" -> stringField) + ) + .refreshTrue + ) doc <- Executor.execute(ElasticRequest.getById(updateByQueryIndex, documentId)).documentAs[TestDocument] } yield assert(updateRes)( equalTo( 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..be59f2217 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[elasticsearch] ( + 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,5 @@ private[elasticsearch] final case class InnerHits( } object InnerHits { - def from(value: Int): InnerHits = - InnerHits(from = Some(value)) - - def name(value: String): InnerHits = - InnerHits(name = Some(value)) - - def size(value: Int): InnerHits = - InnerHits(size = Some(value)) + def apply(): InnerHits = InnerHits(from = None, name = None, size = None) } 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..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} -private[elasticsearch] final case class Script( - source: String, - params: Map[String, Any], - lang: Option[String] +final case class Script private[elasticsearch] ( + private val source: String, + private val params: Map[String, Any], + 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 @@ private[elasticsearch] final case class Script( 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) => @@ -60,3 +60,10 @@ object 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 47599d1d2..f43b304fd 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("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("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 7a9542de0..680d6f713 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -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.name("innerHitName")) + .innerHits(InnerHits().name("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(None, Some("innerHitName"), None)) ) ) ) @@ -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 = diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticSortSpec.scala index f512adc2e..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._ @@ -201,12 +201,12 @@ object ElasticSortSpec extends ZIOSpecDefault { test("sortByScript") { val sort = sortBy(script = Script("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("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("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("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), @@ -358,9 +358,10 @@ object ElasticSortSpec extends ZIOSpecDefault { sourceType = NumberType ).mode(Avg) val sortWithOrder = - sortBy(script = Script("doc['day_of_week'].value").lang("painless"), sourceType = NumberType).order(Desc) + sortBy(script = Script("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("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/ScriptSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala index c92a75e39..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._ @@ -15,17 +15,25 @@ object ScriptSpec extends ZIOSpecDefault { }, 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)) + 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("doc['day_of_week'].value").lang("painless"))( - equalTo(Script("doc['day_of_week'].value", Map.empty, Some("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("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("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) + ) + ) ) } ), @@ -58,7 +66,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("doc['day_of_week'].value").lang(Painless) val expected = """ |{ @@ -70,7 +78,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("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang(Painless) val expected = """ |{