Function score query in Elasticsearch - Time & Space Complexity
When using a function score query in Elasticsearch, it's important to know how the query's execution time changes as the data grows.
We want to understand how the scoring functions affect the total work Elasticsearch does.
Analyze the time complexity of the following function score query.
{
"query": {
"function_score": {
"query": { "match": { "field": "value" } },
"functions": [
{ "weight": 2 },
{ "random_score": {} }
],
"score_mode": "sum"
}
}
}
This query matches documents by a field and then adjusts their scores using multiple functions combined by summing.
Look for repeated work inside the query execution.
- Primary operation: Scoring each matched document by applying all scoring functions.
- How many times: Once for every document that matches the inner query.
As the number of matched documents grows, the scoring work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 documents x 2 functions = 20 scoring operations |
| 100 | 100 documents x 2 functions = 200 scoring operations |
| 1000 | 1000 documents x 2 functions = 2000 scoring operations |
Pattern observation: The total scoring work grows directly with the number of matched documents.
Time Complexity: O(n)
This means the time to score grows in a straight line with the number of matched documents.
[X] Wrong: "Adding more scoring functions does not affect query time much."
[OK] Correct: Each scoring function runs for every matched document, so more functions multiply the work.
Understanding how scoring functions affect query time helps you design efficient searches and explain performance clearly.
What if we added a filter that reduces matched documents by half? How would the time complexity change?
Practice
function_score query in Elasticsearch?Solution
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.Step 2: Compare with other query types
Filtering or updating documents are different operations; function_score specifically adjusts scoring.Final Answer:
To customize how search results are ranked by modifying their scores -> Option CQuick Check:
Function score query = Custom ranking [OK]
- Confusing function_score with filtering queries
- Thinking it updates or deletes documents
- Assuming it only filters exact matches
function_score query in Elasticsearch?Solution
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".Step 2: Validate option structure
{ "function_score": { "query": { ... }, "functions": [ ... ] } } correctly shows the function_score object containing "query" and "functions" keys as expected.Final Answer:
{ "function_score": { "query": { ... }, "functions": [ ... ] } } -> Option AQuick Check:
Correct JSON structure = { "function_score": { "query": { ... }, "functions": [ ... ] } } [OK]
- Using arrays instead of objects for function_score
- Placing functions outside function_score
- Incorrect nesting of query and functions
weight parameter?
{
"function_score": {
"query": { "match": { "title": "elasticsearch" } },
"functions": [
{ "weight": 3 }
]
}
}Solution
Step 1: Understand the weight function in function_score
The weight function multiplies the score of matching documents by the given weight value.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.Final Answer:
It multiplies the original score of matching documents by 3 -> Option BQuick Check:
Weight multiplies score = It multiplies the original score of matching documents by 3 [OK]
- Thinking weight adds to score instead of multiplying
- Confusing weight with filtering
- Assuming weight sets fixed score
{
"function_score": {
"query": { "match": { "content": "search" } },
"functions": {
"weight": 2
}
}
}Solution
Step 1: Check the type of "functions" field
The "functions" field must be an array of function objects, not a single object.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.Final Answer:
"functions" should be an array, not an object -> Option DQuick Check:
Functions must be array = "functions" should be an array, not an object [OK]
- Using object instead of array for functions
- Placing query inside functions
- Assuming score_mode is mandatory
popularity field greater than 10 by a factor of 5 using a function_score query. Which snippet correctly applies this boost?Solution
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.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.Final Answer:
{ "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } } -> Option AQuick Check:
Filter + weight inside functions array = { "function_score": { "query": { "match_all": {} }, "functions": [ { "filter": { "range": { "popularity": { "gt": 10 } } }, "weight": 5 } ] } } [OK]
- Placing weight outside functions array
- Using object instead of array for functions
- Applying weight directly on query instead of functions
Ready to prove it?
Pass the test and this pattern moves unknown
