Bird
Raised Fist0
Elasticsearchquery~20 mins

Function score query in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a function_score query in Elasticsearch?
easy
A. To update documents in the index
B. To filter documents based on exact matches only
C. To customize how search results are ranked by modifying their scores
D. To delete documents matching a query

Solution

  1. Step 1: Understand the role of function_score query

    The function_score query is designed to change the score of documents returned by a query, allowing customization of ranking.
  2. Step 2: Compare with other query types

    Filtering or updating documents are different operations; function_score specifically adjusts scoring.
  3. Final Answer:

    To customize how search results are ranked by modifying their scores -> Option C
  4. Quick Check:

    Function score query = Custom ranking [OK]
Hint: Function score changes ranking by adjusting scores [OK]
Common Mistakes:
  • Confusing function_score with filtering queries
  • Thinking it updates or deletes documents
  • Assuming it only filters exact matches
2. Which of the following is the correct basic structure of a function_score query in Elasticsearch?
easy
A. { "function_score": { "query": { ... }, "functions": [ ... ] } }
B. { "function_score": [ { "query": { ... } }, { "functions": [ ... ] } ] }
C. { "query": { "function_score": { "functions": [ ... ] } } }
D. { "functions": { "function_score": { "query": { ... } } } }

Solution

  1. Step 1: Recall function_score query JSON format

    The function_score query wraps a query and a list of scoring functions inside an object with keys "query" and "functions".
  2. Step 2: Validate option structure

    { "function_score": { "query": { ... }, "functions": [ ... ] } } correctly shows the function_score object containing "query" and "functions" keys as expected.
  3. Final Answer:

    { "function_score": { "query": { ... }, "functions": [ ... ] } } -> Option A
  4. Quick Check:

    Correct JSON structure = { "function_score": { "query": { ... }, "functions": [ ... ] } } [OK]
Hint: Look for "function_score" with nested "query" and "functions" keys [OK]
Common Mistakes:
  • Using arrays instead of objects for function_score
  • Placing functions outside function_score
  • Incorrect nesting of query and functions
3. Given this function_score query snippet, what is the effect of the weight parameter?
{
  "function_score": {
    "query": { "match": { "title": "elasticsearch" } },
    "functions": [
      { "weight": 3 }
    ]
  }
}
medium
A. It adds 3 points to the original score
B. It multiplies the original score of matching documents by 3
C. It filters out documents with score less than 3
D. It sets the score of all documents to 3

Solution

  1. Step 1: Understand the weight function in function_score

    The weight function multiplies the score of matching documents by the given weight value.
  2. Step 2: Analyze the query snippet

    The query matches documents with "title" containing "elasticsearch" and applies a weight of 3, multiplying their scores by 3.
  3. Final Answer:

    It multiplies the original score of matching documents by 3 -> Option B
  4. Quick Check:

    Weight multiplies score = It multiplies the original score of matching documents by 3 [OK]
Hint: Weight multiplies scores, not adds or filters [OK]
Common Mistakes:
  • Thinking weight adds to score instead of multiplying
  • Confusing weight with filtering
  • Assuming weight sets fixed score
4. Identify the error in this function_score query:
{
  "function_score": {
    "query": { "match": { "content": "search" } },
    "functions": {
      "weight": 2
    }
  }
}
medium
A. Missing "score_mode" field
B. "weight" cannot be used inside functions
C. "query" must be inside "functions"
D. "functions" should be an array, not an object

Solution

  1. Step 1: Check the type of "functions" field

    The "functions" field must be an array of function objects, not a single object.
  2. Step 2: Identify the error in the query

    Here, "functions" is incorrectly given as an object with "weight" key instead of an array containing that object.
  3. Final Answer:

    "functions" should be an array, not an object -> Option D
  4. Quick Check:

    Functions must be array = "functions" should be an array, not an object [OK]
Hint: Functions must be an array of objects [OK]
Common Mistakes:
  • Using object instead of array for functions
  • Placing query inside functions
  • Assuming score_mode is mandatory
5. You want to boost documents with a popularity field greater than 10 by a factor of 5 using a function_score query. Which snippet correctly applies this boost?
hard
A. { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } }
B. { "function_score": { "query": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } }
C. { "function_score": { "query": { "match_all": {} }, "weight": 5, "filter": { "range": { "popularity": { "gt": 10 } } } } }
D. { "function_score": { "query": { "match_all": {} }, "functions": { "weight": 5, "filter": { "range": { "popularity": { "gt": 10 } } } } } }

Solution

  1. Step 1: Understand boosting with filter and weight

    To boost only documents matching a condition, use a function with a "filter" and a "weight" inside the "functions" array.
  2. Step 2: Analyze each option

    { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } } correctly uses "functions" as an array with a filter on popularity > 10 and weight 5. Other options misuse weight placement or structure.
  3. Final Answer:

    { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } } -> Option A
  4. Quick Check:

    Filter + weight inside functions array = { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } } [OK]
Hint: Use filter and weight inside functions array to boost conditionally [OK]
Common Mistakes:
  • Placing weight outside functions array
  • Using object instead of array for functions
  • Applying weight directly on query instead of functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard