📖 Scenario: You are building a search feature for a website that helps users find products quickly as they type. To make the search fast and helpful, you will create a special field in Elasticsearch called a search-as-you-type field. This field lets Elasticsearch find matches even if the user types only part of a word.
🎯 Goal: Create an Elasticsearch index with a search_as_you_type field called product_name. Then add some sample products. Finally, write a query that finds products as the user types part of the name.
📋 What You'll Learn
Create an index called products with a product_name field of type search_as_you_type
Add three products with exact names: Apple iPhone, Apple Watch, and Samsung Galaxy
Write a search query that uses multi_match with type set to bool_prefix to find products by partial input
Print the names of matching products from the search results
💡 Why This Matters
🌍 Real World
Search-as-you-type fields are used in online stores, libraries, and apps to help users find items quickly by typing only part of the name.
💼 Career
Knowing how to set up and query search-as-you-type fields is useful for backend developers, search engineers, and anyone working with Elasticsearch to build fast and user-friendly search features.
Progress0 / 4 steps
1
Create the Elasticsearch index with a search-as-you-type field
Create an index called products with a mapping that defines a field named product_name of type search_as_you_type. Use the Elasticsearch PUT request with the mapping inside the mappings key.
Elasticsearch
Hint
Use the PUT method to create the index. Inside mappings, define properties with product_name as search_as_you_type.
2
Add sample products to the index
Add three documents to the products index with the exact product_name values: Apple iPhone, Apple Watch, and Samsung Galaxy. Use the POST method to add each document with an id of your choice.
Elasticsearch
Hint
Use POST /products/_doc/<id> to add each product. The product_name field must match exactly.
3
Write a search query using multi_match with bool_prefix
Write a search query using POST /products/_search that uses multi_match with type set to bool_prefix. Search the product_name field for the partial input Apple. This will find products starting with 'Apple'.
Elasticsearch
Hint
Use multi_match with type set to bool_prefix. Include product_name and its _2gram and _3gram subfields in fields.
4
Print the names of matching products from the search results
Print the product_name values from the search results of the previous query. Show only the names of the products that matched the partial input Apple.
Elasticsearch
Hint
Extract the product_name from each hit in the search results and print it. The output should list the matching product names line by line.
Practice
(1/5)
1. What is the main purpose of a search_as_you_type field in Elasticsearch?
easy
A. To provide instant search results as the user types each character
B. To store large binary files efficiently
C. To perform complex aggregations on numeric data
D. To index documents only after full text is entered
Solution
Step 1: Understand the purpose of search_as_you_type
The search_as_you_type field is designed to help users get search results instantly as they type, improving user experience.
Step 2: Compare with other options
Options B, C, and D describe unrelated Elasticsearch features like binary storage, aggregations, or delayed indexing, which are not related to search_as_you_type.
Final Answer:
To provide instant search results as the user types each character -> Option A
Quick Check:
search_as_you_type = instant search while typing [OK]
Hint: Remember: search_as_you_type means live search suggestions [OK]
Common Mistakes:
Confusing search_as_you_type with binary or numeric fields
Thinking it delays indexing until full input
Assuming it is for aggregations
2. Which is the correct way to define a search_as_you_type field in an Elasticsearch mapping?
easy
A. "title": { "type": "text" }
B. "title": { "type": "completion" }
C. "title": { "type": "keyword" }
D. "title": { "type": "search_as_you_type" }
Solution
Step 1: Identify the correct field type for search-as-you-type
The search_as_you_type field type must be explicitly set as "type": "search_as_you_type" in the mapping.
Step 2: Eliminate other field types
"title": { "type": "text" } uses text, which is for full text search but not optimized for search-as-you-type. "title": { "type": "keyword" } uses keyword, which is for exact matches. "title": { "type": "completion" } uses completion, which is for autocomplete but different from search_as_you_type.
Final Answer:
"title": { "type": "search_as_you_type" } -> Option D
Quick Check:
Mapping type for search-as-you-type = search_as_you_type [OK]
Hint: Use type "search_as_you_type" exactly in mapping [OK]
A. The field 'title' must be of type 'keyword' for this query
B. The 'bool_prefix' type is invalid for multi_match queries
C. The query should include ngram subfields like 'title._2gram' and 'title._3gram'
D. The query string 'app' is too short to match anything
Solution
Step 1: Check query fields for search_as_you_type
Search-as-you-type fields generate subfields with ngrams like '_2gram' and '_3gram' that must be included in the query fields for prefix matching.
Step 2: Identify missing ngram subfields in the query
The query only searches 'title' field, missing 'title._2gram' and 'title._3gram', so it won't match prefixes properly.
Final Answer:
The query should include ngram subfields like 'title._2gram' and 'title._3gram' -> Option C
Quick Check:
Include ngram subfields in fields list for prefix search [OK]
Hint: Always add _2gram and _3gram subfields in multi_match fields [OK]
Common Mistakes:
Omitting ngram subfields in fields list
Thinking bool_prefix is invalid
Assuming keyword type is needed
Blaming query length
5. You want to implement a search-as-you-type feature for a product catalog with a name field. Which of the following mapping and query combinations will correctly support prefix matching as the user types?