# ElasticsearchEmbeddingsCache

> **Class** in `langchain_elasticsearch`

📖 [View in docs](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache)

`Elasticsearch` embeddings cache.

Caches embeddings in Elasticsearch to avoid repeated embedding computations.

## Signature

```python
ElasticsearchEmbeddingsCache(
    self,
    index_name: str,
    *,
    store_input: bool = True,
    metadata: Optional[Dict[str, Any]] = None,
    namespace: Optional[str] = None,
    maximum_duplicates_allowed: int = 1,
    client: Optional[Elasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

## Description

**Setup:**

Install `langchain_elasticsearch` and start Elasticsearch locally using
the start-local script.

```bash
pip install -qU langchain_elasticsearch
curl -fsSL https://elastic.co/start-local | sh
```

This will create an `elastic-start-local` folder. To start Elasticsearch
and Kibana:
```bash
cd elastic-start-local
./start.sh
```

Elasticsearch will be available at `http://localhost:9200`. The password
for the `elastic` user and API key are stored in the `.env` file in the
`elastic-start-local` folder.

Initialize the Elasticsearch embeddings cache store.

**Instantiate:**

```python
from langchain_elasticsearch import ElasticsearchEmbeddingsCache

cache = ElasticsearchEmbeddingsCache(
    index_name="embeddings-cache",
    es_url="http://localhost:9200"
)
```

**Instantiate with API key:**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddingsCache

    cache = ElasticsearchEmbeddingsCache(
        index_name="embeddings-cache",
        es_url="http://localhost:9200",
        es_api_key="your-api-key"
    )
    ```

**Instantiate from cloud:**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddingsCache

    cache = ElasticsearchEmbeddingsCache(
        index_name="embeddings-cache",
        es_cloud_id="<cloud_id>",
        es_api_key="your-api-key"
    )
    ```

**Instantiate from existing connection:**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddingsCache
    from elasticsearch import Elasticsearch

    client = Elasticsearch("http://localhost:9200")
    cache = ElasticsearchEmbeddingsCache(
        index_name="embeddings-cache",
        client=client
    )
    ```

**Use with CacheBackedEmbeddings:**

```python
from langchain.embeddings import CacheBackedEmbeddings
from langchain_openai import OpenAIEmbeddings
from langchain_elasticsearch import ElasticsearchEmbeddingsCache

underlying_embeddings = OpenAIEmbeddings()
cache = ElasticsearchEmbeddingsCache(
    index_name="embeddings-cache",
    es_url="http://localhost:9200"
)
cached_embeddings = CacheBackedEmbeddings.from_bytes_store(
    underlying_embeddings,
    cache,
    namespace=underlying_embeddings.model
)
```

For synchronous applications, use the `ElasticsearchEmbeddingsCache` class.
For asynchronous applications, use the `AsyncElasticsearchEmbeddingsCache`
class.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `index_name` | `str` | Yes | The name of the index or alias to use for the cache. If it doesn't exist, an index is created according to the default mapping. |
| `store_input` | `bool` | No | Whether to store the input text in the cache. Default is True. (default: `True`) |
| `metadata` | `dict` | No | Additional metadata to store in the cache for filtering. Must be JSON serializable. (default: `None`) |
| `namespace` | `str` | No | A namespace to organize the cache. (default: `None`) |
| `maximum_duplicates_allowed` | `int` | No | Maximum duplicate keys permitted when using aliases across multiple indices. Default is 1. (default: `1`) |
| `client` | `AsyncElasticsearch` | No | Pre-existing Elasticsearch connection. Either provide this OR credentials. (default: `None`) |
| `es_url` | `str` | No | URL of the Elasticsearch instance. (default: `None`) |
| `es_cloud_id` | `str` | No | Cloud ID of the Elasticsearch instance. (default: `None`) |
| `es_user` | `str` | No | Username for Elasticsearch. (default: `None`) |
| `es_api_key` | `str` | No | API key for Elasticsearch. (default: `None`) |
| `es_password` | `str` | No | Password for Elasticsearch. (default: `None`) |

## Extends

- `ByteStore`

## Constructors

```python
__init__(
    self,
    index_name: str,
    *,
    store_input: bool = True,
    metadata: Optional[Dict[str, Any]] = None,
    namespace: Optional[str] = None,
    maximum_duplicates_allowed: int = 1,
    client: Optional[Elasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

| Name | Type |
|------|------|
| `index_name` | `str` |
| `store_input` | `bool` |
| `metadata` | `Optional[Dict[str, Any]]` |
| `namespace` | `Optional[str]` |
| `maximum_duplicates_allowed` | `int` |
| `client` | `Optional[Elasticsearch]` |
| `es_url` | `Optional[str]` |
| `es_cloud_id` | `Optional[str]` |
| `es_user` | `Optional[str]` |
| `es_api_key` | `Optional[str]` |
| `es_password` | `Optional[str]` |


## Properties

- `mapping`

## Methods

- [`is_alias()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/is_alias)
- [`encode_vector()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/encode_vector)
- [`decode_vector()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/decode_vector)
- [`mget()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/mget)
- [`build_document()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/build_document)
- [`mset()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/mset)
- [`mdelete()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/mdelete)
- [`yield_keys()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/cache/ElasticsearchEmbeddingsCache/yield_keys)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-elastic/blob/e78a327f38e09abd10128ed28c8774e9a8f9fec0/libs/elasticsearch/langchain_elasticsearch/_sync/cache.py#L285)