📖 Scenario: You are working with a small collection of book data stored in Elasticsearch. You want to find books that contain an exact phrase in their description.
🎯 Goal: Build an Elasticsearch query that uses a match_phrase to find books with the exact phrase "deep learning" in their description.
📋 What You'll Learn
Create an index called books with sample book documents
Add a configuration variable for the phrase to search
Write a match_phrase query using the phrase variable
Print the query JSON to see the final search request
💡 Why This Matters
🌍 Real World
Searching for exact phrases in text fields is common in search engines, like finding books or articles containing specific terms.
💼 Career
Understanding how to build match_phrase queries is useful for roles involving search engine development, data retrieval, and Elasticsearch administration.
Progress0 / 4 steps
1
Create the books index with sample data
Create a variable called books_index that holds a JSON object with two book documents. Each document should have id, title, and description. Use these exact entries: {"id": 1, "title": "AI Basics", "description": "An introduction to artificial intelligence."} and {"id": 2, "title": "Deep Learning Guide", "description": "A comprehensive guide to deep learning techniques."}
Elasticsearch
Hint
Use a list of dictionaries to represent the books. Each dictionary should have keys: id, title, and description.
2
Set the phrase to search
Create a variable called search_phrase and set it to the string "deep learning".
Elasticsearch
Hint
Assign the exact string "deep learning" to the variable search_phrase.
3
Build the match_phrase query
Create a variable called query that holds a JSON object for an Elasticsearch match_phrase query. The query should search the description field for the phrase stored in search_phrase.
Elasticsearch
Hint
Use the match_phrase key inside query with the field description set to search_phrase.
4
Print the final query JSON
Write a print statement to display the query variable.
Elasticsearch
Hint
Use print(query) to show the final query JSON.
Practice
(1/5)
1. What does a match_phrase query in Elasticsearch do?
easy
A. Searches only for single words, ignoring phrases.
B. Searches for any of the words in the query, regardless of order.
C. Searches for an exact sequence of words in the given order.
D. Searches for documents with numeric values only.
Solution
Step 1: Understand the purpose of match_phrase
The match_phrase query looks for the exact sequence of words in the order they appear in the query.
Step 2: Compare with other query types
Unlike match query, which finds any word in any order, match_phrase requires the phrase to appear exactly.
Final Answer:
Searches for an exact sequence of words in the given order. -> Option C
Quick 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
A. { "match": { "field": "search text" } }
B. { "match_phrase": { "field": "search text" } }
C. { "term": { "field": "search text" } }
D. { "range": { "field": { "gte": 10 } } }
Solution
Step 1: Identify the correct query type
The match_phrase query 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.
Your documents contain the phrase "runner fast". What is the issue?
medium
A. The phrase order is incorrect; match_phrase requires exact order.
B. The field name "title" is invalid.
C. match_phrase does not support multi-word phrases.
D. The query syntax is wrong; it should use "match" instead.
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 A
Quick 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?