Skip to content

Commit

Permalink
Create public methods for script and inner hits (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya authored May 18, 2023
1 parent 8e0a44f commit e4d9c87
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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))
)
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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) =>
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package zio.elasticsearch.script.options

import zio.elasticsearch.script.ScriptLang

private[elasticsearch] trait HasLang[S <: HasLang[S]] {

/**
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
)
Expand All @@ -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")
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,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)(
Expand Down Expand Up @@ -794,7 +794,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))
)
)
)
Expand Down Expand Up @@ -2027,11 +2027,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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand Down Expand Up @@ -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)

Expand Down
25 changes: 17 additions & 8 deletions modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -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)
)
)
)
}
),
Expand Down Expand Up @@ -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 =
"""
|{
Expand All @@ -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 =
"""
|{
Expand Down

0 comments on commit e4d9c87

Please sign in to comment.