Search-as-you-type field in Elasticsearch - Time & Space Complexity
When using a search-as-you-type field in Elasticsearch, it is important to understand how the search speed changes as the data grows.
We want to know how the number of operations grows when more documents or longer queries are involved.
Analyze the time complexity of the following Elasticsearch query using a search-as-you-type field.
GET /my_index/_search
{
"query": {
"match": {
"name.search_as_you_type": "appl"
}
}
}
This query searches the "name" field with a search-as-you-type mapping, matching documents as the user types partial input.
Look at what repeats when this query runs.
- Primary operation: Elasticsearch scans inverted index terms matching the typed prefix.
- How many times: It checks terms that start with the input prefix, then fetches matching documents for each term.
As the input prefix gets longer, fewer terms match, so fewer documents are checked.
| Input Size (prefix length) | Approx. Operations |
|---|---|
| 1 (e.g., "a") | Many terms match, many documents checked |
| 3 (e.g., "app") | Fewer terms, fewer documents |
| 5 (e.g., "apple") | Very few terms, minimal documents |
Pattern observation: The longer the typed prefix, the fewer terms and documents Elasticsearch processes, so execution cost decreases with input length.
Time Complexity: O(k)
This means the search cost grows roughly with the number of matching terms and documents for the typed prefix, which depends on prefix length and index size.
[X] Wrong: "The search cost is always the same no matter how long the typed input is."
[OK] Correct: Actually, longer prefixes narrow down matches, so fewer terms and documents are checked, reducing cost.
Understanding how search-as-you-type queries scale helps you explain real-world search performance and design better user experiences.
What if we changed the search-as-you-type field to a standard text field? How would the time complexity change?
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
