From 59ea279c847cc66bfc642141cd7ae8baee2d2d7d Mon Sep 17 00:00:00 2001 From: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:31:18 -0400 Subject: [PATCH] Add term frequency functions to score script query (#4988) * Add term frequency functions to score script query Signed-off-by: Fanit Kolchina * Add tf setup and rewording Signed-off-by: Fanit Kolchina * typo fix Signed-off-by: Fanit Kolchina * changed heading level Signed-off-by: Fanit Kolchina * Update _query-dsl/specialized/script-score.md Co-authored-by: Chris Moore <107723039+cwillum@users.noreply.github.com> Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Nathan Bower Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> * Remove tf from documentation Signed-off-by: Fanit Kolchina --------- Signed-off-by: Fanit Kolchina Signed-off-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Co-authored-by: Chris Moore <107723039+cwillum@users.noreply.github.com> Co-authored-by: Nathan Bower --- _query-dsl/specialized/script-score.md | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/_query-dsl/specialized/script-score.md b/_query-dsl/specialized/script-score.md index 2f40fcdc..d09158f2 100644 --- a/_query-dsl/specialized/script-score.md +++ b/_query-dsl/specialized/script-score.md @@ -328,5 +328,51 @@ GET blogs/_search ``` {% include copy-curl.html %} +### Term frequency functions + +Term frequency functions expose term-level statistics in the score script source. You can use these statistics to implement custom information retrieval and ranking algorithms, like query-time multiplicative or additive score boosting by popularity. To apply a term frequency function, call one of the following Painless methods: + +- `int termFreq(String , String )`: Retrieves the term frequency within a field for a specific term. +- `long totalTermFreq(String , String )`: Retrieves the total term frequency within a field for a specific term. +- `long sumTotalTermFreq(String )`: Retrieves the sum of total term frequencies within a field. + +#### Example + +The following query calculates the score as the total term frequency for each field in the `fields` list multiplied by the `multiplier` value: + +```json +GET /demo_index_v1/_search +{ + "query": { + "function_score": { + "query": { + "match_all": {} + }, + "script_score": { + "script": { + "source": """ + for (int x = 0; x < params.fields.length; x++) { + String field = params.fields[x]; + if (field != null) { + return params.multiplier * totalTermFreq(field, params.term); + } + } + return params.default_value; + + """, + "params": { + "fields": ["title", "description"], + "term": "ai", + "multiplier": 2, + "default_value": 1 + } + } + } + } + } +} +``` +{% include copy-curl.html %} + If [`search.allow_expensive_queries`]({{site.url}}{{site.baseurl}}/query-dsl/index/#expensive-queries) is set to `false`, `script_score` queries are not executed. {: .important} \ No newline at end of file