0
0
Elasticsearchquery~30 mins

Completion suggester in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Completion Suggester in Elasticsearch
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Loop over response['suggest']['title-suggest'][0]['options'] and print the text field of each option.