0
0
Elasticsearchquery~3 mins

Why Autocomplete with edge n-gram in Elasticsearch? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your search box could guess your words instantly, saving you time and frustration?

The Scenario

Imagine typing a search query on a website and waiting for the page to reload every time you add a letter, hoping it guesses what you want.

Without autocomplete, you must type the full word and submit the search to see results.

The Problem

This manual approach is slow and frustrating because the system can only search after you finish typing.

It also makes many mistakes because it can't suggest partial matches as you type.

Users often get annoyed and leave the site.

The Solution

Autocomplete with edge n-gram breaks words into smaller pieces starting from the beginning, so the system can quickly suggest matches as you type each letter.

This makes searching fast and smooth, giving instant suggestions and improving user experience.

Before vs After
Before
GET /products/_search
{
  "query": {
    "match": {
      "name": "lap"
    }
  }
}
After
PUT /products
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete_tokenizer"
        }
      },
      "tokenizer": {
        "autocomplete_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20,
          "token_chars": ["letter"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "standard"
      }
    }
  }
}
What It Enables

It enables instant, smart search suggestions that update as you type, making search faster and more user-friendly.

Real Life Example

When you start typing "lap" in an online store's search box, autocomplete with edge n-gram suggests "laptop", "lapel pin", or "lap desk" immediately, helping you find products faster.

Key Takeaways

Manual search waits for full input, causing delays and frustration.

Edge n-gram breaks words into starting pieces for quick matching.

Autocomplete with edge n-gram improves speed and user experience by suggesting results as you type.