Skip to content

Commit

Permalink
Create public methods for script and inner hits
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya committed May 11, 2023
1 parent dca91d0 commit fdcb9ed
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import zio.elasticsearch.ElasticAggregation._
import zio.elasticsearch.ElasticHighlight.highlight
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticSort.sortBy
import zio.elasticsearch.ElasticUtilities.script
import zio.elasticsearch.domain.{PartialTestDocument, TestDocument, TestSubDocument}
import zio.elasticsearch.executor.Executor
import zio.elasticsearch.executor.response.{CardinalityAggregationResponse, MaxAggregationResponse}
Expand All @@ -29,13 +30,12 @@ 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.json.ast.Json.{Arr, Str}
import zio.schema.codec.JsonCodec
import zio.stream.{Sink, ZSink}
import zio.test._
import zio.test.TestAspect._
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._

import java.time.LocalDate
import scala.util.Random
Expand Down Expand Up @@ -1102,7 +1102,7 @@ object HttpExecutorSpec extends IntegrationSpec {
.execute(
ElasticRequest
.search(firstSearchIndex, query)
.sort(sortBy(Script("doc['intField'].value").lang("painless"), NumberType).order(Asc))
.sort(sortBy(script("doc['intField'].value").lang("painless"), NumberType).order(Asc))
)
.documentAs[TestDocument]
} yield assert(res)(
Expand Down Expand Up @@ -1459,7 +1459,7 @@ object HttpExecutorSpec extends IntegrationSpec {
req6 = ElasticRequest.updateByScript(
index,
firstDocumentId,
Script("ctx._source.intField = params['factor']").withParams("factor" -> 100)
script("ctx._source.intField = params['factor']").withParams("factor" -> 100)
)
req7 =
ElasticRequest
Expand Down Expand Up @@ -1493,7 +1493,7 @@ object HttpExecutorSpec extends IntegrationSpec {
ElasticRequest.updateByScript(
index,
documentId,
Script("ctx._source.intField += params['factor']").withParams("factor" -> factor)
script("ctx._source.intField += params['factor']").withParams("factor" -> factor)
)
)
doc <- Executor.execute(ElasticRequest.getById(index, documentId)).documentAs[TestDocument]
Expand All @@ -1508,7 +1508,7 @@ object HttpExecutorSpec extends IntegrationSpec {
.updateByScript(
index,
documentId,
Script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
)
.orCreate(document)
)
Expand Down Expand Up @@ -1539,7 +1539,7 @@ object HttpExecutorSpec extends IntegrationSpec {
ElasticRequest
.updateAllByQuery(
updateByQueryIndex,
Script("ctx._source['stringField'] = params['str']").withParams("str" -> stringField)
script("ctx._source['stringField'] = params['str']").withParams("str" -> stringField)
)
.refreshTrue
)
Expand All @@ -1565,7 +1565,7 @@ object HttpExecutorSpec extends IntegrationSpec {
.updateByQuery(
index = updateByQueryIndex,
query = term(field = TestDocument.stringField.keyword, value = "StringField"),
script = Script("ctx._source['intField']++")
script = script("ctx._source['intField']++")
)
.refreshTrue
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 LambdaWorks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package zio.elasticsearch

import zio.elasticsearch.query.InnerHits
import zio.elasticsearch.script.Script

object ElasticUtilities {

/**
* Constructs an empty instance of [[zio.elasticsearch.query.InnerHits]].
*
* @return
* an instance of [[zio.elasticsearch.query.InnerHits]].
*/
def innerHits(): InnerHits = InnerHits()

/**
* Constructs a instance of [[zio.elasticsearch.script.Script]] using the source parameter.
*
* @param source
* sets `source` parameter for [[zio.elasticsearch.script.Script]]
* @return
* an instance of [[zio.elasticsearch.script.Script]].
*/
def script(source: String): Script = Script(source = source, params = Map.empty, lang = None)

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,3 @@ private[elasticsearch] final case class InnerHits(
List(from.map("from" -> Num(_)), size.map("size" -> Num(_)), name.map("name" -> Str(_))).flatten: _*
)
}

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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,3 @@ private[elasticsearch] final case class Script(
).flatten: _*
)
}

object Script {
def apply(source: String): Script =
Script(source = source, params = Map.empty, lang = None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package zio.elasticsearch

import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticRequest.Bulk
import zio.elasticsearch.ElasticUtilities.innerHits
import zio.elasticsearch.domain._
import zio.elasticsearch.query._
import zio.elasticsearch.utils._
Expand Down Expand Up @@ -492,11 +493,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 @@ -1832,11 +1833,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 @@ -5,9 +5,9 @@ import zio.elasticsearch.ElasticHighlight.highlight
import zio.elasticsearch.ElasticQuery.term
import zio.elasticsearch.ElasticRequest._
import zio.elasticsearch.ElasticSort.sortBy
import zio.elasticsearch.ElasticUtilities.script
import zio.elasticsearch.domain.TestDocument
import zio.elasticsearch.query.sort.Missing.First
import zio.elasticsearch.script.Script
import zio.elasticsearch.utils.RichString
import zio.json.ast.Json
import zio.json.ast.Json.{Arr, Str}
Expand Down Expand Up @@ -299,7 +299,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
val jsonRequest = updateByQuery(
index = Index,
query = term(TestDocument.stringField.keyword, "StringField"),
script = Script("ctx._source['intField']++")
script = script("ctx._source['intField']++")
) match { case r: UpdateByQuery => r.toJson }

val expected =
Expand All @@ -324,7 +324,7 @@ object ElasticRequestDSLSpec extends ZIOSpecDefault {
val jsonRequest = updateByScript(
index = Index,
id = DocId,
script = Script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
script = script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
).orCreate[TestDocument](
TestDocument(
stringField = "stringField",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ package zio.elasticsearch

import zio.elasticsearch.ElasticAggregation.termsAggregation
import zio.elasticsearch.ElasticQuery.{matchAll, term}
import zio.elasticsearch.ElasticUtilities.script
import zio.elasticsearch.domain.TestDocument
import zio.elasticsearch.executor.Executor
import zio.elasticsearch.executor.response.{
BulkResponse,
CreateBulkResponse,
Shards,
TermsAggregationBucket,
TermsAggregationResponse
}
import zio.elasticsearch.executor.response._
import zio.elasticsearch.request.CreationOutcome.Created
import zio.elasticsearch.request.DeletionOutcome.Deleted
import zio.elasticsearch.request.UpdateConflicts.Proceed
import zio.elasticsearch.request.UpdateOutcome
import zio.elasticsearch.result.UpdateByQueryResult
import zio.elasticsearch.script.Script
import zio.test.Assertion._
import zio.test.{Spec, TestEnvironment, TestResultZIOOps, assertZIO}

Expand Down Expand Up @@ -210,7 +204,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
.updateByScript(
index = index,
id = DocumentId("V4x8q4UB3agN0z75fv5r"),
script = Script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
script = script("ctx._source.intField += params['factor']").withParams("factor" -> 2)
)
.orCreate(doc = secondDoc)
.routing(Routing("routing"))
Expand All @@ -233,7 +227,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
assertZIO(
Executor.execute(
ElasticRequest
.updateAllByQuery(index = index, script = Script("ctx._source['intField']++"))
.updateAllByQuery(index = index, script = script("ctx._source['intField']++"))
.conflicts(Proceed)
.routing(Routing("routing"))
.refreshTrue
Expand All @@ -247,7 +241,7 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
.updateByQuery(
index = index,
query = term(field = TestDocument.stringField.keyword, value = "StringField"),
script = Script("ctx._source['intField']++")
script = script("ctx._source['intField']++")
)
.conflicts(Proceed)
.routing(Routing("routing"))
Expand Down
26 changes: 14 additions & 12 deletions modules/library/src/test/scala/zio/elasticsearch/ScriptSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.elasticsearch

import zio.Scope
import zio.elasticsearch.ElasticUtilities.script
import zio.elasticsearch.script.Script
import zio.elasticsearch.utils.RichString
import zio.test.Assertion.equalTo
Expand All @@ -11,40 +12,40 @@ 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("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']").withParams("factor" -> 2))(
assert(script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2))(
equalTo(Script("doc['day_of_week'].value * params['factor']", Map("factor" -> 2), None))
)
},
test("successfully create Script with source and lang") {
assert(Script("doc['day_of_week'].value").lang("painless"))(
assert(script("doc['day_of_week'].value").lang("painless"))(
equalTo(Script("doc['day_of_week'].value", Map.empty, Some("painless")))
)
},
test("successfully create Script with source, params and lang") {
assert(Script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless"))(
assert(script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless"))(
equalTo(Script("doc['day_of_week'].value * params['factor']", Map("factor" -> 2), Some("painless")))
)
}
),
suite("encoding Script as JSON")(
test("properly encode Script with only source") {
val script = Script("doc['day_of_week'].value")
val scriptObject = script("doc['day_of_week'].value")
val expected =
"""
|{
| "source": "doc['day_of_week'].value"
|}
|""".stripMargin

assert(script.toJson)(equalTo(expected.toJson))
assert(scriptObject.toJson)(equalTo(expected.toJson))
}
),
suite("encoding Script as JSON")(
test("properly encode Script with source and params") {
val script = Script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2)
val scriptObject = script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2)
val expected =
"""
|{
Expand All @@ -55,10 +56,10 @@ object ScriptSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(script.toJson)(equalTo(expected.toJson))
assert(scriptObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Script with source and lang") {
val script = Script("doc['day_of_week'].value").lang("painless")
val scriptObject = script("doc['day_of_week'].value").lang("painless")
val expected =
"""
|{
Expand All @@ -67,10 +68,11 @@ object ScriptSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(script.toJson)(equalTo(expected.toJson))
assert(scriptObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Script with source, params and lang") {
val script = Script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless")
val scriptObject =
script("doc['day_of_week'].value * params['factor']").withParams("factor" -> 2).lang("painless")
val expected =
"""
|{
Expand All @@ -82,7 +84,7 @@ object ScriptSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(script.toJson)(equalTo(expected.toJson))
assert(scriptObject.toJson)(equalTo(expected.toJson))
}
)
)
Expand Down
Loading

0 comments on commit fdcb9ed

Please sign in to comment.