Multi-match query in Elasticsearch - Time & Space Complexity
When using a multi-match query in Elasticsearch, it is important to understand how the search time changes as the amount of data grows.
We want to know how the number of operations increases when searching across multiple fields.
Analyze the time complexity of the following multi-match query.
{
"query": {
"multi_match": {
"query": "search text",
"fields": ["title", "description", "content"]
}
}
}
This query searches the text "search text" across three fields in each document.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching the query text in each specified field of every document.
- How many times: For each document, the search runs once per field listed (here 3 fields), so it repeats for all documents times number of fields.
As the number of documents grows, the search checks more entries. Also, more fields mean more checks per document.
| Input Size (n documents) | Approx. Operations |
|---|---|
| 10 | 30 (10 docs x 3 fields) |
| 100 | 300 (100 docs x 3 fields) |
| 1000 | 3000 (1000 docs x 3 fields) |
Pattern observation: The operations grow directly with the number of documents and fields checked.
Time Complexity: O(n * f)
This means the search time grows linearly with the number of documents (n) and the number of fields (f) searched.
[X] Wrong: "Searching multiple fields does not affect the search time much because it's just one query."
[OK] Correct: Each field adds a separate search operation per document, so more fields mean more work and longer search time.
Understanding how multi-field searches scale helps you explain performance considerations clearly and shows you grasp how search engines work under the hood.
"What if we added more fields to the multi-match query? How would the time complexity change?"
Practice
multi-match query in Elasticsearch?Solution
Step 1: Understand the multi-match query concept
A multi-match query allows searching across several fields in one query, making it easier to find matches in different parts of documents.Step 2: Compare with other options
The other options describe operations like creating indexes, updating documents, or deleting indexes, which are not the purpose of multi-match queries.Final Answer:
To search multiple fields with a single query -> Option AQuick Check:
Multi-match = search multiple fields [OK]
- Confusing multi-match with update or delete operations
- Thinking it creates or deletes indexes
- Assuming it searches only one field
Solution
Step 1: Identify required keys in multi-match query
A multi-match query requires a "query" key with the search term and a "fields" key listing the fields to search.Step 2: Check each option for correct syntax
{ "multi_match": { "query": "apple", "fields": ["title", "description"] } } correctly includes both "query" and "fields" keys. { "match": { "query": "apple", "fields": ["title", "description"] } } uses "match" instead of "multi_match". { "multi_match": { "fields": ["title", "description"] } } misses the "query" key. { "multi_match": { "query": "apple" } } misses the "fields" key.Final Answer:
{ "multi_match": { "query": "apple", "fields": ["title", "description"] } } -> Option AQuick Check:
multi_match needs query and fields [OK]
- Using 'match' instead of 'multi_match'
- Omitting the 'query' key
- Omitting the 'fields' key
{
"multi_match": {
"query": "quick fox",
"fields": ["title", "content"],
"type": "phrase"
}
}Solution
Step 1: Understand the 'type' parameter in multi-match
The "type": "phrase" option makes the query look for the exact phrase "quick fox" in the specified fields.Step 2: Check fields and matching behavior
The fields are "title" and "content", so the phrase must appear in either field, not necessarily both.Final Answer:
Searches for documents where "quick fox" appears as a phrase in either title or content -> Option CQuick Check:
type phrase = exact phrase match in fields [OK]
- Thinking it searches for words separately (not phrase)
- Assuming it searches only one field
- Believing it requires phrase in all fields
{
"multi_match": {
"query": "data",
"fields": "title, description"
}
}Solution
Step 1: Check the type of "fields" value
The "fields" key must be an array of field names, not a single string with commas.Step 2: Validate other keys
The "query" key is correctly a string. "multi_match" is the correct query type. "fields" should not be omitted.Final Answer:
"fields" should be an array, not a string -> Option BQuick Check:
fields must be array, not comma string [OK]
- Passing fields as a comma-separated string
- Changing multi_match to match incorrectly
- Thinking query must be an array
Solution
Step 1: Understand field boosting syntax
To give more importance to a field, add a caret (^) and a boost number after the field name, e.g., "title^3".Step 2: Check each option for correct boosting
{ "multi_match": { "query": "coffee", "fields": ["title^3", "description", "tags"] } } boosts "title" by 3, others default. { "multi_match": { "query": "coffee", "fields": ["title", "description^3", "tags"] } } boosts "description" incorrectly. { "multi_match": { "query": "coffee", "fields": ["title", "description", "tags"], "boost": 3 } } tries to boost all fields equally with "boost" key, which is invalid here. { "multi_match": { "query": "coffee", "fields": ["title", "description", "tags"], "type": "boost" } } uses an invalid "type" value.Final Answer:
{ "multi_match": { "query": "coffee", "fields": ["title^3", "description", "tags"] } } -> Option DQuick Check:
Use ^number to boost fields [OK]
- Boosting wrong field
- Using 'boost' key incorrectly
- Setting invalid 'type' value
Ready to prove it?
Pass the test and this pattern moves unknown
