Script Score Query Usage

edit

A query allowing you to modify the score of documents that are retrieved by a query. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents.

See the Elasticsearch documentation on script_score query for more details.

Fluent DSL example

edit
q
.ScriptScore(sn => sn
    .Name("named_query")
    .Boost(1.1)
    .Query(qq => qq
        .Range(r => r
            .Field(f => f.NumberOfCommits)
            .GreaterThan(50)
        )
    )
    .Script(s => s
        .Source(_scriptScoreSource)
        .Params(p => p
            .Add("origin", 100)
            .Add("scale", 10)
            .Add("decay", 0.5)
            .Add("offset", 0)
        )
    )
)

Object Initializer syntax example

edit
new ScriptScoreQuery
{
    Name = "named_query",
    Boost = 1.1,
    Query = new NumericRangeQuery
    {
        Field = Infer.Field<Project>(f => f.NumberOfCommits),
        GreaterThan = 50
    },
    Script = new InlineScript(_scriptScoreSource)
    {
        Params = new Dictionary<string, object>
        {
            { "origin", 100 },
            { "scale", 10 },
            { "decay", 0.5 },
            { "offset", 0 }
        }
    },
}

Example json output.

{
  "script_score": {
    "_name": "named_query",
    "boost": 1.1,
    "query": {
      "range": {
        "numberOfCommits": {
          "gt": 50.0
        }
      }
    },
    "script": {
      "source": "decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['numberOfCommits'].value)",
      "params": {
        "origin": 100,
        "scale": 10,
        "decay": 0.5,
        "offset": 0
      }
    }
  }
}