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

# Elasticsearch 集成

> 使用 LangChain Python 与 Elasticsearch 向量存储集成。

> [Elasticsearch](https://www.elastic.co/elasticsearch/) 是一个分布式、RESTful 的搜索和分析引擎，能够执行向量搜索和词法搜索。它构建在 Apache Lucene 库之上。

本笔记本展示了如何使用与 `Elasticsearch` 向量存储相关的功能。

## 设置

要使用 `Elasticsearch` 向量搜索，您必须安装 `langchain-elasticsearch` 包。

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

### 凭证

设置 Elasticsearch 实例以供使用主要有两种方式：

1. Elastic Cloud：Elastic Cloud 是一个托管的 Elasticsearch 服务。注册 [免费试用](https://cloud.elastic.co/registration?utm_source=langchain\&utm_content=documentation)。

   要连接到一个不需要登录凭证的 Elasticsearch 实例（使用启用安全性的 docker 实例启动），请将 Elasticsearch URL 和索引名称以及嵌入对象传递给构造函数。

2. 本地安装 Elasticsearch：通过在本地运行 Elasticsearch 来开始使用。最简单的方法是使用官方 Elasticsearch Docker 镜像。更多信息请参阅 [Elasticsearch Docker 文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html)。

### 在本地运行 Elasticsearch

在本地运行 Elasticsearch 进行开发和测试的最简单方法是使用 [start-local](https://github.com/elastic/start-local) 脚本。此脚本使用 Docker 通过一个简单的命令设置 Elasticsearch（以及可选的 Kibana）。

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
curl -fsSL https://elastic.co/start-local | sh
```

这将创建一个 `elastic-start-local` 文件夹，其中包含配置文件和启动脚本。要启动 Elasticsearch：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
cd elastic-start-local
./start.sh
```

Elasticsearch 将在 `http://localhost:9200` 上可用。`elastic` 用户的密码和 API 密钥会自动生成并存储在 `elastic-start-local` 文件夹中的 `.env` 文件中。

如果您只需要 Elasticsearch 而不需要 Kibana，可以使用 `--esonly` 选项：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
curl -fsSL https://elastic.co/start-local | sh -s -- --esonly
```

<Note>
  start-local 设置仅用于本地测试，不应在生产环境中使用。有关生产安装，请参阅官方 [Elasticsearch 文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html)。
</Note>

### 使用身份验证运行

对于生产环境，我们建议您启用安全性运行。要使用登录凭证连接，您可以使用参数 `es_api_key` 或 `es_user` 和 `es_password`。

<EmbeddingTabs />

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# | output: false
# | echo: false
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
```

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

elastic_vector_search = ElasticsearchStore(
    es_url="http://localhost:9200",
    index_name="langchain_index",
    embedding=embeddings,
    es_user="elastic",
    es_password="changeme",
)
```

#### 如何获取默认 "elastic" 用户的密码？

要获取 Elastic Cloud 上默认 "elastic" 用户的密码：

1. 登录 Elastic Cloud 控制台 [cloud.elastic.co](https://cloud.elastic.co)
2. 转到 "Security" > "Users"
3. 找到 "elastic" 用户并点击 "Edit"
4. 点击 "Reset password"
5. 按照提示重置密码

#### 如何获取 API 密钥？

要获取 API 密钥：

1. 登录 Elastic Cloud 控制台 [cloud.elastic.co](https://cloud.elastic.co)
2. 打开 Kibana 并转到 Stack Management > API Keys
3. 点击 "Create API key"
4. 输入 API 密钥的名称并点击 "Create"
5. 复制 API 密钥并将其粘贴到 `api_key` 参数中

### Elastic cloud

要连接到 Elastic Cloud 上的 Elasticsearch 实例，您可以使用 `es_cloud_id` 参数或 `es_url`。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
elastic_vector_search = ElasticsearchStore(
    es_cloud_id="<cloud_id>",
    index_name="test_index",
    embedding=embeddings,
    es_user="elastic",
    es_password="changeme",
)
```

如果您想获得一流的模型调用自动跟踪，您也可以通过取消注释以下内容来设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

## 初始化

Elasticsearch 正在本地主机 localhost:9200 上通过 [docker](#running-elasticsearch-locally) 运行。有关如何从 Elastic Cloud 连接到 Elasticsearch 的更多详细信息，请参阅上面的 [使用身份验证连接](#running-with-authentication)。

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

vector_store = ElasticsearchStore(
    "langchain-demo", embedding=embeddings, es_url="http://localhost:9201"
)
```

## 管理向量存储

### 向向量存储添加项目

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

from langchain_core.documents import Document

document_1 = Document(
    page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
    metadata={"source": "tweet"},
)

document_2 = Document(
    page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
    metadata={"source": "news"},
)

document_3 = Document(
    page_content="Building an exciting new project with LangChain - come check it out!",
    metadata={"source": "tweet"},
)

document_4 = Document(
    page_content="Robbers broke into the city bank and stole $1 million in cash.",
    metadata={"source": "news"},
)

document_5 = Document(
    page_content="Wow! That was an amazing movie. I can't wait to see it again.",
    metadata={"source": "tweet"},
)

document_6 = Document(
    page_content="Is the new iPhone worth the price? Read this review to find out.",
    metadata={"source": "website"},
)

document_7 = Document(
    page_content="The top 10 soccer players in the world right now.",
    metadata={"source": "website"},
)

document_8 = Document(
    page_content="LangGraph is the best framework for building stateful, agentic applications!",
    metadata={"source": "tweet"},
)

document_9 = Document(
    page_content="The stock market is down 500 points today due to fears of a recession.",
    metadata={"source": "news"},
)

document_10 = Document(
    page_content="I have a bad feeling I am going to get deleted :(",
    metadata={"source": "tweet"},
)

documents = [
    document_1,
    document_2,
    document_3,
    document_4,
    document_5,
    document_6,
    document_7,
    document_8,
    document_9,
    document_10,
]
uuids = [str(uuid4()) for _ in range(len(documents))]

vector_store.add_documents(documents=documents, ids=uuids)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
['21cca03c-9089-42d2-b41c-3d156be2b519',
 'a6ceb967-b552-4802-bb06-c0e95fce386e',
 '3a35fac4-e5f0-493b-bee0-9143b41aedae',
 '176da099-66b1-4d6a-811b-dfdfe0808d30',
 'ecfa1a30-3c97-408b-80c0-5c43d68bf5ff',
 'c0f08baa-e70b-4f83-b387-c6e0a0f36f73',
 '489b2c9c-1925-43e1-bcf0-0fa94cf1cbc4',
 '408c6503-9ba4-49fd-b1cc-95584cd914c5',
 '5248c899-16d5-4377-a9e9-736ca443ad4f',
 'ca182769-c4fc-4e25-8f0a-8dd0a525955c']
```

### 从向量存储中删除项目

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
vector_store.delete(ids=[uuids[-1]])
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
True
```

## 查询向量存储

一旦您的向量存储已创建并且相关文档已添加，您很可能希望在链或代理运行期间对其进行查询。这些示例还展示了如何在搜索时使用过滤。

### 直接查询

#### 相似性搜索

执行带有元数据过滤的简单相似性搜索可以这样做：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search(
    query="LangChain provides abstractions to make working with LLMs easy",
    k=2,
    filter=[{"term": {"metadata.source.keyword": "tweet"}}],
)
for res in results:
    print(f"* {res.page_content} [{res.metadata}]")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]
* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]
```

#### 带分数的相似性搜索

如果您想执行相似性搜索并接收相应的分数，您可以运行：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search_with_score(
    query="Will it be hot tomorrow",
    k=1,
    filter=[{"term": {"metadata.source.keyword": "news"}}],
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
* [SIM=0.765887] The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees. [{'source': 'news'}]
```

### 通过转换为检索器进行查询

您也可以将向量存储转换为检索器，以便在您的链中更轻松地使用。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
retriever = vector_store.as_retriever(
    search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.2}
)
retriever.invoke("Stealing from the bank is a crime")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[Document(metadata={'source': 'news'}, page_content='Robbers broke into the city bank and stole $1 million in cash.'),
 Document(metadata={'source': 'news'}, page_content='The stock market is down 500 points today due to fears of a recession.'),
 Document(metadata={'source': 'website'}, page_content='Is the new iPhone worth the price? Read this review to find out.'),
 Document(metadata={'source': 'tweet'}, page_content='Building an exciting new project with LangChain - come check it out!')]
```

## 距离相似性算法

Elasticsearch 支持以下向量距离相似性算法：

* cosine
* euclidean
* dot\_product

余弦相似性算法是默认值。

您可以通过 similarity 参数指定所需的相似性算法。

**注意**：根据检索策略，相似性算法无法在查询时更改。它需要在为字段创建索引映射时设置。如果您需要更改相似性算法，则需要删除索引并使用正确的 distance\_strategy 重新创建它。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
db = ElasticsearchStore.from_documents(
    docs,
    embeddings,
    es_url="http://localhost:9200",
    index_name="test",
    distance_strategy="COSINE",
    # distance_strategy="EUCLIDEAN_DISTANCE"
    # distance_strategy="DOT_PRODUCT"
)
```

## 检索策略

Elasticsearch 相较于其他纯向量数据库的一个巨大优势在于其支持广泛的检索策略。在本笔记本中，我们将配置 `ElasticsearchStore` 以支持一些最常见的检索策略。

默认情况下，`ElasticsearchStore` 使用 `DenseVectorStrategy`（在 0.2.0 版本之前称为 `ApproxRetrievalStrategy`）。

### DenseVectorStrategy

这将返回与查询向量最相似的前 k 个向量。`k` 参数在初始化 `ElasticsearchStore` 时设置。默认值为 10。

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

db = ElasticsearchStore.from_documents(
    docs,
    embeddings,
    es_url="http://localhost:9200",
    index_name="test",
    strategy=DenseVectorStrategy(),
)

docs = db.similarity_search(
    query="What did the president say about Ketanji Brown Jackson?", k=10
)
```

#### 示例：使用稠密向量和关键词搜索的混合检索

此示例将展示如何配置 ElasticsearchStore 以执行混合检索，结合使用近似语义搜索和基于关键词的搜索。

我们使用 RRF 来平衡来自不同检索方法的两个分数。

要启用混合检索，我们需要在 `DenseVectorStrategy` 构造函数中设置 `hybrid=True`。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
db = ElasticsearchStore.from_documents(
    docs,
    embeddings,
    es_url="http://localhost:9200",
    index_name="test",
    strategy=DenseVectorStrategy(hybrid=True),
)
```

当启用混合时，执行的查询将是近似语义搜索和基于关键词搜索的组合。

它将使用 rrf（倒数排名融合）来平衡来自不同检索方法的两个分数。

**注意**：RRF 需要 Elasticsearch 8.9.0 或更高版本。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
    "retriever": {
        "rrf": {
            "retrievers": [
                {
                    "standard": {
                        "query": {
                            "bool": {
                                "filter": [],
                                "must": [{"match": {"text": {"query": "foo"}}}],
                            }
                        },
                    },
                },
                {
                    "knn": {
                        "field": "vector",
                        "filter": [],
                        "k": 1,
                        "num_candidates": 50,
                        "query_vector": [1.0, ..., 0.0],
                    },
                },
            ]
        }
    }
}
```

#### 示例：使用 Elasticsearch 中嵌入模型的稠密向量搜索

此示例将展示如何配置 `ElasticsearchStore` 以使用部署在 Elasticsearch 中的嵌入模型进行稠密向量检索。

要使用此功能，请在 `DenseVectorStrategy` 构造函数中通过 `query_model_id` 参数指定 model\_id。

**注意**：这需要模型部署并运行在 Elasticsearch ML 节点中。有关如何使用 `eland` 部署模型，请参阅 [笔记本示例](https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/integrations/hugging-face/loading-model-from-hugging-face.ipynb)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
DENSE_SELF_DEPLOYED_INDEX_NAME = "test-dense-self-deployed"

# 注意：这里没有指定嵌入函数
# 相反，我们将使用部署在 Elasticsearch 中的嵌入模型
db = ElasticsearchStore(
    es_cloud_id="<your cloud id>",
    es_user="elastic",
    es_password="<your password>",
    index_name=DENSE_SELF_DEPLOYED_INDEX_NAME,
    query_field="text_field",
    vector_query_field="vector_query_field.predicted_value",
    strategy=DenseVectorStrategy(model_id="sentence-transformers__all-minilm-l6-v2"),
)

# 设置一个摄入管道来执行文本字段的嵌入
db.client.ingest.put_pipeline(
    id="test_pipeline",
    processors=[
        {
            "inference": {
                "model_id": "sentence-transformers__all-minilm-l6-v2",
                "field_map": {"query_field": "text_field"},
                "target_field": "vector_query_field",
            }
        }
    ],
)

# 使用管道创建新索引，
# 不依赖 langchain 来创建索引
db.client.indices.create(
    index=DENSE_SELF_DEPLOYED_INDEX_NAME,
    mappings={
        "properties": {
            "text_field": {"type": "text"},
            "vector_query_field": {
                "properties": {
                    "predicted_value": {
                        "type": "dense_vector",
                        "dims": 384,
                        "index": True,
                        "similarity": "l2_norm",
                    }
                }
            },
        }
    },
    settings={"index": {"default_pipeline": "test_pipeline"}},
)

db.from_texts(
    ["hello world"],
    es_cloud_id="<cloud id>",
    es_user="elastic",
    es_password="<cloud password>",
    index_name=DENSE_SELF_DEPLOYED_INDEX_NAME,
    query_field="text_field",
    vector_query_field="vector_query_field.predicted_value",
    strategy=DenseVectorStrategy(model_id="sentence-transformers__all-minilm-l6-v2"),
)

# 执行搜索
db.similarity_search("hello world", k=10)
```

### SparseVectorStrategy (ELSER)

此策略使用 Elasticsearch 的稀疏向量检索来检索前 k 个结果。目前我们只支持我们自己的 "ELSER" 嵌入模型。

**注意**：这需要 ELSER 模型部署并运行在 Elasticsearch ml 节点中。

要使用此功能，请在 `ElasticsearchStore` 构造函数中指定 `SparseVectorStrategy`（在 0.2.0 版本之前称为 `SparseVectorRetrievalStrategy`）。您需要提供一个模型 ID。

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

# 注意此示例没有嵌入函数。这是因为我们在索引时和查询时在 Elasticsearch 内部推断令牌。
# 这需要 ELSER 模型加载并运行在 Elasticsearch 中。
db = ElasticsearchStore.from_documents(
    docs,
    es_cloud_id="<cloud id>",
    es_user="elastic",
    es_password="<cloud password>",
    index_name="test-elser",
    strategy=SparseVectorStrategy(model_id=".elser_model_2"),
)

db.client.indices.refresh(index="test-elser")

results = db.similarity_search(
    "What did the president say about Ketanji Brown Jackson", k=4
)
print(results[0])
```

### DenseVectorScriptScoreStrategy

此策略使用 Elasticsearch 的脚本分数查询来执行精确向量检索（也称为暴力检索）以检索前 k 个结果。（此策略在 0.2.0 版本之前称为 `ExactRetrievalStrategy`。）

要使用此功能，请在 `ElasticsearchStore` 构造函数中指定 `DenseVectorScriptScoreStrategy`。

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

db = ElasticsearchStore.from_documents(
    docs,
    embeddings,
    es_url="http://localhost:9200",
    index_name="test",
    strategy=DenseVectorScriptScoreStrategy(),
)
```

### BM25Strategy

最后，您可以使用全文关键词搜索。

要使用此功能，请在 `ElasticsearchStore` 构造函数中指定 `BM25Strategy`。

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

db = ElasticsearchStore.from_documents(
    docs,
    es_url="http://localhost:9200",
    index_name="test",
    strategy=BM25Strategy(),
)
```

### BM25RetrievalStrategy

此策略允许用户使用纯 BM25 进行搜索，而不使用向量搜索。

要使用此功能，请在 `ElasticsearchStore` 构造函数中指定 `BM25RetrievalStrategy`。

请注意，在下面的示例中，未指定嵌入选项，表示搜索是在不使用嵌入的情况下进行的。

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

db = ElasticsearchStore(
    es_url="http://localhost:9200",
    index_name="test_index",
    strategy=ElasticsearchStore.BM25RetrievalStrategy(),
)

db.add_texts(
    ["foo", "foo bar", "foo bar baz", "bar", "bar baz", "baz"],
)

results = db.similarity_search(query="foo", k=10)
print(results)
```

## 自定义查询

通过搜索时的 `custom_query` 参数，您可以调整用于从 Elasticsearch 检索文档的查询。如果您想使用更复杂的查询，例如支持字段的线性提升，这将非常有用。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 一个自定义查询的示例，只是在文本字段上进行 BM25 搜索。
def custom_query(query_body: dict, query: str):
    """用于 Elasticsearch 的自定义查询。
    Args:
        query_body (dict): Elasticsearch 查询主体。
        query (str): 查询字符串。
    Returns:
        dict: Elasticsearch 查询主体。
    """
    print("检索策略创建的查询检索器：")
    print(query_body)
    print()

    new_query_body = {"query": {"match": {"text": query}}}

    print("实际在 Elasticsearch 中使用的查询：")
    print(new_query_body)
    print()

    return new_query_body


results = db.similarity_search(
    "What did the president say about Ketanji Brown Jackson",
    k=4,
    custom_query=custom_query,
)
print("结果：")
print(results[0])
```

## 自定义文档构建器

通过搜索时的 `doc_builder` 参数，您可以调整如何使用从 Elasticsearch 检索的数据构建 Document。如果您有未使用 LangChain 创建的索引，这将特别有用。

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

from langchain_core.documents import Document


def custom_document_builder(hit: Dict) -> Document:
    src = hit.get("_source", {})
    return Document(
        page_content=src.get("content", "Missing content!"),
        metadata={
            "page_number": src.get("page_number", -1),
            "original_filename": src.get("original_filename", "Missing filename!"),
        },
    )


results = db.similarity_search(
    "What did the president say about Ketanji Brown Jackson",
    k=4,
    doc_builder=custom_document_builder,
)
print("结果：")
print(results[0])
```

## 用于检索增强生成的用法

有关如何将此向量存储用于检索增强生成 (RAG) 的指南，请参阅以下部分：

* [教程](/oss/python/langchain/rag)
* [操作指南：使用 RAG 进行问答](https://python.langchain.com/docs/how_to/#qa-with-rag)
* [检索概念文档](https://python.langchain.com/docs/concepts/retrieval)

# 常见问题解答

## 问题：将文档索引到 Elasticsearch 时出现超时错误。如何解决？

一个可能的问题是您的文档可能需要更长的时间才能索引到 Elasticsearch 中。ElasticsearchStore 使用 Elasticsearch 批量 API，该 API 有一些默认值，您可以调整这些值以减少超时错误的可能性。

当您使用 SparseVectorRetrievalStrategy 时，这也是一个好主意。

默认值为：

* `chunk_size`：500
* `max_chunk_bytes`：100MB

要调整这些值，您可以将 `chunk_size` 和 `max_chunk_bytes` 参数传递给 ElasticsearchStore 的 `add_texts` 方法。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    vector_store.add_texts(
        texts,
        bulk_kwargs={
            "chunk_size": 50,
            "max_chunk_bytes": 200000000
        }
    )
```

# 升级到 ElasticsearchStore

如果您已经在基于 langchain 的项目中使用 Elasticsearch，您可能正在使用旧的实现：`ElasticVectorSearch` 和 `ElasticKNNSearch`，它们现在已被弃用。我们引入了一个新的实现，称为 `ElasticsearchStore`，它更灵活且更易于使用。本笔记本将指导您完成升级到新实现的过程。

## 有什么新功能？

新的实现现在是一个名为 `ElasticsearchStore` 的类，可以通过策略用于近似稠密向量、精确稠密向量、稀疏向量 (ELSER)、BM25 检索和混合检索。

## 我正在使用 ElasticKNNSearch

旧实现：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.vectorstores.elastic_vector_search import ElasticKNNSearch

db = ElasticKNNSearch(
  elasticsearch_url="http://localhost:9200",
  index_name="test_index",
  embedding=embedding
)

```

新实现：

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

db = ElasticsearchStore(
  es_url="http://localhost:9200",
  index_name="test_index",
  embedding=embedding,
  # 如果您使用 model_id
  # strategy=DenseVectorStrategy(model_id="test_model")
  # 如果您使用混合搜索
  # strategy=DenseVectorStrategy(hybrid=True)
)

```

## 我正在使用 ElasticVectorSearch

旧实现：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.vectorstores.elastic_vector_search import ElasticVectorSearch

db = ElasticVectorSearch(
  elasticsearch_url="http://localhost:9200",
  index_name="test_index",
  embedding=embedding
)

```

新实现：

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

db = ElasticsearchStore(
  es_url="http://localhost:9200",
  index_name="test_index",
  embedding=embedding,
  strategy=DenseVectorScriptScoreStrategy()
)

```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
db.client.indices.delete(
    index="test-metadata, test-elser, test-basic",
    ignore_unavailable=True,
    allow_no_indices=True,
)
```

***

## API 参考

有关所有 `ElasticSearchStore` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/python/langchain-elasticsearch/vectorstores/ElasticsearchStore)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/vectorstores/elasticsearch.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
