Autocomplete is a feature to predict the rest of a word a user is typing. It is an important feature to implement that can improve the user’s experience of your product.
Creating an autocomplete might sound daunting at first if you’ve never created one. But with the help of the features in Elasticsearch, it’s actually a simple thing to do.
If you have little knowledge of Elasticsearch, I suggest that you read my other articles first. We do not require this, but knowing how an analyzer and a text field work definitely will help you understand this article.
The article “Basics of Elasticsearch for Developer” will introduce you to Elasticsearch. The article “Elasticsearch: Text vs. Keyword” will teach you the difference between text and keyword in Elasticsearch and also will explain how Elasticsearch’s analyzer works.
Creating the index
First, let’s create an index called
autocomplete-example
. We will use this index for the examples in this article.Defining a mapping
Before indexing a document, let’s first define a mapping. We will only need one field,
simple_autocomplete
, with field data type text and will use a standard analyzer.Since Elasticsearch uses the standard analyzer as default, we need not define it in the mapping.
Indexing a document
Let’s index a document. For the examples in this article, we will only need one document, containing the text “Hong Kong.”
Let’s start with the query that we normally use,
match_query
.The
standard analyzer
will lowercase your indexed text and split the text to tokens on stop words before storing it to an inverted index.The
match_query
by default will use the index-time analyzer, so the analyzer it uses is the same as the one indexed in the index, which is standard analyzer
.Let’s see how our “Hong Kong” text looks in the inverted index with the API provided by the Elasticsearch:
When we do a search query to the index with match query, we will only get a result when we type text containing either “Hong” or “Kong.” This is because Elasticsearch only returns a result when the analyzed query is an exact match with a token in the inverted index.
If the user type “Ho” or “Kon” or “Hon Kon,” there won’t be any response from Elasticsearch.
For an autocomplete, this one isn’t very useful to help the user, right? At the least, autocomplete needs to show something, even if we do not type the full words.
To fix it, we can use a
match_phrase_prefix
query provided by Elasticsearch.match_phrase_prefix
query will allow the user to get a result without typing all the words. By using the usual match query, we won’t get any result from the Elasticsearch if we type “Hon” or “Kon,” but with match_pharse_prefix
, we can get a result.There is still a shortcoming of this autocomplete: If the user types “Hon Kon,” it still won’t return any result. This is because “Hon Kon” is not the prefix of “Hong Kong”.
An autocomplete with a text field data type and the standard analyzer is very simple, but it has pros and cons that you can consider before using this type of autocomplete.
Pros
Cons
I recommend an autocomplete with only the standard analyzer when you only need a simple autocomplete. You can also use this type of autocomplete if the index you want to create an autocomplete of is already in production and indexed with documents. Since this autocomplete uses the default analyzer and default mapping for text, it will work for most text documents.
Creating an autocomplete with the text field data type and standard analyzer is the simplest and easiest autocomplete that we can build with Elasticsearch. It requires almost no setup and can usually create an autocomplete for an existing index.
Even if it’s enough for most use cases, it still has many weaknesses because it can only handle simple queries. To overcome that, we can use a custom-defined analyzer or the Suggesters feature in Elasticsearch, which I plan to write about. Please wait for it!
At last, I want to say thank you to you for reading this article until the end. I hope this article will help you with your project.
Previously published at https://codecurated.com/blog/create-a-simple-autocomplete-with-elasticsearch/