Function Score Query
The FunctionScore
allows you to modify the score of documents that are retrieved by a query.
In order to use the FunctionScore
query and create needed FunctionScoreFunction
import the following:
import zio.elasticsearch.query.FunctionScoreQuery
import zio.elasticsearch.query.FunctionScoreFunction._
import zio.elasticsearch.ElasticQuery._
For creating FunctionScore
query you require FunctionScoreFunction
or multiple of them.
You can create these functions in following way.
You can create DecayFunction
with DecayFunctionType.Exp
using the expDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Any] = expDecayFunction("field", origin = "11, 12", scale = "2km")
You can create a type-safe
DecayFunction
with DecayFunctionType.Exp
using the expDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km")
You can create DecayFunction
with DecayFunctionType.Gauss
using the gaussDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Any] = gaussDecayFunction(field = "field", origin = "11, 12", scale = "2km")
You can create a type-safe
DecayFunction
with DecayFunctionType.Gauss
using the gaussDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Document] = gaussDecayFunction(field = Document.field, origin = "11, 12", scale = "2km")
You can create DecayFunction
with DecayFunctionType.Linear
using the linearDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Any] = linearDecayFunction(field = "field", origin = "11, 12", scale = "2km")
You can create a type-safe
DecayFunction
with DecayFunctionType.Linear
using the expDecayFunction
method with origin and scale in the following manner:
val function: DecayFunction[Document] = linearDecayFunction(field = Document.field, origin = "11, 12", scale = "2km")
You can create FieldValueFactor
using the fieldValueFactor
method:
val function: FieldValueFactor[Any] = fieldValueFactor(field = "field")
You can create a type-safe
FieldValueFactor
using the fieldValueFactor
method:
val function: FieldValueFactor[Document] = fieldValueFactor(field = Document.field)
You can create RandomScoreFunction
using the randomScoreFunction
in three different ways depending on amount of parameters
you want to use:
val function: RandomScoreFunction[Any] = randomScoreFunction()
val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10)
val function: RandomScoreFunction[Any] = randomScoreFunction(seed = 10, field = "field")
You can create a type-safe
RandomScoreFunction
using the randomScoreFunction
method:
val function: RandomScoreFunction = randomScoreFunction(seed = 10, field = Document.field)
You can create ScriptScoreFunction
using the scriptScoreFunction
method with script in following manner:
val function: ScriptScoreFunction[Any] = scriptScoreFunction(script = Script("params.agg1 > 10"))
val function: ScriptScoreFunction[Any] = scriptScoreFunction(scriptSource = "params.agg1 > 10")
You can create WeightFunction
using the weightFunction
method(you must provide Any
type parameter when using):
val function: WeightFunction[Any] = scriptScoreFunction(weight = 10)
You can use these functions to create FunctionScore
query using the functionScore
method in the following manner:
val randomScoreFunction: RandomScoreFunction[Any] = randomScoreFunction()
val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10)
val query: FunctionScoreQuery[Any] = functionScore(randomScoreFunction, weightFunction)
You can create a type-safe FunctionScore
query
using the functionScore
, if all functions are created type-safe, in the following manner:
val decayFunction: DecayFunction[Document] = expDecayFunction(field = Document.field, origin = "11, 12", scale = "2km")
val randomScoreFunction: RandomScoreFunction[Document] = randomScoreFunction(seed = 10, field = Document.field)
val weightFunction: WeightFunction[Any] = scriptScoreFunction(weight = 10)
val query: FunctionScoreQuery[Document] = functionScore(decayFunction, randomScoreFunction, weightFunction)
If you want to change the boost
, you can use boost
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boost(5.0)
If you want to change the boostMode
, you can use boostMode
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).boostMode(FunctionScoreBoostMode.Max)
If you want to change the maxBoost
, you can use maxBoost
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).maxBoost(5.0)
If you want to change the minScore
, you can use minScore
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).minScore(5.0)
If you want to change the query
, you can use query
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).query(matches(Document.field, "value"))
If you want to change the scoreMode
, you can use scoreMode
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).scoreMode(FunctionScoreScoreMode.Max)
If you want to add a one or multiple new FunctionScoreFunction
you can use withFunctions
method:
import zio.elasticsearch.query.DistanceUnit
val queryWithDistance: FunctionScoreQuery[Document] = functionScore(randomScoreFunction(seed = 10, field = Document.field)).withFunctions(scriptScoreFunction(weight = 10))
You can find more information about FunctionScore
query here.