A search-as-you-type field helps users find what they want quickly by showing results while they type. It makes searching faster and easier.
Search-as-you-type field in Elasticsearch
PUT /my_index
{
"mappings": {
"properties": {
"my_field": {
"type": "search_as_you_type"
}
}
}
}The search_as_you_type field type creates special subfields to support fast prefix matching.
You use this field type in your index mapping before adding documents.
name field that supports search-as-you-type for product names.PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "search_as_you_type"
}
}
}
}name field and its subfields.GET /products/_search
{
"query": {
"multi_match": {
"query": "iph",
"type": "bool_prefix",
"fields": [
"name",
"name._2gram",
"name._3gram"
]
}
}
}This example creates an index called library with a title field that supports search-as-you-type. It adds two books and then searches for titles starting with "Grea".
PUT /library
{
"mappings": {
"properties": {
"title": {
"type": "search_as_you_type"
}
}
}
}
POST /library/_doc/1
{
"title": "The Great Gatsby"
}
POST /library/_doc/2
{
"title": "Great Expectations"
}
GET /library/_search
{
"query": {
"multi_match": {
"query": "Grea",
"type": "bool_prefix",
"fields": [
"title",
"title._2gram",
"title._3gram"
]
}
}
}Search-as-you-type fields create hidden subfields with n-grams to speed up prefix searches.
Use multi_match with bool_prefix type to query these fields effectively.
Remember to reindex your data after changing mappings.
Search-as-you-type fields help users find matches while typing.
Define the field with type: search_as_you_type in your mapping.
Use multi_match queries with bool_prefix to search these fields.
Practice
search_as_you_type field in Elasticsearch?Solution
Step 1: Understand the purpose of search_as_you_type
Thesearch_as_you_typefield 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 tosearch_as_you_type.Final Answer:
To provide instant search results as the user types each character -> Option AQuick Check:
search_as_you_type = instant search while typing [OK]
- Confusing search_as_you_type with binary or numeric fields
- Thinking it delays indexing until full input
- Assuming it is for aggregations
search_as_you_type field in an Elasticsearch mapping?Solution
Step 1: Identify the correct field type for search-as-you-type
Thesearch_as_you_typefield type must be explicitly set as"type": "search_as_you_type"in the mapping.Step 2: Eliminate other field types
"title": { "type": "text" } usestext, which is for full text search but not optimized for search-as-you-type. "title": { "type": "keyword" } useskeyword, which is for exact matches. "title": { "type": "completion" } usescompletion, which is for autocomplete but different from search_as_you_type.Final Answer:
"title": { "type": "search_as_you_type" } -> Option DQuick Check:
Mapping type for search-as-you-type = search_as_you_type [OK]
- Using "text" instead of "search_as_you_type"
- Confusing with "completion" type
- Omitting the type field
{
"mappings": {
"properties": {
"name": { "type": "search_as_you_type" }
}
}
}Which query will correctly search for documents where the
name field starts with "mic"?Solution
Step 1: Understand how to query search_as_you_type fields
Search-as-you-type fields require amulti_matchquery withtype: bool_prefixand fields including the base field and its n-gram subfields.Step 2: Analyze each query option
{ "match": { "name": "mic" } } uses simple match, which does not support prefix matching well. { "term": { "name": "mic" } } uses term query, which requires exact match. { "prefix": { "name": "mic" } } uses prefix query, which is not optimized for search_as_you_type. { "multi_match": { "query": "mic", "type": "bool_prefix", "fields": ["name", "name._2gram", "name._3gram"] } } correctly uses multi_match with bool_prefix and the required fields.Final Answer:
{ "multi_match": { "query": "mic", "type": "bool_prefix", "fields": ["name", "name._2gram", "name._3gram"] } } -> Option BQuick Check:
Use multi_match + bool_prefix + ngram fields [OK]
- Using term or prefix queries alone
- Not including ngram subfields in fields list
- Using simple match query for prefix search
search_as_you_type field but get no results:{ "multi_match": { "query": "app", "type": "bool_prefix", "fields": ["title"] } }What is the likely problem?
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 CQuick Check:
Include ngram subfields in fields list for prefix search [OK]
- Omitting ngram subfields in fields list
- Thinking bool_prefix is invalid
- Assuming keyword type is needed
- Blaming query length
name field. Which of the following mapping and query combinations will correctly support prefix matching as the user types?Solution
Step 1: Choose the correct mapping for search-as-you-type
Thesearch_as_you_typetype is designed for prefix matching and instant search as the user types.Step 2: Use the correct query for prefix matching
Themulti_matchquery withbool_prefixtype and fields including the base field and its ngram subfields is required for effective prefix matching.Step 3: Evaluate other options
Mapping: { "name": { "type": "text" } }
Query: { "match": { "name": "lap" } } usestextwith simple match, which does not support prefix search well. Mapping: { "name": { "type": "keyword" } }
Query: { "prefix": { "name": "lap" } } useskeywordwith prefix query, which is less flexible. Mapping: { "name": { "type": "completion" } }
Query: { "term": { "name": "lap" } } usescompletionwith term query, which is incorrect usage.Final Answer:
Mapping: { "name": { "type": "search_as_you_type" } } and Query: { "multi_match": { "query": "lap", "type": "bool_prefix", "fields": ["name", "name._2gram", "name._3gram"] } } -> Option AQuick Check:
search_as_you_type + multi_match bool_prefix + ngram fields = correct [OK]
- Using text type with match query for prefix search
- Using keyword type with prefix query only
- Confusing completion type with search_as_you_type
- Using term query for prefix matching
Ready to prove it?
Pass the test and this pattern moves unknown
