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

    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:

    Configure an embedder

    Next, use the Cloud UI to configure an OpenAI embedder:

    Animated image of the Meilisearch Cloud UI showing a user clicking on "add embedder". This opens up a modal window, where the user fills in the name of the embedder, chooses OpenAI as its source. They then select a model, input their API key, and type out a document template.

    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.

    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:

    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.