Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Create public methods for script and inner hits #216

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
)
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.from("doc['intField'].value").lang("painless"), NumberType).order(Asc))
)
.documentAs[TestDocument]
} yield assert(res)(
equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField))
)
Expand Down Expand Up @@ -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']").params("factor" -> 100)
)
req7 =
ElasticRequest
Expand Down Expand Up @@ -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']").params("factor" -> factor)
)
)
doc <- Executor.execute(ElasticRequest.getById(index, documentId)).documentAs[TestDocument]
Expand All @@ -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']").params("factor" -> 2)
)
.orCreate(document)
)
Expand All @@ -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']").params("str" -> stringField)
)
.refreshTrue
)
doc <- Executor.execute(ElasticRequest.getById(updateByQueryIndex, documentId)).documentAs[TestDocument]
} yield assert(updateRes)(
equalTo(
Expand All @@ -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
)
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,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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed? Not sorted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not make sense to me when creating new InnerHits to say just InnerHits.name(...), withName made more sense. I would consider doing same for form and size. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are trying to avoid with prefix. @arnoldlacko?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could drop all these and just have InnerHits.empty.name(...).from(...)?

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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[elasticsearch] (
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 =
Expand Down Expand Up @@ -57,6 +57,6 @@ private[elasticsearch] final case class Script(
}

object Script {
def apply(source: String): Script =
def from(source: String): Script =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use from instead of apply?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We agreed earlier that overriding apply method is not something we want to be doing as it makes false feeling that Script has only one field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider source as a required param. And if we have Script("source") then we can consider it as a wrapper on the string. I would use apply.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I will change it to apply.

Script(source = source, params = Map.empty, lang = None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ 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")
)

assert(aggregation1)(
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 Expand Up @@ -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")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)(
Expand Down Expand Up @@ -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))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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']").params("factor" -> 2)
).orCreate[TestDocument](
TestDocument(
stringField = "stringField",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']").params("factor" -> 2)
)
.orCreate(doc = secondDoc)
.routing(Routing("routing"))
Expand All @@ -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
Expand All @@ -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"))
Expand Down
31 changes: 20 additions & 11 deletions modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ 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']").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("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']").params("factor" -> 2).lang("painless"))(
equalTo(
Script(
source = "doc['day_of_week'].value * params['factor']",
params = Map("factor" -> 2),
lang = Some("painless")
)
)
)
}
),
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 =
"""
|{
Expand All @@ -44,7 +52,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']").params("factor" -> 2)
val expected =
"""
|{
Expand All @@ -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.from("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.from("doc['day_of_week'].value * params['factor']").params("factor" -> 2).lang("painless")
val expected =
"""
|{
Expand Down