Elasticsearch Implementing Search-as-You-Type in Elasticsearch

By Opster Team

Updated: Jul 23, 2023

| 2 min read

Introduction

Search-as-you-type is a popular feature in modern applications, providing users with real-time search results as they type their query. Elasticsearch, a powerful and scalable search engine, can be configured to provide this functionality. In this article, we will discuss how to implement search-as-you-type in Elasticsearch using edge n-grams and the completion suggester.

1. Edge N-Grams

Edge n-grams are a technique used to break down text into smaller tokens, starting from the beginning of the text. This allows Elasticsearch to match partial words, providing search results as the user types.

Step-by-step implementation:

1.1. Create an index with a custom analyzer

First, create an index with a custom analyzer that uses the edge_ngram tokenizer. This tokenizer will generate edge n-grams for the indexed text.

PUT /search_as_you_type
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "search_autocomplete": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "search_autocomplete"
      }
    }
  }
}

1.2. Index sample documents

Index some sample documents to test the search-as-you-type functionality.

POST /search_as_you_type/_doc
{
  "title": "Elasticsearch Guide"
}

POST /search_as_you_type/_doc
{
  "title": "Elasticsearch Advanced Techniques"
}

1.3. Test the search-as-you-type functionality

Now, test the search-as-you-type functionality by running a search query.

GET /search_as_you_type/_search
{
  "query": {
    "match": {
      "title": "elas"
    }
  }
}

The search results should include both sample documents, as the query “elas” matches the beginning of the word “Elasticsearch” in both titles.

2. Completion Suggester

The completion suggester is another approach to implementing search-as-you-type in Elasticsearch. It provides fast and efficient auto-complete suggestions based on user input.

Step-by-step implementation:

2.1. Create an index with a completion field

Create an index with a completion field to store the suggestions.

PUT /suggestions
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "suggest": {
        "type": "completion"
      }
    }
  }
}

2.2. Index sample documents with suggestions

Index sample documents with the “suggest” field containing the input for auto-complete suggestions.

POST /suggestions/_doc
{
  "title": "Elasticsearch Guide",
  "suggest": {
    "input": ["Elasticsearch", "Guide"]
  }
}

POST /suggestions/_doc
{
  "title": "Elasticsearch Advanced Techniques",
  "suggest": {
    "input": ["Elasticsearch", "Advanced", "Techniques"]
  }
}

2.3. Test the search-as-you-type functionality

Test the search-as-you-type functionality using the _search endpoint with the “suggest” field.

GET /suggestions/_search
{
  "suggest": {
    "title-suggestion": {
      "prefix": "elas",
      "completion": {
        "field": "suggest"
      }
    }
  }
}

The search results should include both sample documents as suggestions, as the query “elas” matches the beginning of the word “Elasticsearch” in both titles.

Conclusion

In conclusion, Elasticsearch provides multiple ways to implement search-as-you-type functionality, such as edge n-grams and the completion suggester. Choose the approach that best fits your application’s requirements and performance needs.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?