In an earlier , we discussed various approaches to implement Autocomplete . We came to a conclusion that Completion Suggester covers most of the cases required in implementing a fully functional and fast autocomplete. This post explains in detail about what is Completion Suggester and how to use them practically. post functionality Completion Suggester is a type of suggester in , which is used to implement autocomplete functionality. It uses an in-memory data structure called . stores FST on a per segment basis, which means suggestions scale horizontally as more new nodes are added. Completion Suggester Elasticsearch Finite State Transducer Elasticsearch Mapping To use Completion Suggester, a special type of mapping type called is defined. Let’s take an example of Marvel movie data and define an index named with type as . Complete movie list can be accessed from completion movies marvels here Here is a type of completion field. In this field, we can add various other mapping parameters like , , etc. name.completion analyzer search_analyzer Indexing Data To index data, a slightly different syntax is used. A suggestion field is made of an and an optional parameter. Let’s index a movie into our index. input weight movies We can also define a for each field. This weight can help us in controlling the ranking of documents when querying. weight We can also index multiple suggestions for a document at the same time Querying To query document, we need to specify suggest type as . Let’s query for in our index. index contains all the 22 movies from Marvel Cinematic Section of this . completion thor movies movies page We get the following movies as result Thor Thor: Ragnarok Thor: The Dark World We see that all documents are having as 1. This means that all the documents in completion suggestor are ranked equally. To give boost to a particular document, or to alter the ranking, we can use the optional parameter called . We have already indexed (with no weight) and (with weight as 2). Let’s search for in our movies index. _score weight Iron Man Iron Man 2 Iron Man We get the following movies as result Iron Man 2 (score as 2) Iron Man (score as 1). We can clearly see here how weight is used to control the ranking of documents. This is the reason why is ranked higher than when searched for . Iron Man 2 Iron Man Iron Man We can also specify the to control the number of documents returned. size We can also add fuzziness in completion suggester. This helps us in providing suggestions even when there is a typo. Let’s try searching for with fuzzy query captain amrica the We get the following movies as result Captain America: The First Avenger Captain America: The Winter Soldier Let’s try finding suggestion for movie names which contain . america We get no results. This is because completion suggester support prefix matching. It starts matching from the start of the string and there is no movie which contains at the start of the string. To deal with this type of situation, we can tokenize the input text on space and keep all the phrases as canonical names. This way will be inserted as america Captain America: The First Avenger Filtering Document In queries, we can filter documents by using but filter does not work in Completion Suggester. To understand this better, let’s run a query which finds all movies with name released in year . filter iron man 2008 The response received looks like In the response, we see that key along with is present. This happened because and works at the same level parallely. Hence we get both keys in response. So we cannot apply filter in a suggestion query. hits suggest query suggest To deal with this, Completion Suggester provides , which are basically filters for field. Let’s define another mapping for movies index, this time with as a context suggester for field. Context Suggester completion year name We can index our complete movies data into this index. Let’s find all movies with name released in year . iron man 2008 We get the following movies as result Iron Man We can also boost context suggester as well. Let’s search for movies with name as , released in year and , giving a boost of 4 to year . iron man 2008 2010 2008 We get the following movies as the result Iron Man (score as 4) Iron Man 2 (score as 1) References Completion Suggester Elasticsearch Context Suggester