What if you could search many places at once with just one simple question?
Why Multi-match query in Elasticsearch? - Purpose & Use Cases
Imagine you have a huge library of books and you want to find all books that mention a certain word either in the title, the author name, or the summary. You try searching each field one by one manually.
Searching each field separately means you have to write many queries and combine results yourself. This is slow, complicated, and easy to miss matches if you forget a field. It's like looking for a needle in many haystacks one by one.
The multi-match query lets you search multiple fields at once with a single query. It automatically checks all the fields you want and finds the best matches, saving time and effort.
GET /books/_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "magic" } },
{ "match": { "author": "magic" } },
{ "match": { "summary": "magic" } }
]
}
}
}GET /books/_search
{
"query": {
"multi_match": {
"query": "magic",
"fields": ["title", "author", "summary"]
}
}
}It makes searching across many fields simple and powerful, helping you find relevant results faster and with less code.
When shopping online, you want to find products by name, description, or brand all at once. Multi-match query helps the search engine do exactly that behind the scenes.
Manual searching across fields is slow and error-prone.
Multi-match query searches many fields in one go.
This saves time and finds better results easily.
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
