Skip to content

Commit

Permalink
Fix code remarks, add tests and rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya committed May 29, 2023
1 parent 0c243ba commit a268162
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ sealed trait FunctionScoreQuery[S] extends ElasticQuery[S] with HasBoost[Functio
*
* @param value
* a [[zio.elasticsearch.query.ElasticQuery]] to be executed
* @tparam S1
* the type of the [[zio.elasticsearch.query.ElasticQuery]] for type shrinking
* @return
* an instance of [[zio.elasticsearch.query.FunctionScoreQuery]] enriched with the `query` parameter.
*/
def query(value: ElasticQuery[S]): FunctionScoreQuery[S]
def query[S1 <: S](value: ElasticQuery[S1]): FunctionScoreQuery[S1]

/**
* Sets the `scoreMode` parameter for the [[zio.elasticsearch.query.FunctionScoreQuery]]. The `scoreMode` parameter
Expand All @@ -274,12 +276,12 @@ sealed trait FunctionScoreQuery[S] extends ElasticQuery[S] with HasBoost[Functio
* @param functions
* multiple [[zio.elasticsearch.query.FunctionScoreFunction]] to be added to query
* @tparam S1
* the type of the sub-queries, for which an implicit [[zio.schema.Schema]] is required
* the type of the [[zio.elasticsearch.query.FunctionScoreFunction]] for type shrinking
* @return
* an instance of [[zio.elasticsearch.query.FunctionScoreQuery]] enriched with the `functionScoreFunctions`
* parameter.
*/
def withFunctions[S1 <: S: Schema](functions: FunctionScoreFunction[S1]*): FunctionScoreQuery[S1]
def withFunctions[S1 <: S](functions: FunctionScoreFunction[S1]*): FunctionScoreQuery[S1]
}

private[elasticsearch] final case class FunctionScore[S](
Expand All @@ -304,26 +306,26 @@ private[elasticsearch] final case class FunctionScore[S](
def minScore(value: Double): FunctionScoreQuery[S] =
self.copy(minScore = Some(value))

def query(value: ElasticQuery[S]): FunctionScoreQuery[S] =
def query[S1 <: S](value: ElasticQuery[S1]): FunctionScoreQuery[S1] =
self.copy(query = Some(value))

def scoreMode(value: FunctionScoreScoreMode): FunctionScoreQuery[S] =
self.copy(scoreMode = Some(value))

def withFunctions[S1 <: S: Schema](functions: FunctionScoreFunction[S1]*): FunctionScoreQuery[S1] =
def withFunctions[S1 <: S](functions: FunctionScoreFunction[S1]*): FunctionScoreQuery[S1] =
self.copy(functionScoreFunctions = functionScoreFunctions ++ functions)

private[elasticsearch] def toJson(fieldPath: Option[String]): Json =
Obj(
"function_score" -> Obj(
Chunk(
Some("functions" -> Arr(functionScoreFunctions.map(_.toJson))),
boost.map("boost" -> Num(_)),
boostMode.map(bm => "boost_mode" -> Str(s"${bm.toString.toLowerCase}")),
maxBoost.map("max_boost" -> Num(_)),
minScore.map("min_score" -> Num(_)),
boost.map("boost" -> _.toJson),
boostMode.map(bm => "boost_mode" -> bm.toString.toLowerCase.toJson),
maxBoost.map("max_boost" -> _.toJson),
minScore.map("min_score" -> _.toJson),
query.map(q => "query" -> q.toJson(None)),
scoreMode.map(sm => "score_mode" -> Str(s"${sm.toString.toLowerCase}"))
scoreMode.map(sm => "score_mode" -> sm.toString.toLowerCase.toJson)
).flatten
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticHighlight.highlight
import zio.elasticsearch.ElasticQuery.{script => _, _}
import zio.Chunk
import zio.elasticsearch.domain._
import zio.elasticsearch.query.DistanceType.Plane
import zio.elasticsearch.query.DistanceUnit.Kilometers
Expand Down Expand Up @@ -400,34 +400,154 @@ object ElasticQuerySpec extends ZIOSpecDefault {
val randomScore = randomScoreFunction()
val fieldValue = fieldValueFactor(TestDocument.stringField)
val decay = expDecayFunction("field", origin = "11, 12", scale = "2km")
val typedDecay = expDecayFunction(TestDocument.intField, origin = "11,12", scale = "2km")

val query = functionScore(scriptScore, weight, randomScore, fieldValue)
val fullQuery: FunctionScoreQuery[TestDocument] = functionScore(scriptScore, weight, randomScore)
.withFunctions(decay)
.withFunctions(fieldValue)
.boost(2.0)
.boostMode(FunctionScoreBoostMode.Avg)
.maxBoost(42)
.minScore(32)
.query(matches("stringField", "string"))
.query(matches("stringField", "value"))
.scoreMode(FunctionScoreScoreMode.Min)

assert(query)(
val queryWithType: FunctionScoreQuery[TestDocument] =
functionScore(fieldValue).query(matches(TestDocument.stringField, "value"))
val queryTypeShrink: FunctionScoreQuery[TestDocument] =
functionScore(scriptScore).query(matches(TestDocument.stringField, "value"))
val queryWithoutTypeShrink: FunctionScoreQuery[Any] =
functionScore(scriptScore).query(matches("stringField", "value"))
val queryWithNewAnyQuery: FunctionScoreQuery[TestDocument] =
functionScore(fieldValue).query(matches("stringField", "value"))

val anyQueryWithNewTypedFunction = functionScore(scriptScore).withFunctions(fieldValue)
val anyQueryWithNewAnyFunction = functionScore(scriptScore).withFunctions(weight)
val typedQueryWithNewTypedFunction = functionScore(fieldValue).withFunctions(typedDecay)
val typedQueryWithNewAnyFunction = functionScore(fieldValue).withFunctions(weight)

assert(fullQuery)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(
scriptScore,
weight,
randomScore,
fieldValue,
decay
decay,
fieldValue
),
boost = Some(2.0),
boostMode = Some(FunctionScoreBoostMode.Avg),
maxBoost = Some(42.0),
minScore = Some(32.0),
query = Some(Match("stringField", "string")),
query = Some(Match("stringField", "value")),
scoreMode = Some(FunctionScoreScoreMode.Min)
)
)
) &&
assert(queryTypeShrink)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(scriptScore),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = Some(Match("stringField", "value")),
scoreMode = None
)
)
) &&
assert(queryWithType)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(fieldValue),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = Some(Match("stringField", "value")),
scoreMode = None
)
)
) &&
assert(queryWithoutTypeShrink)(
equalTo(
FunctionScore[Any](
functionScoreFunctions = Chunk(scriptScore),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = Some(Match("stringField", "value")),
scoreMode = None
)
)
) &&
assert(queryWithNewAnyQuery)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(fieldValue),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = Some(Match("stringField", "value")),
scoreMode = None
)
)
) &&
assert(anyQueryWithNewTypedFunction)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(scriptScore, fieldValue),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = None,
scoreMode = None
)
)
) &&
assert(anyQueryWithNewAnyFunction)(
equalTo(
FunctionScore[Any](
functionScoreFunctions = Chunk(scriptScore, weight),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = None,
scoreMode = None
)
)
) &&
assert(typedQueryWithNewTypedFunction)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(fieldValue, typedDecay),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = None,
scoreMode = None
)
)
) &&
assert(typedQueryWithNewAnyFunction)(
equalTo(
FunctionScore[TestDocument](
functionScoreFunctions = Chunk(fieldValue, weight),
boost = None,
boostMode = None,
maxBoost = None,
minScore = None,
query = None,
scoreMode = None
)
)
)
},
test("geoDistance") {
Expand Down

0 comments on commit a268162

Please sign in to comment.