A match phrase query helps you find documents where words appear together in the exact order you want.
Match phrase query in Elasticsearch
Introduction
Syntax
Elasticsearch
{
"query": {
"match_phrase": {
"field_name": "your phrase here"
}
}
}Replace field_name with the name of the field you want to search.
The phrase inside quotes is the exact sequence of words to match.
Examples
message field for the exact phrase "quick brown fox".Elasticsearch
{
"query": {
"match_phrase": {
"message": "quick brown fox"
}
}
}title field contains the phrase "Elasticsearch tutorial" exactly.Elasticsearch
{
"query": {
"match_phrase": {
"title": "Elasticsearch tutorial"
}
}
}Sample Program
This query looks for documents where the description field contains the phrase "fresh organic apples" exactly as typed.
Elasticsearch
{
"query": {
"match_phrase": {
"description": "fresh organic apples"
}
}
}Important Notes
The match phrase query is stricter than a simple match query because it requires the words to be in order.
You can add a slop parameter to allow some words between the phrase words if needed.
Summary
Use match phrase query to find exact word sequences in text fields.
It helps when word order matters in your search.
It is useful for searching quotes, names, or fixed phrases.
Practice
1. What does a
match_phrase query in Elasticsearch do?easy
Solution
Step 1: Understand the purpose of match_phrase
Thematch_phrasequery looks for the exact sequence of words in the order they appear in the query.Step 2: Compare with other query types
Unlikematchquery, which finds any word in any order,match_phraserequires the phrase to appear exactly.Final Answer:
Searches for an exact sequence of words in the given order. -> Option CQuick Check:
match_phrase = exact word order search [OK]
Hint: Remember: match_phrase means exact words in exact order [OK]
Common Mistakes:
- Confusing match_phrase with match query
- Thinking it ignores word order
- Assuming it works only for single words
2. Which of the following is the correct syntax for a
match_phrase query in Elasticsearch?easy
Solution
Step 1: Identify the correct query type
Thematch_phrasequery uses the key "match_phrase" with the field and the phrase to search.Step 2: Check the syntax format
The correct syntax is a JSON object with "match_phrase" as the key and the field name mapping to the phrase string.Final Answer:
{ "match_phrase": { "field": "search text" } } -> Option BQuick Check:
Correct JSON syntax for match_phrase = { "match_phrase": { "field": "search text" } } [OK]
Hint: Look for "match_phrase" key with field and phrase inside braces [OK]
Common Mistakes:
- Using "match" instead of "match_phrase"
- Confusing with "term" or "range" queries
- Incorrect JSON structure or missing braces
3. Given this Elasticsearch query:
Which documents will match?
{ "query": { "match_phrase": { "description": "quick brown fox" } } }Which documents will match?
medium
Solution
Step 1: Analyze the match_phrase query
The query searches for the exact phrase "quick brown fox" in the field "description".Step 2: Understand phrase matching behavior
Only documents with the words in the exact order "quick brown fox" will match, not any other order or partial words.Final Answer:
Documents containing the phrase "quick brown fox" exactly in that order. -> Option DQuick Check:
Exact phrase match = Documents containing the phrase "quick brown fox" exactly in that order. [OK]
Hint: match_phrase requires exact word order in the phrase [OK]
Common Mistakes:
- Assuming word order does not matter
- Thinking partial word matches count
- Confusing with match query behavior
4. You wrote this query but it returns no results:
Your documents contain the phrase "runner fast". What is the issue?
{ "query": { "match_phrase": { "title": "fast runner" } } }Your documents contain the phrase "runner fast". What is the issue?
medium
Solution
Step 1: Check phrase order in query vs documents
The query searches for "fast runner" but documents have "runner fast" which is reversed.Step 2: Understand match_phrase behavior
match_phrase requires the exact word order, so reversed order causes no matches.Final Answer:
The phrase order is incorrect; match_phrase requires exact order. -> Option AQuick Check:
Exact order matters in match_phrase = The phrase order is incorrect; match_phrase requires exact order. [OK]
Hint: Check word order carefully when match_phrase returns no hits [OK]
Common Mistakes:
- Ignoring word order importance
- Assuming match_phrase works like match
- Changing field name unnecessarily
5. You want to find documents containing the phrase "data science" but also allow a gap of one word between "data" and "science". Which
match_phrase query option should you use?hard
Solution
Step 1: Understand phrase slop in match_phrase
The "slop" parameter allows words to appear with gaps or reordered within a limit.Step 2: Identify correct syntax for slop
Using "slop": 1 inside thematch_phrasequery allows one word gap between "data" and "science".Final Answer:
{ "match_phrase": { "field": "data science", "slop": 1 } } -> Option AQuick Check:
Use slop to allow gaps in match_phrase = { "match_phrase": { "field": "data science", "slop": 1 } } [OK]
Hint: Add "slop" to match_phrase to allow word gaps [OK]
Common Mistakes:
- Using slop with match instead of match_phrase
- Confusing boost with slop
- Using term query for phrase matching
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
