Building Search API with ElasticSearch — Part 03: Autocomplete

André Coelho
3 min readJul 6, 2022

--

Our next implementation will be autocomplete. There are a few ways to implement an autocomplete solution and we are going to choose the Completion Suggester.

Mapping

Completion Suggester requires a field type called “completion”. We created the field “title_suggest” in our index and it will be used for this purpose.

      "title_suggest": {
"type": "completion"
}

The inputs have already been created in our bulk insert script. Simply apply a split function on the titles of the movies and the generated array is the input of the field “title_suggest”.

Java Code

The implementation using the Java API Client is quite simple. In ServiceCore we created the query Suggester passing the term as a parameter and configuring the fuzzy parameters that served to help correct terms.

To extract the suggestions from the query, it is necessary to access the Suggester object in SearchResponse, in SearchService. Inside the Suggester will be the Map with the key “title-suggest” and the title suggestions for the informed term.
We retrieved all the suggestions and created a list of suggested titles for the user.

Running tests

Our first test will be the search for the term “love”.

curl --location --request GET 'http://localhost:8080/api/movies/autocomplete?size=10&text=love'

Note that the title list has the term “love” in its composition.

Result search term “love”

In this test we want to have the suggestion of the movie Interstellar, and we will start by writing “inter”

curl --location --request GET 'http://localhost:8080/api/movies/autocomplete?size=10&text=inter'
Result for term “inter”

Writing a little closer to the title Interstellar.

curl --location --request GET 'http://localhost:8080/api/movies/autocomplete?size=10&text=interste'

The closest suggestion is exactly the movie we want.

Response for term “interste”

We can see the power of Completion Suggester for autocomplete solutions. We were able to get suggestions close to the terms we reported.

In the next post we will create a more complex query to search for the most relevant movies for the input terms.

Building Search API with ElasticSearch — Part 01: Preparing the environment
Building Search API with ElasticSearch — Part 02: Pagination
Building Search API with ElasticSearch — Part 03: Autocomplete
Building Search API with ElasticSearch — Part 04: Search Query
Building Search API with ElasticSearch — Part 05: Simple Facets
Building Search API with ElasticSearch — Part 06: Filters
Building Search API with ElasticSearch — Part 07: “Did you mean”
Building Search API with ElasticSearch — Part 08: Sort results
Building Search API with ElasticSearch — Part 09: Recommendation with More Like This Query
Building Search API with ElasticSearch — Part 10: Front-End
Building Search API with ElasticSearch — Part 11: Elastic APM

--

--

André Coelho

Developer of web and mobile systems. Enthusiast in the area of ​​automation and electronics and I have hobbie music.