DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How We Rebuilt a Legacy HBase + Elasticsearch System Using Apache Iceberg, Spark, Trino, and Doris
  • Set Up Spring Data Elasticsearch With Basic Authentication
  • Introduction to Spring Data Elasticsearch 5.5
  • Keep Your Search Cluster Fit: Essential Health Checks to Keep Elasticsearch Healthy

Trending

  • The Update Problem REST Doesn't Solve
  • Unlocking Smart Meter Insights with Smart Datastream
  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • 5 Security Considerations for Deploying AI on Edge Devices
  1. DZone
  2. Data Engineering
  3. Big Data
  4. How to Implement Typeahead Search with Elasticsearch

How to Implement Typeahead Search with Elasticsearch

Check out four different methods to implement typeahead search using Elasticsearch for a better end-user experience.

By 
Praveen KG user avatar
Praveen KG
·
Oct. 01, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
18.0K Views

Join the DZone community and get the full member experience.

Join For Free

Today, search is an important functionality in enterprise applications and end-users are obsessed with the experience of Google Search and expecting the application search also provides similar experiences.

This requires us to design and implement a search engine along with your golden source (RDBMS/NOSQL). There are many search engines available in the market today like Elasticsearch, Apache Solr, Azure Cognitive Search, etc. These provide a better search experience and features like typeahead, fuzzy search, boosting the search results based on relevancy, similarity search, etc.

What Is Typeahead Search?

Typeahead search, also known as autosuggest or autocomplete feature, is a way of filtering out the data by checking if the user input data is a subset of the data. If so, all the partially matched texts to the user are a way of providing hints when typing the text. This feature certainly helps the end users' experience while searching.

How to Achieve Typeahead Search With Elasticsearch

Elasticsearch provides four different ways to achieve the typeahead search. Let's take a look at all these four approaches and see which approach is optimal and has a better implementation:

  • Match Phrase Prefix
  • Edge Ngram
  • Completion Suggester
  • Search-as-you-type

Match Phrase Prefix Query

In this approach, we need to use the prefix query against the search field. The query returns the documents that contain the words of a provided prefix text, in the same order as provided. 

For example:

JSON
 
GET /_search
{
  "query": {
    "match_phrase_prefix": {
      "message": {
        "query": "quick brown f"
      }
    }
  }
}


The above search returns documents quick brown fox or two quick brown ferrets but not the fox is quick and brown as the query will match phrases beginning with quick brown f-

Edge Ngram

With this approach, we need to configure the custom analyzer with an edge-n-gram filter.

JSON
 
PUT myIndex
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "lowercase"
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 5,
          "token_chars": [
            "letter"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "autocomplete",
        "search_analyzer": "autocomplete_search"
      }
    }
  }
}


A few points to be considered while implementing this approach:

  • It's better to use the same analyzer for both index and search
  • Since the tokenizer breaks the text down into words on custom characters, it might increase the index time
  • For the same reason mentioned above, it will increase the index storage

Completion Suggester

Elasticsearch provides a Completion Suggester as a native solution for auto-complete/search-as-you-type functionality. The auto-complete functionality should be as fast as the user types to provide instant feedback relevant to what a user has already typed in. Hence, the completion suggester uses data structures that enable fast lookups but are costly to build and are stored in memory.

How to set up Completion Suggester?

To use the Completion Suggester, we need special mapping for the field:

JSON
 
PUT movie
{
    "mappings": {
        "_doc" : {
            "properties" : {
                "suggest" : {
                    "type" : "completion"
                },
                "title" : {
                    "type": "keyword"
                }
            }
        }
    }
}


Index data:

JSON
 
PUT movie/_doc/1?refresh
{
  "suggest" : [ "Elevate Me Later", "Covert affairs" ]
}


Query:

JSON
 
POST movie/_search?pretty
{
    "suggest": {
        "movie-suggest" : {
            "prefix" : "ele", 
            "completion" : { 
                "field" : "suggest",
                "skip_duplicates": true
            }
        }
    }
}


Search-As-You-Type

Search-as-you-type is field type is optimized to provide out-of-the-box support for typeahead search or as-you-type completion use cases. Search-as-you-type mapping creates a number of subfields and indexes the data by analyzing the terms, that help to partially match the indexed text value. It supports both prefix completion and infix completion.

To configure search-as-you-type, add the below mapping for your index field:

JSON
 
PUT search-index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "search_as_you_type"
      }
    }
  }
}


You can look at either search-as-you-type or completion suggester for implementing the typeahead functionality using Elasticsearch. Though with match phrase prefix or edge-n-gram, we can achieve the same, search-as-you-type wraps this implementation internally and provides efficient and optimized functionality as a field type.

Elasticsearch

Opinions expressed by DZone contributors are their own.

Related

  • How We Rebuilt a Legacy HBase + Elasticsearch System Using Apache Iceberg, Spark, Trino, and Doris
  • Set Up Spring Data Elasticsearch With Basic Authentication
  • Introduction to Spring Data Elasticsearch 5.5
  • Keep Your Search Cluster Fit: Essential Health Checks to Keep Elasticsearch Healthy

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook