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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Building a Cost-Effective ELK Stack for Centralized Logging
  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • How to Scale Elasticsearch to Solve Your Scalability Issues

Trending

  • Fixing Common Oracle Database Problems
  • Understanding and Mitigating IP Spoofing Attacks
  • Performance Optimization Techniques for Snowflake on AWS
  • Build an MCP Server Using Go to Connect AI Agents With Databases
  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
17.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

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Building a Cost-Effective ELK Stack for Centralized Logging
  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • How to Scale Elasticsearch to Solve Your Scalability Issues

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!