> ## 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 BM25 集成

> 使用 LangChain Python 与 Elasticsearch BM25 检索器集成。

> [Elasticsearch](https://www.elastic.co/elasticsearch/) 是一个分布式、RESTful 的搜索和分析引擎。它提供了一个分布式、多租户的全文搜索引擎，具有 HTTP Web 接口和无模式的 JSON 文档。

> 在信息检索中，[Okapi BM25](https://en.wikipedia.org/wiki/Okapi_BM25)（BM 是 best matching 的缩写）是一种排序函数，搜索引擎用它来估计文档与给定搜索查询的相关性。它基于 Stephen E. Robertson、Karen Spärck Jones 等人在 1970 年代和 1980 年代开发的概率检索框架。

> 实际排序函数的名称是 BM25。更全的名称 Okapi BM25 包含了第一个使用它的系统的名称，即 Okapi 信息检索系统，该系统于 1980 年代和 1990 年代在伦敦城市大学实现。BM25 及其更新的变体，例如 BM25F（一种可以考虑文档结构和锚文本的 BM25 版本），代表了用于文档检索的类 TF-IDF 检索函数。

本笔记本展示了如何使用一个使用 `ElasticSearch` 和 `BM25` 的检索器。

有关 BM25 细节的更多信息，请参阅[这篇博客文章](https://www.elastic.co/blog/practical-bm25-part-2-the-bm25-algorithm-and-its-variables)。

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.retrievers import (
    ElasticSearchBM25Retriever,
)
```

## 创建新的检索器

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
elasticsearch_url = "http://localhost:9200"
retriever = ElasticSearchBM25Retriever.create(elasticsearch_url, "langchain-index-4")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 或者，你可以加载一个现有的索引
# import elasticsearch
# elasticsearch_url="http://localhost:9200"
# retriever = ElasticSearchBM25Retriever(elasticsearch.Elasticsearch(elasticsearch_url), "langchain-index")
```

## 添加文本（如果需要）

我们可以选择性地向检索器添加文本（如果它们尚未存在）

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
retriever.add_texts(["foo", "bar", "world", "hello", "foo bar"])
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
['cbd4cb47-8d9f-4f34-b80e-ea871bc49856',
 'f3bd2e24-76d1-4f9b-826b-ec4c0e8c7365',
 '8631bfc8-7c12-48ee-ab56-8ad5f373676e',
 '8be8374c-3253-4d87-928d-d73550a2ecf0',
 'd79f457b-2842-4eab-ae10-77aa420b53d7']
```

## 使用检索器

我们现在可以使用检索器了！

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
result = retriever.invoke("foo")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
result
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[Document(page_content='foo', metadata={}),
 Document(page_content='foo bar', metadata={})]
```

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

***

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

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