A user waiting 200 ms for an autocomplete response kills UX. In one electronics e-commerce project, every typed character triggered a full-text search query against the database. Server load increased, speed dropped, and customers left. The solution — Elasticsearch Completion Suggester. This mechanism, based on FST (Finite State Transducer), stores data in memory and returns responses in 1–5 ms. We have implemented it in over 50 projects over 10+ years and guarantee stable performance under load.
This guide covers Elasticsearch autocomplete setup using Completion Suggester, comparing it with Edge N-gram. How it works: you type "no" — the system returns "notebook", "socks", "november" without delay. The suggester uses a completion field containing an input array (autocomplete options) and an optional weight (priority). For popular products, the weight is set based on sales or clicks.
How to Implement Completion Suggester for Autocomplete?
Step 1: Define the Mapping
Completion Suggester is the fastest option but less flexible: no full-text scoring, limited filtering. Mapping for completion field:
PUT /products
{
"mappings": {
"properties": {
"title": { "type": "text" },
"suggest": {
"type": "completion",
"analyzer": "simple",
"search_analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50
}
}
}
}
Step 2: Index Documents with Weights
Indexing a document with weight (e.g., higher for popular products):
PUT /products/_doc/1
{
"title": "Ноутбук ASUS VivoBook 15",
"suggest": {
"input": ["Ноутбук ASUS VivoBook", "ASUS VivoBook 15", "VivoBook"],
"weight": 10
}
}
Step 3: Query with Fuzzy and Duplicate Skipping
Autocomplete query with fuzzy:
POST /products/_search
{
"suggest": {
"product-suggest": {
"prefix": "ноут",
"completion": {
"field": "suggest",
"size": 5,
"skip_duplicates": true,
"fuzzy": { "fuzziness": 1 }
}
}
},
"_source": ["title", "price"],
"size": 0
}
The response returns options with _source fields. Response time — 1–5 ms.
Choosing Between Completion Suggester and Edge N-gram
The choice depends on priority: speed vs. flexibility. The suggester is 10x faster than N-gram under the same load — confirmed by our load tests. If your data is static (product catalog, city list) and maximum speed is needed — choose it. If full-text search with filtering and sorting is required — Edge N-gram.
What is Context Suggester and How to Use It?
Context Suggester extends the completion mechanism with filtering by context — for example, by product category. Mapping with context:
PUT /products
{
"mappings": {
"properties": {
"suggest": {
"type": "completion",
"contexts": [
{ "name": "category", "type": "category", "path": "category" }
]
},
"category": { "type": "keyword" }
}
}
}
Indexing with context:
PUT /products/_doc/1
{
"title": "Ноутбук ASUS",
"category": "laptops",
"suggest": {
"input": ["Ноутбук ASUS", "ASUS"],
"contexts": { "category": ["laptops"] }
}
}
Query with context and boost:
POST /products/_search
{
"suggest": {
"product-suggest": {
"prefix": "ас",
"completion": {
"field": "suggest",
"size": 5,
"contexts": {
"category": [
{ "context": "laptops", "boost": 2 },
{ "context": "tablets" }
]
}
}
}
}
}
Context Suggester increases relevance: if the user is in the "Laptops" section, suggestions show only laptops. In one project, we achieved a 30% increase in suggestion click-through rate due to accuracy.
What are the Limitations of Completion Suggester?
Edge N-gram: Alternative with Full-Text Search
Edge N-gram indexes partial tokens (minimum length 2 characters) and uses standard match query with full TF/IDF scoring, filtering, and sorting. Example mapping:
PUT /products
{
"settings": {
"analysis": {
"tokenizer": {
"edge_ngram_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20,
"token_chars": ["letter", "digit"]
}
},
"analyzer": {
"autocomplete_index": {
"type": "custom",
"tokenizer": "edge_ngram_tokenizer",
"filter": ["lowercase"]
},
"autocomplete_search": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete_index",
"search_analyzer": "autocomplete_search"
}
}
}
}
Query:
POST /products/_search
{
"query": {
"match": { "title": { "query": "ноут", "operator": "and" } }
},
"sort": [{ "_score": "desc" }],
"size": 5
}
| Feature | Completion Suggester | Edge N-gram |
|---|---|---|
| Response speed | 1–5 ms | 10–50 ms |
| Query flexibility | Limited (prefix) | Full (match, filtering, sorting) |
| Memory usage | High (FST) | Medium (inverted index) |
| Typo support | Built-in fuzzy | Requires separate analyzer |
| Best use case | Static data with weights | Dynamic data with full-text search |
Typical Autocomplete Setup Mistakes
- Forgetting
skip_duplicates: without it, duplicate options may appear. - Not setting
max_input_length— default is 50, may be insufficient for long names. - Using
size: 0in the main query but forgetting_source— extra fields are returned. - Not configuring debounce on the frontend: sending a request on every character creates a request avalanche. We recommend a 200–300 ms delay and caching previous results.
Server-Side Implementation Example
Example in PHP using elasticsearch-php:
public function autocomplete(string $query): array
{
$response = $this->client->search([
'index' => 'products',
'body' => [
'suggest' => [
'product-suggest' => [
'prefix' => mb_strtolower($query),
'completion' => [
'field' => 'suggest',
'size' => 8,
'skip_duplicates' => true,
'fuzzy' => ['fuzziness' => 'AUTO'],
]
]
],
'_source' => ['title', 'slug', 'price'],
'size' => 0,
]
]);
return array_map(fn($opt) => [
'title' => $opt['text'],
'price' => $opt['_source']['price'],
], $response['suggest']['product-suggest'][0]['options'] ?? []);
}
On the frontend, be sure to add a 200–300 ms debounce and cache previous results to avoid overloading the server.
What's Included in Our Autocomplete Solution
Our complete autocomplete implementation package includes:
- Requirements analysis and selection of optimal mechanism (Completion Suggester vs Edge N-gram)
- Mapping design and index configuration
- API endpoint implementation with fuzzy and context support
- Frontend integration (debounce, caching)
- Full documentation and code repository access
- Training session for your developers
- One month post-deployment support
- Response time guarantee of under 5 ms
Pricing: Basic Completion Suggester setup starts at $500. A full solution with Context Suggester and Edge N-gram ranges from $1,000 to $2,000, depending on complexity. Typical savings from reduced server load and improved conversion rates can exceed $5,000 per month for high-traffic e-commerce sites.
Timeline: We can set up a basic autocomplete in 1 day. Adding Context Suggester takes another half day. Full Edge N-gram integration with all features requires 1–2 days. Contact us for a free project assessment—we'll estimate your exact needs and provide a fixed price.
Elasticsearch official documentation recommends using Completion Suggester for high-speed autocomplete scenarios.







