📖 Scenario: You are working with an Elasticsearch index that stores information about books in a library. Each book has fields like title, author, and genre.Your task is to find books that exactly match a specific genre using a term query.
🎯 Goal: Build an Elasticsearch term query to find all books where the genre field exactly matches the value "Science Fiction".
📋 What You'll Learn
Create an Elasticsearch index mapping with fields: title, author, and genre.
Add a configuration variable for the genre to search for, named search_genre with the value "Science Fiction".
Write a term query that searches the genre field for the exact value in search_genre.
Complete the query JSON structure to be ready for execution.
💡 Why This Matters
🌍 Real World
Searching for exact matches in a large collection of documents, such as finding books by genre in a library database.
💼 Career
Understanding term queries is essential for roles involving search engine development, data indexing, and backend search optimization.
Progress0 / 4 steps
1
Create the Elasticsearch index mapping
Create a JSON object called index_mapping that defines an Elasticsearch index mapping with fields title, author, and genre, all of type keyword.
Elasticsearch
Hint
Use a dictionary with mappings and properties keys. Set each field's type to keyword.
2
Add the search genre variable
Create a variable called search_genre and set it to the string "Science Fiction".
Elasticsearch
Hint
Assign the exact string "Science Fiction" to the variable search_genre.
3
Write the term query
Create a JSON object called term_query that uses a term query to search the genre field for the exact value stored in search_genre.
Elasticsearch
Hint
Use the term key inside query and set the genre field to the variable search_genre.
4
Complete the query JSON structure
Add a size field to the term_query JSON object and set it to 10 to limit the number of results returned.
Elasticsearch
Hint
Add the size key at the top level of term_query and set it to 10.
Practice
(1/5)
1. What does a term query in Elasticsearch do?
easy
A. Analyzes and breaks the search term into tokens
B. Finds exact matches of the given term in the data
C. Finds documents with terms similar to the search term
D. Sorts documents based on the term frequency
Solution
Step 1: Understand the purpose of term query
A term query searches for exact matches without analyzing the search term.
Step 2: Compare options with term query behavior
Only Finds exact matches of the given term in the data describes exact matching, which is the core of term query.
Final Answer:
Finds exact matches of the given term in the data -> Option B
Quick Check:
Term query = exact match [OK]
Hint: Term query matches exactly, no analysis or tokenizing [OK]
Common Mistakes:
Confusing term query with match query that analyzes text
Thinking term query sorts results
Assuming term query finds similar or partial matches
2. Which of the following is the correct syntax for a term query in Elasticsearch?
easy
A. { "terms": { "user": ["kimchy"] } }
B. { "match": { "user": "kimchy" } }
C. { "term_query": { "user": "kimchy" } }
D. { "term": { "user": "kimchy" } }
Solution
Step 1: Identify correct term query syntax
The term query uses the key "term" with a field and exact value.
Step 2: Check each option
{ "term": { "user": "kimchy" } } matches the correct syntax. { "match": { "user": "kimchy" } } is a match query, C is invalid, D is a terms query (plural) for multiple values.
Hint: Term query uses "term" key with field and exact value [OK]
Common Mistakes:
Using "match" instead of "term" for exact match
Confusing "term" with "terms" for single value
Adding extra keys like "term_query" which is invalid
3. Given this Elasticsearch query:
{ "query": { "term": { "status": "active" } } }
What documents will this query return?
medium
A. Documents where the status field exactly equals "active"
B. Documents where the status field contains the word "active" anywhere
C. Documents where the status field starts with "active"
D. Documents where the status field is analyzed and matches "active"
Solution
Step 1: Understand term query behavior on field
The term query matches documents where the field value exactly equals the given term without analysis.
Step 2: Analyze each option
Only Documents where the status field exactly equals "active" correctly describes exact equality. Others describe partial or analyzed matches which term query does not do.
Final Answer:
Documents where the status field exactly equals "active" -> Option A
Quick Check:
Term query = exact field value match [OK]
Hint: Term query matches exact field value only [OK]
Common Mistakes:
Thinking term query matches partial or analyzed text
Assuming term query matches prefixes or substrings
Confusing term query with match query behavior
4. You wrote this term query but it returns no results:
{ "query": { "term": { "user": "Kimchy" } } }
Your data has the user field stored as lowercase "kimchy". What is the problem?
medium
A. Term query is case sensitive and "Kimchy" does not match "kimchy"
B. The term query syntax is incorrect
C. The user field is missing in the documents
D. Term query analyzes the term and changes case
Solution
Step 1: Understand term query case sensitivity
Term query matches exact terms and is case sensitive, so "Kimchy" does not match "kimchy".
Step 2: Check other options
Syntax is correct, field exists, and term query does not analyze or change case.
Final Answer:
Term query is case sensitive and "Kimchy" does not match "kimchy" -> Option A
Quick Check:
Term query = exact case match [OK]
Hint: Term query matches exact case; check letter casing [OK]
Common Mistakes:
Assuming term query ignores case
Thinking syntax error causes no results
Believing term query analyzes or lowercases terms
5. You want to find documents where the tags field contains exactly the term urgent. The tags field is an array of keywords. Which query is best to use?
hard
A. { "match": { "tags": "urgent" } }
B. { "terms": { "tags": "urgent" } }
C. { "term": { "tags": "urgent" } }
D. { "term": { "tags.keyword": "urgent" } }
Solution
Step 1: Understand term query on array fields
Term query matches exact terms in arrays, so it works directly on the tags field.
Step 2: Evaluate other options
Match query analyzes text (not exact). Terms query expects multiple values to match any. Using tags.keyword is unnecessary if tags is keyword type.
Final Answer:
{ "term": { "tags": "urgent" } } -> Option C
Quick Check:
Term query matches exact term in array [OK]
Hint: Use term query for exact keyword in array fields [OK]
Common Mistakes:
Using match query which analyzes and may not match exactly
Using terms query incorrectly for single term
Confusing field type and using .keyword unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.