A multi-match query helps you search for a word or phrase in many places at once. It makes finding information faster and easier.
Multi-match query in Elasticsearch
{
"multi_match": {
"query": "search text",
"fields": ["field1", "field2", "field3"]
}
}The query is the word or phrase you want to find.
The fields list tells Elasticsearch where to look.
{
"multi_match": {
"query": "apple",
"fields": ["title", "description"]
}
}{
"multi_match": {
"query": "fresh orange",
"fields": ["name", "tags"],
"type": "phrase"
}
}{
"multi_match": {
"query": "banana",
"fields": ["title^2", "description"]
}
}This query looks for the word "coffee" in the title, summary, and content fields of documents.
{
"query": {
"multi_match": {
"query": "coffee",
"fields": ["title", "summary", "content"]
}
}
}You can boost some fields by adding ^number after the field name to make matches there count more.
Use the type option to change how the query matches, like "phrase" for exact phrases.
Multi-match queries are great for simple searches across many fields without writing multiple queries.
Multi-match queries let you search many fields at once with one query.
You specify the search word or phrase and the fields to check.
You can control importance and matching style with extra options.
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
