Back to Mem0

Elasticsearch

docs/components/vectordbs/dbs/elasticsearch.mdx

2.0.126.1 KB
Original Source

Elasticsearch is a distributed, RESTful search and analytics engine that can efficiently store and search vector data using dense vectors and k-NN search.

Installation

Elasticsearch support requires the Elasticsearch client as an extra dependency.

<CodeGroup> ```bash Python pip install elasticsearch>=8.0.0 ```
bash
npm install mem0ai @elastic/elasticsearch
</CodeGroup>

Usage

<CodeGroup> ```python Python import os from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx"

config = { "vector_store": { "provider": "elasticsearch", "config": { "collection_name": "mem0", "host": "localhost", "port": 9200, "embedding_model_dims": 1536 } } }

m = Memory.from_config(config) messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"})


```typescript TypeScript
import { Memory } from "mem0ai/oss";

// Set OPENAI_API_KEY in your environment.
const config = {
  embedder: {
    provider: "openai",
    config: {
      apiKey: process.env.OPENAI_API_KEY,
      model: "text-embedding-3-small",
    },
  },
  vectorStore: {
    provider: "elasticsearch",
    config: {
      collectionName: "mem0",
      embeddingModelDims: 1536,
      host: "localhost",
      port: 9200,
      // For Elastic Cloud, pass cloudId and apiKey instead of host/port.
      // For basic auth, pass username and password.
    },
  },
};

const memory = new Memory(config);
const messages = [
  { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
  { role: "assistant", content: "How about thriller movies? They can be quite engaging." },
  { role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." },
  { role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." },
];
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
</CodeGroup> <Note> The TypeScript SDK uses camelCase config keys: `collectionName`, `embeddingModelDims`, `cloudId`, `apiKey`, `useSsl`, `verifyCerts`, `caCerts`, `autoCreateIndex`, and `username` (in place of the Python `user`). `collectionName` and `embeddingModelDims` are required. Because the vector store embeds text with your configured embedder before writing, set an `embedder` in the config as shown above. </Note>

Config

Here are the parameters available for configuring Elasticsearch:

ParameterDescriptionDefault Value
collection_nameThe name of the index to store the vectorsmem0
embedding_model_dimsDimensions of the embedding model1536
hostThe host where the Elasticsearch server is runninglocalhost
portThe port where the Elasticsearch server is running9200
cloud_idCloud ID for Elastic Cloud deploymentNone
api_keyAPI key for authenticationNone
userUsername for basic authenticationNone
passwordPassword for basic authenticationNone
use_sslWhether to use SSL for the connectionTrue
ca_certsPath to CA bundle for SSL certificate verificationNone
verify_certsWhether to verify SSL certificatesTrue
auto_create_indexWhether to automatically create the indexTrue
custom_search_queryFunction returning a custom search queryNone
headersCustom headers to include in requestsNone

Features

  • Efficient vector search using Elasticsearch's native k-NN search
  • Support for both local and cloud deployments (Elastic Cloud)
  • Multiple authentication methods (Basic Auth, API Key)
  • Automatic index creation with optimized mappings for vector search
  • Memory isolation through payload filtering
  • Custom search query function to customize the search query

Custom Search Query

<Note> `custom_search_query` is available in the Python SDK only. The TypeScript SDK runs a fixed k-NN query with optional metadata filters. </Note>

The custom_search_query parameter allows you to customize the search query when Memory.search is called.

Example

python
import os
from typing import List, Optional, Dict
from mem0 import Memory

def custom_search_query(query: List[float], limit: int, filters: Optional[Dict]) -> Dict:
    return {
        "knn": {
            "field": "vector", 
            "query_vector": query, 
            "k": limit, 
            "num_candidates": limit * 2
        }
    }

os.environ["OPENAI_API_KEY"] = "sk-xx"

config = {
    "vector_store": {
        "provider": "elasticsearch",
        "config": {
            "collection_name": "mem0",
            "host": "localhost",
            "port": 9200,
            "embedding_model_dims": 1536,
            "custom_search_query": custom_search_query
        }
    }
}

It should be a function that takes the following parameters:

  • query: a query vector used in Memory.search
  • limit: a number of results used in Memory.search
  • filters: a dictionary of key-value pairs used in Memory.search. You can add custom pairs for the custom search query.

The function should return a query body for the Elasticsearch search API.