0
0
Elasticsearchquery~20 mins

Function score query in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Score Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this function score query?
Given the following Elasticsearch function score query, what is the resulting score for a document with field1 value 5 and field2 value 10?
Elasticsearch
{
  "query": {
    "function_score": {
      "query": { "match_all": {} },
      "functions": [
        {
          "field_value_factor": {
            "field": "field1",
            "factor": 2,
            "modifier": "sqrt"
          }
        },
        {
          "field_value_factor": {
            "field": "field2",
            "factor": 1.5
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  }
}
AScore = sqrt(5) * 2 + 10 * 1.5 = 4.472 + 15 = 19.472
BScore = (sqrt(5) + 10) * 1.5 = (2.236 + 10) * 1.5 = 18.354
CScore = sqrt(5 * 2) + 10 * 1.5 = 3.162 + 15 = 18.162
DScore = sqrt(5) * 2 * 1.5 + 10 = 6.708 + 10 = 16.708
Attempts:
2 left
💡 Hint
Remember that the score_mode 'sum' adds the results of each function, and boost_mode 'replace' uses the function score as the final score.
🧠 Conceptual
intermediate
1:00remaining
Which score_mode combines function scores by multiplying them?
In an Elasticsearch function score query, which score_mode option multiplies the scores from each function together?
A"sum"
B"max"
C"avg"
D"multiply"
Attempts:
2 left
💡 Hint
Think about how scores combine when you want to increase or decrease the total score multiplicatively.
🔧 Debug
advanced
1:30remaining
Why does this function score query cause a syntax error?
Identify the syntax error in this function score query snippet:
Elasticsearch
{
  "query": {
    "function_score": {
      "query": { "match_all": {} },
      "functions": [
        {
          "field_value_factor": {
            "field": "popularity",
            "factor": 1.2
          }
        },
        {
          "random_score": {
            "seed": 12345
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply"
    }
  }
}
AInvalid value for score_mode, should be 'multiply' not 'sum'
BMissing closing brace '}' at the end of the JSON
Crandom_score cannot be used inside functions array
Dfield_value_factor requires a 'modifier' field
Attempts:
2 left
💡 Hint
Check if all braces and brackets are properly closed.
📝 Syntax
advanced
1:30remaining
Which option correctly uses a filter in a function score query?
Select the option that correctly applies a filter to a function inside a function score query.
A{ "weight": 2, "filter": { "term": { "status": "active" } } }
B{ "filter": { "term": { "status": "active" } }, "weight": 2 }
C{ "weight": 2, "query": { "term": { "status": "active" } } }
D{ "filter": { "match": { "status": "active" } }, "weight": 2 }
Attempts:
2 left
💡 Hint
The filter must be a sibling of weight inside the function object.
🚀 Application
expert
2:00remaining
How many functions are applied in this function score query?
Given this function score query, how many scoring functions are applied?
Elasticsearch
{
  "query": {
    "function_score": {
      "query": { "match": { "title": "search" } },
      "functions": [
        { "weight": 3 },
        { "field_value_factor": { "field": "popularity", "factor": 1.1 } },
        { "random_score": {} },
        { "script_score": { "script": "doc['views'].value / 10" } }
      ],
      "score_mode": "avg",
      "boost_mode": "multiply"
    }
  }
}
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count each object inside the 'functions' array.