Skip to main content

Script Score Query

The ScriptScore query uses a script to compute a custom relevance score for the documents matched by a wrapped query.

In order to use the ScriptScore query import the following:

import zio.elasticsearch.query.ScriptScoreQuery
import zio.elasticsearch.ElasticQuery._
import zio.elasticsearch.script.Script

You can create a ScriptScore query with arbitrary query (MatchAll in this example) and a Script using the scriptScore method in the following manner:

val query: ScriptScoreQuery[Any] = scriptScore(matchAll, Script("doc['intField'].value * 2"))

You can create a type-safe ScriptScore query with arbitrary type-safe query using the scriptScore method in the following manner:

val query: ScriptScoreQuery[Document] =
scriptScore(matches(Document.stringField, "test"), Script("doc['intField'].value * 2"))

If you want to change the boost, you can use boost method:

val queryWithBoost: ScriptScoreQuery[Document] =
scriptScore(matches(Document.stringField, "test"), Script("doc['intField'].value * 2")).boost(2.0)

If you want to change the minScore, you can use minScore method:

val queryWithMinScore: ScriptScoreQuery[Document] =
scriptScore(matches(Document.stringField, "test"), Script("doc['intField'].value * 2")).minScore(5.0)

You can find more information about ScriptScore query here.