How to Implement Typeahead Search with Elasticsearch
Check out four different methods to implement typeahead search using Elasticsearch for a better end-user experience.
Join the DZone community and get the full member experience.
Join For FreeToday, 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:
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.
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:
PUT movie
{
"mappings": {
"_doc" : {
"properties" : {
"suggest" : {
"type" : "completion"
},
"title" : {
"type": "keyword"
}
}
}
}
}
Index data:
PUT movie/_doc/1?refresh
{
"suggest" : [ "Elevate Me Later", "Covert affairs" ]
}
Query:
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:
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.
Opinions expressed by DZone contributors are their own.
Comments