## Elasticsearch

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.

**Python**  
```bash
pip install elasticsearch>=8.0.0
```

**TypeScript**  
```bash
npm install mem0ai @elastic/elasticsearch
```

### Usage

**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)
m.add(messages, user_id="alice", metadata={"category": "movies"})
```

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

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,
    },
  },
};

const memory = new Memory(config);
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
```

The TypeScript SDK uses camelCase config keys: `collectionName`, `embeddingModelDims`, `cloudId`, `apiKey`, `useSsl`, `verifyCerts`, `caCerts`, `autoCreateIndex`, and `username`. `collectionName` and `embeddingModelDims` are required.

### Config

Here are the parameters available for configuring Elasticsearch:

| Parameter                | Description                                            | Default Value |  
|--------------------------|--------------------------------------------------------|---------------|  
| `collection_name`        | The name of the index to store the vectors             | `mem0`        |  
| `embedding_model_dims`   | Dimensions of the embedding model                       | `1536`       |  
| `host`                   | The host where the Elasticsearch server is running     | `localhost`   |  
| `port`                   | The port where the Elasticsearch server is running     | `9200`       |  
| `cloud_id`              | Cloud ID for Elastic Cloud deployment                   | `None`       |  
| `api_key`               | API key for authentication                               | `None`       |  
| `user`                  | Username for basic authentication                        | `None`       |  
| `password`              | Password for basic authentication                        | `None`       |  
| `use_ssl`               | Whether to use SSL for the connection                   | `True`       |  
| `ca_certs`              | Path to CA bundle for SSL certificate verification       | `None`       |  
| `verify_certs`          | Whether to verify SSL certificates                       | `True`       |  
| `auto_create_index`      | Whether to automatically create the index               | `True`       |  
| `custom_search_query`    | Function returning a custom search query                | `None`       |  
| `headers`               | Custom headers to include in requests                   | `None`       |

### 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

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

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

# Define custom_search_query

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
        }
    }
}
```

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