# AsyncElasticsearchCache

> **Class** in `langchain_elasticsearch`

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

`Elasticsearch` LLM cache.

Caches LLM responses in Elasticsearch to avoid repeated calls for identical
prompts.

## Signature

```python
AsyncElasticsearchCache(
    self,
    index_name: str,
    *,
    store_input: bool = True,
    store_input_params: bool = True,
    metadata: Optional[Dict[str, Any]] = None,
    client: Optional[AsyncElasticsearch] = 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 cache store.

**Instantiate:**

```python
from langchain_elasticsearch import ElasticsearchCache

cache = ElasticsearchCache(
    index_name="llm-cache",
    es_url="http://localhost:9200"
)
```

**Instantiate with API key:**

```python
from langchain_elasticsearch import ElasticsearchCache

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

**Instantiate from cloud:**

```python
from langchain_elasticsearch import ElasticsearchCache

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

**Instantiate from existing connection:**

```python
from langchain_elasticsearch import ElasticsearchCache
from elasticsearch import Elasticsearch

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

**Use with LangChain:**

```python
from langchain.globals import set_llm_cache
from langchain_elasticsearch import ElasticsearchCache

set_llm_cache(ElasticsearchCache(
    index_name="llm-cache",
    es_url="http://localhost:9200"
))
```

For synchronous applications, use the `ElasticsearchCache` class.
For asynchronous applications, use the `AsyncElasticsearchCache` 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 LLM input (prompt) in the cache. Default True. (default: `True`) |
| `store_input_params` | `bool` | No | Whether to store the LLM parameters in the cache. Default True. (default: `True`) |
| `metadata` | `dict` | No | Additional metadata to store in the cache for filtering. Must be JSON serializable. (default: `None`) |
| `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

- `BaseCache`

## Constructors

```python
__init__(
    self,
    index_name: str,
    *,
    store_input: bool = True,
    store_input_params: bool = True,
    metadata: Optional[Dict[str, Any]] = None,
    client: Optional[AsyncElasticsearch] = 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` |
| `store_input_params` | `bool` |
| `metadata` | `Optional[Dict[str, Any]]` |
| `client` | `Optional[AsyncElasticsearch]` |
| `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/_async/cache/AsyncElasticsearchCache/is_alias)
- [`alookup()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/cache/AsyncElasticsearchCache/alookup)
- [`build_document()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/cache/AsyncElasticsearchCache/build_document)
- [`aupdate()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/cache/AsyncElasticsearchCache/aupdate)
- [`aclear()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/cache/AsyncElasticsearchCache/aclear)

---

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