0
0
Elasticsearchquery~30 mins

Autocomplete with edge n-gram in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Autocomplete with edge n-gram
📖 Scenario: You are building a search feature for a bookstore website. You want users to get suggestions as they type book titles.
🎯 Goal: Create an Elasticsearch index with edge n-gram analyzer to enable autocomplete suggestions on book titles.
📋 What You'll Learn
Create an index called books with edge n-gram analyzer
Add a title field using the edge n-gram analyzer
Index sample book documents with exact titles
Query the index to get autocomplete suggestions for a given prefix
💡 Why This Matters
🌍 Real World
Autocomplete helps users find books quickly by suggesting titles as they type, improving user experience on bookstore websites.
💼 Career
Understanding edge n-gram analyzers and autocomplete queries is useful for search engineers and backend developers working with Elasticsearch.
Progress0 / 4 steps
1
Create the books index with edge n-gram analyzer
Write a JSON request to create an Elasticsearch index called books with a custom analyzer named autocomplete that uses an edge_ngram tokenizer with min_gram 1 and max_gram 10. Define a title field of type text that uses this analyzer for indexing and a standard analyzer for searching.
Elasticsearch
Need a hint?

Define the settings.analysis.analyzer.autocomplete with the edge_ngram_tokenizer. Then map the title field to use this analyzer.

2
Index sample book documents
Index three book documents into the books index with the exact title values: "The Great Gatsby", "Gone with the Wind", and "Great Expectations".
Elasticsearch
Need a hint?

Use POST /books/_doc/<id> to add each book with the exact title.

3
Write the autocomplete query
Write a JSON query to search the books index for autocomplete suggestions on the title field using a match query with the prefix "Gre".
Elasticsearch
Need a hint?

Use a match query on the title field with the value "Gre" to get autocomplete results.

4
Display the autocomplete results
Print the titles of the books returned by the autocomplete query for the prefix "Gre". Assume the query result is stored in a variable called response and print each matching _source.title on a new line.
Elasticsearch
Need a hint?

Loop over response["hits"]["hits"] and print each _source.title.