Getting started with AI-powered search experimental

    AI-powered search, sometimes also called vector search and hybrid search, is an experimental technology that uses large language models to retrieve search results based on the meaning and context of a query.

    This tutorial will walk you through configuring AI-powered search in your Meilisearch project. You will activate the vector store setting, generate document embeddings with OpenAI, and perform your first search.

    Requirements

    Create a new index

    Create a kitchenware index and add this kitchenware products dataset to it. If necessary, consult the quick start for instructions on how to configure a basic Meilisearch installation.

    First, activate the AI-powered search experimental feature. Exactly how to do that depends on whether you are using Meilisearch Cloud or self-hosting Meilisearch.

    Meilisearch Cloud projects

    If using Meilisearch Cloud, navigate to your project overview and find "Experimental features". Then check the "AI-powered search" box.

    A section of the project overview interface titled "Experimental features". The image shows a few options, including "Vector store".

    Meilisearch Cloud AI-powered search waitlist

    To ensure proper scaling of Meilisearch Cloud's latest AI-powered search offering, you must enter the waitlist before activating vector search. You will not be able to activate vector search in the Cloud interface or via the /experimental-features route until your sign-up has been approved.

    Self-hosted instances

    Use the /experimental-features route to activate vector search during runtime:

    curl \
      -X PATCH 'http://localhost:7700/experimental-features/' \
      -H 'Content-Type: application/json'  \
      --data-binary '{
        "vectorStore": true
      }'
    

    Generate vector embeddings with OpenAI

    Next, you must generate vector embeddings for all documents in your dataset. Embeddings are mathematical representations of the meanings of words and sentences in your documents. Meilisearch relies on external providers to generate these embeddings. Use OpenAI for this tutorial.

    Use the embedders index setting of the update /settings endpoint to configure a default OpenAI embedder:

    curl \
      -X PATCH 'http://localhost:7700/indexes/kitchenware/settings' \
      -H 'Content-Type: application/json' \
      --data-binary '{
        "embedders": {
          "default": {
            "source":  "openAi",
            "apiKey": "OPEN_AI_API_KEY",
            "model": "text-embedding-3-small",
            "documentTemplate": "An object used in a kitchen named '{{doc.name}}'"
          }
        }
      }'
    

    Replace OPEN_AI_API_KEY with your OpenAI API key. You may use any key tier for this tutorial, but prefer Tier 2 keys for optimal performance in production environments.

    documentTemplate

    documentTemplate describes a short Liquid template. The text inside curly brackets ({{) indicates a document field in dot notation, where doc indicates the document itself and the string that comes after the dot indicates a document attribute. Meilisearch replaces these brackets and their contents with the corresponding field value.

    The resulting text is the prompt OpenAI uses to generate document embeddings.

    For example, kitchenware documents have three fields: id, name, and price. If your documentTemplate is "An object used in a kitchen named '{{doc.name}}'", the text Meilisearch will send to the embedder when indexing the first document is "An object used in a kitchen named 'Wooden spoon'".

    For the best results, always provide a documentTemplate. Keep your templates short and only include highly relevant information. This ensures optimal indexing performance and search result relevancy.

    Perform AI-powered searches with q and hybrid to retrieve search results using the default embedder you configured in the previous step:

    curl \
      -X POST 'http://localhost:7700/indexes/kitchenware/search' \
      -H 'content-type: application/json' \
      --data-binary '{ 
        "q": "kitchen utensils made of wood",
        "hybrid": {
          "embedder": "default",
          "semanticRatio": 0.7
        }
      }'
    

    Meilisearch will return a mix of semantic and full-text matches, prioritizing results that match the query's meaning and context. If you want Meilisearch to return more results based on the meaning and context of a search, set semanticRatio to a value greater than 0.5. Setting semanticRatio to a value lower than 0.5, instead, will return more full-text matches.

    Conclusion

    You have seen how to set up and perform AI-powered searches with Meilisearch and OpenAI. For more in-depth information, consult the reference for embedders and the hybrid search parameter.

    AI-powered search is an experimental Meilisearch feature and is undergoing active development—join the discussion on GitHub.