Building Search API with ElasticSearch — Part 09: Recommendation with More Like This Query

André Coelho
3 min readJul 12, 2022

--

Having a recommendation functionality can be very useful when you want to provide the user with relevant content in their search context.
There are several tools on the market focused on this strategy, which can reduce the complexity of an implementation like this. However, if you want to develop your own recommendation tool, there are several articles and learning models that can help with this task.

The intention of this post is not to demonstrate the creation of a recommendation system but to create a simple recommendation of documents similar to another document.

How will this be done? It is through More Like This Query. In the post Using More Like This Query (MLT) — ElasticSearch, we created a query to retrieve documents similar to one another and we are going to apply this same approach to retrieve more documents.

Java Code

Our More Like This Query is implemented in SearchCoreService. We receive the parameter “code” and retrieve the corresponding movie.
Then the id of the retrieved document will be the parameters for MLT to retrieve more movie. Note that we have defined the fields “title”, “genres” and “director” where searches and analysis will be carried out.

In our SearchController we create a new endpoint because we separate retrieval from traditional searches and MLT search.

Running tests

Let’s call the “/mlt” endpoint to retrieve the movies related to “The Departed”

curl --location --request GET 'http://localhost:8080/api/movies/mlt?code=38&size=5'

MLT returned an interesting list of documents. All films are from the same director, this is due to the “director” field used in the query. In this way, a recommendation of more films by the same director can be interesting to arouse the user’s interest in the other films.

Result MLT Query

A new search to find films similar to “The Matrix” returns a list of films that are not by the same director but due to the genre, we have retrieved a list of films that may be considered relevant recommendations.

Results MLT Query

We have reached the end of building features in our API, in the next post we will present our interface and show how to use Elastic APM for application observability.

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.