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

# ElasticsearchEmbeddingsCache 集成

> 使用 LangChain Python 与 ElasticsearchEmbeddingsCache 存储进行集成。

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

## 概述

`ElasticsearchEmbeddingsCache` 是一个 `ByteStore` 实现，它使用您的 Elasticsearch 实例来高效地存储和检索嵌入向量。

### 集成详情

| 类                                                                                                                                   | 包                                                                                           |  本地 | JS 支持 |                                                 下载量                                                |                                                版本                                                |
| :---------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ | :-: | :---: | :------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------: |
| [`ElasticsearchEmbeddingsCache`](https://reference.langchain.com/python/langchain-elasticsearch/cache/ElasticsearchEmbeddingsCache) | [`langchain-elasticsearch`](https://reference.langchain.com/python/langchain-elasticsearch) |  ✅  |   ❌   | ![PyPI - 下载量](https://img.shields.io/pypi/dm/langchain_elasticsearch?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain_elasticsearch?style=flat-square\&label=%20) |

## 设置

要创建 `ElasticsearchEmbeddingsCache` 字节存储，您需要一个 Elasticsearch 集群。您可以 [在本地设置一个](https://www.elastic.co/downloads/elasticsearch) 或创建一个 [Elastic 账户](https://www.elastic.co/elasticsearch)。

### 安装

LangChain `ElasticsearchEmbeddingsCache` 集成位于 `langchain-elasticsearch` 包中：

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

## 实例化

现在我们可以实例化我们的字节存储：

```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",
    },
)
```

## 用法

您可以使用 `mset` 方法像这样在键下设置数据：

```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']
```

您还可以使用 `mdelete` 方法删除数据：

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

## 用作嵌入缓存

与其他 `ByteStore` 一样，您可以将 `ElasticsearchEmbeddingsCache` 实例用于 RAG 的 [文档摄入中的持久缓存](/oss/python/integrations/embeddings#caching)。

但是，默认情况下缓存的向量不可搜索。开发者可以自定义 Elasticsearch 文档的构建方式，以添加索引向量字段。

这可以通过子类化和重写方法来实现：

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

在重写映射和文档构建时，请仅进行添加性修改，保持基础映射完整。

***

## API 参考

有关所有 `ElasticsearchEmbeddingsCache` 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/python/langchain-elasticsearch/cache/ElasticsearchEmbeddingsCache)

***

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

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