📖 Scenario: You are building a search feature for a bookstore website. You want to help users find book titles quickly by suggesting completions as they type.
🎯 Goal: Create an Elasticsearch index with a completion suggester field, add some book titles, and query the suggester to get autocomplete suggestions.
📋 What You'll Learn
Create an index called books with a title_suggest field of type completion
Add three book documents with titles: 'The Great Gatsby', 'The Grapes of Wrath', and 'Great Expectations'
Write a completion suggester query to suggest titles starting with 'Gr'
Print the suggested book titles from the query response
💡 Why This Matters
🌍 Real World
Autocomplete suggestions help users find items faster on websites and apps, improving user experience.
💼 Career
Knowing how to implement completion suggesters is useful for search engineers, backend developers, and data engineers working with Elasticsearch.
Progress0 / 4 steps
1
Create the Elasticsearch index with completion suggester mapping
Create an index called books with a mapping that includes a title_suggest field of type completion.
Elasticsearch
Hint
Use the PUT method to create the index and define the mapping with title_suggest as a completion type.
2
Add book documents with titles and suggest fields
Add three documents to the books index with these exact titles: 'The Great Gatsby', 'The Grapes of Wrath', and 'Great Expectations'. Each document must have a title_suggest field with the same title as input.
Elasticsearch
Hint
Use PUT /books/_doc/<id> to add each document. The title_suggest field must have an input array with the title string.
3
Write a completion suggester query for prefix 'Gr'
Write a search query on the books index using the _search endpoint. Use a suggest section with a completion suggester named title-suggest that queries the title_suggest field with the prefix 'Gr'.
Elasticsearch
Hint
Use the _search endpoint with a suggest object. The suggester name is title-suggest. Set prefix to 'Gr' and field to title_suggest.
4
Print the suggested book titles from the query response
Print the list of suggested book titles from the title-suggest section of the search response. Extract the text field from each suggestion option.
Elasticsearch
Hint
Loop over response['suggest']['title-suggest'][0]['options'] and print the text field of each option.
Practice
(1/5)
1. What is the main purpose of the completion suggester in Elasticsearch?
easy
A. To update documents in bulk
B. To perform full-text search on documents
C. To aggregate data based on terms
D. To provide fast autocomplete suggestions as users type
Solution
Step 1: Understand the role of completion suggester
The completion suggester is designed to quickly suggest words or phrases as a user types input.
Step 2: Differentiate from other Elasticsearch features
Full-text search, aggregation, and bulk updates serve different purposes unrelated to autocomplete suggestions.
Final Answer:
To provide fast autocomplete suggestions as users type -> Option D
Completion field in properties with type completion = {
"properties": {
"suggest": { "type": "completion" }
}
} [OK]
Hint: Completion field must be under properties with type 'completion' [OK]
Common Mistakes:
Placing completion field outside properties
Using text or keyword type instead of completion
Misnaming mapping keys
3. Given the following suggest query, what will be the expected output if the index contains documents with suggest fields 'apple', 'applet', and 'application'?
Assuming the mapping defines the completion field as suggest (not suggestion).
medium
A. The prefix value 'ban' is invalid
B. The suggest name 'my-suggest' is not allowed
C. The field name in completion should be 'suggest', not 'suggestion'
D. The suggest query must be inside 'query' key
Solution
Step 1: Check field name in completion suggester
The completion suggester must reference the correct field name defined in the mapping. Here, the mapping uses 'suggest', but the query uses 'suggestion'.
Step 2: Validate other parts of the query
The prefix 'ban' is valid, suggest name 'my-suggest' is allowed, and suggest queries do not require a 'query' key.
Final Answer:
The field name in completion should be 'suggest', not 'suggestion' -> Option C
Quick Check:
Field name mismatch causes error [OK]
Hint: Field name in query must match mapping exactly [OK]
Common Mistakes:
Using wrong field name in completion query
Thinking prefix value is invalid
Expecting suggest query inside 'query' key
5. You want to implement a completion suggester that suggests city names but excludes any suggestions that start with the letter 'x'. Which approach correctly applies this filter in the suggest query?
Step 1: Understand filtering with contexts in completion suggester
Completion suggester supports filtering using contexts. To exclude suggestions starting with 'x', you include all allowed first letters except 'x' in the context filter.
Step 2: Analyze each option
{
"suggest": {
"city-suggest": {
"prefix": "",
"completion": {
"field": "city_suggest",
"skip_duplicates": true
}
}
}
} has no filtering. {
"suggest": {
"city-suggest": {
"prefix": "",
"completion": {
"field": "city_suggest",
"contexts": {
"first_letter": ["!x"]
}
}
}
}
} tries to exclude 'x' with '!x' which is invalid syntax. {
"suggest": {
"city-suggest": {
"prefix": "",
"completion": {
"field": "city_suggest",
"contexts": {
"first_letter": ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "z"]
}
}
}
}
} explicitly lists all letters except 'x' in contexts, correctly filtering out 'x'. {
"suggest": {
"city-suggest": {
"prefix": "",
"completion": {
"field": "city_suggest",
"filter": {
"term": { "first_letter": "x" }
}
}
}
}
} uses a 'filter' key which is not supported in completion suggester.