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

(dsl): Support script query #232

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -19,7 +19,7 @@ package zio.elasticsearch
import zio.Chunk
import zio.elasticsearch.ElasticAggregation._
import zio.elasticsearch.ElasticHighlight.highlight
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticQuery.{script => _, _}
import zio.elasticsearch.ElasticSort.sortBy
import zio.elasticsearch.aggregation.AggregationOrder
import zio.elasticsearch.domain.{PartialTestDocument, TestDocument, TestSubDocument}
Expand Down Expand Up @@ -874,6 +874,28 @@ object HttpExecutorSpec extends IntegrationSpec {
Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
),
test("search for a document using should without satisfying minimumShouldMatch condition") {
checkN(4)(genDocumentId, genTestDocument, genDocumentId, genTestDocument) {
(firstDocumentId, firstDocument, secondDocumentId, secondDocument) =>
for {
_ <- Executor.execute(ElasticRequest.deleteByQuery(firstSearchIndex, matchAll))
_ <-
Executor.execute(
ElasticRequest.upsert[TestDocument](firstSearchIndex, firstDocumentId, firstDocument)
)
_ <- Executor.execute(
ElasticRequest
.upsert[TestDocument](firstSearchIndex, secondDocumentId, secondDocument)
.refreshTrue
)
query = ElasticQuery.script(Script("doc['booleanField'].value == true"))
res <- Executor.execute(ElasticRequest.search(firstSearchIndex, query)).documentAs[TestDocument]
} yield assert(res)(hasSameElements(List(firstDocument, secondDocument).filter(_.booleanField == true)))
}
} @@ around(
Executor.execute(ElasticRequest.createIndex(firstSearchIndex)),
Executor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
),
test("search for a document using script query") {
checkOnce(genDocumentId, genTestDocument, genDocumentId, genTestDocument) {
(firstDocumentId, firstDocument, secondDocumentId, secondDocument) =>
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package zio.elasticsearch
import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitive
import zio.elasticsearch.query._
import zio.elasticsearch.script.Script
import zio.schema.Schema

object ElasticQuery {
Expand Down Expand Up @@ -507,6 +508,16 @@ object ElasticQuery {
final def range(field: String): RangeQuery[Any, Any, Unbounded.type, Unbounded.type] =
Range.empty[Any, Any](field = field)

/**
* Constructs an instance of [[zio.elasticsearch.query.ScriptQuery]] with the provided script.
* @param script
* the script that is used by the query
* @return
* an instance of [[zio.elasticsearch.query.ScriptQuery]] that represents the script query to be performed.
*/
final def script(script: Script): ScriptQuery =
query.Script(script = script, boost = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that should satisfy the criteria using
* the specified parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ private[elasticsearch] object Range {
)
}

sealed trait ScriptQuery extends ElasticQuery[Any] with HasBoost[ScriptQuery]

private[elasticsearch] final case class Script(script: zio.elasticsearch.script.Script, boost: Option[Double])
extends ScriptQuery { self =>
def boost(value: Double): ScriptQuery =
self.copy(boost = Some(value))

private[elasticsearch] def toJson(fieldPath: Option[String]): Json =
Obj("script" -> Obj(("script" -> script.toJson) +: Chunk.fromIterable(boost.map("boost" -> Num(_)))))
}

sealed trait TermQuery[S] extends ElasticQuery[S] with HasBoost[TermQuery[S]] with HasCaseInsensitive[TermQuery[S]]

private[elasticsearch] final case class Term[S](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticHighlight.highlight
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.ElasticQuery.{script => _, _}
import zio.elasticsearch.domain._
import zio.elasticsearch.query.DistanceType.Plane
import zio.elasticsearch.query.DistanceUnit.Kilometers
import zio.elasticsearch.query.ValidationMethod.IgnoreMalformed
import zio.elasticsearch.query._
import zio.elasticsearch.script.{Painless, Script}
import zio.elasticsearch.utils._
import zio.test.Assertion.equalTo
import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert}
Expand Down Expand Up @@ -962,6 +963,36 @@ object ElasticQuerySpec extends ZIOSpecDefault {
)
)
},
test("script") {
val query =
ElasticQuery.script(Script("doc['day_of_week'].value > params['day']").params("day" -> 2).lang(Painless))
val queryWithBoost = ElasticQuery.script(Script("doc['day_of_week'].value > 2")).boost(2.0)

assert(query)(
equalTo(
zio.elasticsearch.query.Script(
script = Script(
source = "doc['day_of_week'].value > params['day']",
params = Map("day" -> 2),
lang = Some(Painless)
),
boost = None
)
)
) &&
assert(queryWithBoost)(
equalTo(
zio.elasticsearch.query.Script(
script = Script(
source = "doc['day_of_week'].value > 2",
params = Map.empty,
lang = None
),
boost = Some(2.0)
)
)
)
},
test("startsWith") {
val query = startsWith("testField", "test")
val queryTs = startsWith(TestDocument.stringField, "test")
Expand Down Expand Up @@ -2322,6 +2353,41 @@ object ElasticQuerySpec extends ZIOSpecDefault {
assert(queryMixedBoundsWithBoost.toJson(fieldPath = None))(equalTo(expectedMixedBoundsWithBoost.toJson)) &&
assert(queryWithFormat.toJson(fieldPath = None))(equalTo(expectedWithFormat.toJson))
},
test("script") {
val query =
ElasticQuery.script(Script("doc['day_of_week'].value > params['day']").params("day" -> 2).lang(Painless))
val queryWithBoost = ElasticQuery.script(Script("doc['day_of_week'].value > 2")).boost(2.0)

val expected =
"""
|{
| "script": {
| "script": {
| "lang": "painless",
| "source": "doc['day_of_week'].value > params['day']",
| "params": {
| "day": 2
| }
| }
| }
|}
|""".stripMargin

val expectedWithBoost =
"""
|{
| "script": {
| "script": {
| "source": "doc['day_of_week'].value > 2"
| },
| "boost": 2.0
| }
|}
|""".stripMargin

assert(query.toJson(fieldPath = None))(equalTo(expected.toJson)) &&
assert(queryWithBoost.toJson(fieldPath = None))(equalTo(expectedWithBoost.toJson))
},
test("startsWith") {
val query = startsWith(TestDocument.stringField, "test")
val queryWithBoost = startsWith(TestDocument.stringField, "test").boost(3.14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ object ScriptSpec extends ZIOSpecDefault {
},
test("successfully create Script with source and lang") {
assert(Script("doc['day_of_week'].value").lang(Painless))(
equalTo(Script("doc['day_of_week'].value").lang(Painless))
equalTo(
Script(source = "doc['day_of_week'].value", params = Map.empty, lang = Some(Painless))
)
)
},
test("successfully create Script with source, params and lang") {
Expand Down