> ## Documentation Index
> Fetch the complete documentation index at: https://nvd-54.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Elasticsearch 向量嵌入缓存

> 使用 LangChain Python 集成 Elasticsearch 存储。

这将帮助你开始使用 Elasticsearch [键值存储](/oss/python/integrations/stores)。 所有 `ElasticsearchEmbeddingsCache` 功能和配置的详细文档请前往 [API reference](https://reference.langchain.com/python/langchain-elasticsearch/cache/ElasticsearchEmbeddingsCache).

## 概述

The `ElasticsearchEmbeddingsCache` is a `ByteStore` implementation that uses your Elasticsearch instance for efficient storage and retrieval of embeddings.

### 集成详情

| Class                                                                                                                               | Package                                                                                     | Local | JS support |                                                 Downloads                                                |                                                Version                                                |
| :---------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ | :---: | :--------: | :------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------: |
| [`ElasticsearchEmbeddingsCache`](https://reference.langchain.com/python/langchain-elasticsearch/cache/ElasticsearchEmbeddingsCache) | [`langchain-elasticsearch`](https://reference.langchain.com/python/langchain-elasticsearch) |   ✅   |      ❌     | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain_elasticsearch?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain_elasticsearch?style=flat-square\&label=%20) |

## 设置

To create a `ElasticsearchEmbeddingsCache` byte store, you'll need an Elasticsearch cluster. You can [set one up locally](https://www.elastic.co/downloads/elasticsearch) or create an [Elastic account](https://www.elastic.co/elasticsearch).

### 安装

The LangChain `ElasticsearchEmbeddingsCache` integration lives in the `langchain-elasticsearch` package:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-elasticsearch
```

## Instantiation

Now we can instantiate our byte store:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_elasticsearch import ElasticsearchEmbeddingsCache

# Example config for a locally running Elasticsearch instance
kv_store = ElasticsearchEmbeddingsCache(
    es_url="https://localhost:9200",
    index_name="llm-chat-cache",
    metadata={"project": "my_chatgpt_project"},
    namespace="my_chatgpt_project",
    es_user="elastic",
    es_password="<GENERATED PASSWORD>",
    es_params={
        "ca_certs": "~/http_ca.crt",
    },
)
```

## 使用方法

You can set data under keys like this using the `mset` method:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
kv_store.mset(
    [
        ["key1", b"value1"],
        ["key2", b"value2"],
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[b'value1', b'value2']
```

And you can delete data using the `mdelete` method:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
kv_store.mdelete(
    [
        "key1",
        "key2",
    ]
)

kv_store.mget(
    [
        "key1",
        "key2",
    ]
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[None, None]
```

## Use as an embeddings cache

Like other `ByteStores`, you can use an `ElasticsearchEmbeddingsCache` instance for [persistent caching in document ingestion](/oss/python/integrations/embeddings#caching) for RAG.

However, cached vectors won't be searchable by default. The developer can customize the building of the Elasticsearch document in order to add indexed vector field.

This can be done by subclassing and overriding methods:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from typing import Any, Dict, List


class SearchableElasticsearchStore(ElasticsearchEmbeddingsCache):
    @property
    def mapping(self) -> Dict[str, Any]:
        mapping = super().mapping
        mapping["mappings"]["properties"]["vector"] = {
            "type": "dense_vector",
            "dims": 1536,
            "index": True,
            "similarity": "dot_product",
        }
        return mapping

    def build_document(self, llm_input: str, vector: List[float]) -> Dict[str, Any]:
        body = super().build_document(llm_input, vector)
        body["vector"] = vector
        return body
```

When overriding the mapping and the document building, please only make additive modifications, keeping the base mapping intact.

***

## API 参考

所有 `ElasticsearchEmbeddingsCache` features and configurations, head to the [API reference](https://reference.langchain.com/python/langchain-elasticsearch/cache/ElasticsearchEmbeddingsCache)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/stores/elasticsearch.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
