Class MongoDbEmbeddingStore

java.lang.Object
dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore
All Implemented Interfaces:
EmbeddingStore<TextSegment>

public class MongoDbEmbeddingStore extends Object implements EmbeddingStore<TextSegment>
Represents a MongoDB index as an embedding store.

More info to set up MongoDb as vectorDatabase.

tutorial how to use a knn-vector in MongoDB Atlas (great starting point).

If you are using a free tier, #createIndex = true might not be supported, so you will need to create an index manually. In your Atlas web console go to: DEPLOYMENT -> Database -> {your cluster} -> Atlas Search -> Create Index Search -> "JSON Editor" under "Atlas Search" -> Next -> Select your database in the left pane -> Insert the following JSON into the right pane (set "dimensions" and "metadata"->"fields" to desired values)

 {
   "mappings": {
     "dynamic": false,
     "fields": {
       "embedding": {
         "dimensions": 384,
         "similarity": "cosine",
         "type": "knnVector"
       },
       "metadata": {
         "dynamic": false,
         "fields": {
           "test-key": {
             "type": "token"
           }
         },
         "type": "document"
       }
     }
   }
 }
 
-> Next -> Create Search Index
  • Constructor Details

    • MongoDbEmbeddingStore

      public MongoDbEmbeddingStore(com.mongodb.client.MongoClient mongoClient, String databaseName, String collectionName, String indexName, Long maxResultRatio, com.mongodb.client.model.CreateCollectionOptions createCollectionOptions, org.bson.conversions.Bson filter, IndexMapping indexMapping, Boolean createIndex)
  • Method Details

    • builder

      public static MongoDbEmbeddingStore.Builder builder()
    • add

      public String add(Embedding embedding)
      Description copied from interface: EmbeddingStore
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      embedding - The embedding to be added to the store.
      Returns:
      The auto-generated ID associated with the added embedding.
    • add

      public void add(String id, Embedding embedding)
      Description copied from interface: EmbeddingStore
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      id - The unique identifier for the embedding to be added.
      embedding - The embedding to be added to the store.
    • add

      public String add(Embedding embedding, TextSegment textSegment)
      Description copied from interface: EmbeddingStore
      Adds a given embedding and the corresponding content that has been embedded to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      embedding - The embedding to be added to the store.
      textSegment - Original content that was embedded.
      Returns:
      The auto-generated ID associated with the added embedding.
    • addAll

      public List<String> addAll(List<Embedding> embeddings)
      Description copied from interface: EmbeddingStore
      Adds multiple embeddings to the store.
      Specified by:
      addAll in interface EmbeddingStore<TextSegment>
      Parameters:
      embeddings - A list of embeddings to be added to the store.
      Returns:
      A list of auto-generated IDs associated with the added embeddings.
    • addAll

      public List<String> addAll(List<Embedding> embeddings, List<TextSegment> embedded)
      Description copied from interface: EmbeddingStore
      Adds multiple embeddings and their corresponding contents that have been embedded to the store.
      Specified by:
      addAll in interface EmbeddingStore<TextSegment>
      Parameters:
      embeddings - A list of embeddings to be added to the store.
      embedded - A list of original contents that were embedded.
      Returns:
      A list of auto-generated IDs associated with the added embeddings.
    • findRelevant

      public List<EmbeddingMatch<TextSegment>> findRelevant(Embedding referenceEmbedding, int maxResults, double minScore)
      Description copied from interface: EmbeddingStore
      Finds the most relevant (closest in space) embeddings to the provided reference embedding.
      Specified by:
      findRelevant in interface EmbeddingStore<TextSegment>
      Parameters:
      referenceEmbedding - The embedding used as a reference. Returned embeddings should be relevant (closest) to this one.
      maxResults - The maximum number of embeddings to be returned.
      minScore - The minimum relevance score, ranging from 0 to 1 (inclusive). Only embeddings with a score of this value or higher will be returned.
      Returns:
      A list of embedding matches. Each embedding match includes a relevance score (derivative of cosine distance), ranging from 0 (not relevant) to 1 (highly relevant).