Retrieve related search results
This guide shows you how to use the similar documents endpoint to create an AI-powered movie recommendation workflow.
First, you will create an embedder and add documents to your index. You will then perform a search, and use the top result's primary key to retrieve similar movies in your database.
Prerequisites
- A running Meilisearch project
- A tier >=2 OpenAI API key
Create a new index
Create an index called movies
and add this movies.json
dataset to it. If necessary, consult the getting started for more instructions on index creation.
Each document in the dataset represents a single movie and has the following structure:
id
: a unique identifier for each document in the databasetitle
: the title of the movieoverview
: a brief summary of the movie's plotgenres
: an array of genres associated with the movieposter
: a URL to the movie's poster imagerelease_date
: the release date of the movie, represented as a Unix timestamp
Configure an embedder
Next, use the Cloud UI to configure an OpenAI embedder:
You may also use the /settings/embedders
API subroute to configure your embedder:
curl -X PATCH 'MEILISEARCH_URL/indexes/movies/settings'
-H 'Content-Type: application/json'
-H 'Authorization: Bearer MEILISEARCH_API_KEY'
--data-binary '{
"embedders": {
"movies-text": {
"source": "openAi",
"apiKey": "OPENAI_API_KEY",
"model": "text-embedding-3-small",
"documentTemplate": "A movie titled '{{doc.title}}' released in {{ doc.release_date }}. The movie genres are: {{doc.genres}}. The story is about: {{doc.overview|truncatewords: 20}}"
}
}
}'
Replace MEILISEARCH_URL
, MEILISEARCH_API_KEY
, and OPENAI_API_KEY
with the corresponding values in your application.
Meilisearch will start generating the embeddings for all movies in your dataset. Use the returned taskUid
to track the progress of this task. Once it is finished, you are ready to start searching.
Perform a hybrid search
With your documents added and all embeddings generated, you can perform a search:
curl -X POST 'MEILISEARCH_URL/indexes/INDEX_NAME/search' \
-H 'content-type: application/json' \
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
--data-binary '{
"q": "batman",
"hybrid": {
"embedder": "EMBEDDER_NAME"
}
}'
This request returns a list of movies. Pick the top result and take note of its primary key in the id
field. In this case, it's the movie "Batman" with id
192.
Return similar documents
Pass "Batman"'s id
to your index's /similar
route, specifying movies-text
as your embedder:
curl \
-X POST 'MEILISEARCH_URL/indexes/movies/similar' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
--data-binary '{
"id": 192,
"embedder": "EMBEDDER_NAME"
}'
Meilisearch will return a list of the 20 documents most similar to the movie you chose. You may then choose to display some of these similar results to your users, pointing them to other movies that may also interest them.
Conclusion
Congratulations! You have successfully built an AI-powered movie search and recommendation system using Meilisearch by:
- Setting up a Meilisearch project and configured it for AI-powered search
- Implementing hybrid search combining keyword and semantic search capabilities
- Integrating Meilisearch's similarity search for movie recommendations
In a real-life application, you would now start integrating this workflow into a front end, like the one in this official Meilisearch blog post.