Understanding key components
Collections: a collection is a structured storage that organizes and stores textual data and associated metadata. Collections enable sophisticated search, retrieval, and classification tasks using vector embeddings. Search Methods: a search method associated with a collection, defines how to convert collection items into a vector representation and provides indexing parameters. Vector embeddings: for vector-based search and comparison, Modus converts each item in the collection into a vector representation called embedding. By embedding data, you enable powerful natural language and similarity-based searches.Modus runtime automatically compute the embeddings, according to your
configuration, when you add or update items.
Initializing your collection
Before implementing search, ensure you have defined a collection in the app manifest. In this example,myProducts
is the collection used to store product descriptions.
First, we need to populate the collection with items (for example, product
descriptions). You can insert individual or multiple items using the upsert
and upsertBatch
methods, respectively.
Use upsert
to insert a product description into the collection. If you don’t
specify a key, Modus generates a unique key for you.
Configure your search method
The search capability relies on a search method and embedding function. To configure your search method.Create an embedding function
An embedding function is any API function that transforms text into vectors that represent their meaning in a high-dimensional space. Embeddings functions must have the following signature:Declare the model in the app manifestCreate the embedding function using the embedding model:
model.json
Declare the search method
With an embedding function in place, declare a search method in the collection properties.modus.json
Implement semantic similarity search
With the products stored, you can now search the collection by semantic similarity. The search] API computes an embedding for the provided text, compares it with the embeddings of the items in the collection, and returns the most similar items.Search result format
The search response is a CollectionSearchResult containing the following fields:collection
: the name of the collection.status
: the status of the operation.objects
: the search result items with their text, distance, and score values.distance
: a lower value indicates a closer match between the search query and the item in the collectionscore
: a higher value (closer to 1) represents a better match
Search for similar Items
When you need to search similar items to a given item, use thesearchByVector
API. Retrieve the vector associated with the given item by its key, then perform
a search using that vector.