> ## 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.

# OpenSearch 集成

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

> [OpenSearch](https://opensearch.org/) 是一个可扩展、灵活且可扩展的开源软件套件，用于搜索、分析和可观测性应用，基于 Apache 2.0 许可证。`OpenSearch` 是一个基于 `Apache Lucene` 的分布式搜索和分析引擎。

本笔记本展示了如何使用与 `OpenSearch` 数据库相关的功能。

要运行，你应该有一个正在运行的 OpenSearch 实例：[参见此处获取简单的 Docker 安装](https://hub.docker.com/r/opensearchproject/opensearch)。

`similarity_search` 默认执行近似 k-NN 搜索，该搜索使用几种算法之一，如 lucene、nmslib、faiss，推荐用于大型数据集。要执行暴力搜索，我们有其他搜索方法，称为脚本评分和无痛脚本。查看[此处](https://opensearch.org/docs/latest/search-plugins/knn/index/)了解更多详情。

## 安装

安装 Python 客户端。

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

我们想使用 [`OpenAIEmbeddings`](https://reference.langchain.com/python/langchain-openai/embeddings/base/OpenAIEmbeddings)，所以我们需要获取 OpenAI API 密钥。

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

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import OpenSearchVectorSearch
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import TextLoader

loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
```

## 使用近似 k-NN 的 similarity\_search

使用自定义参数的 `近似 k-NN` 搜索的 `similarity_search`

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docsearch = OpenSearchVectorSearch.from_documents(
    docs, embeddings, opensearch_url="http://localhost:9200"
)

# 如果使用默认的 Docker 安装，请改用此实例化方式：
# docsearch = OpenSearchVectorSearch.from_documents(
#     docs,
#     embeddings,
#     opensearch_url="https://localhost:9200",
#     http_auth=("admin", "admin"),
#     use_ssl = False,
#     verify_certs = False,
#     ssl_assert_hostname = False,
#     ssl_show_warn = False,
# )
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query, k=10)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].page_content)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docsearch = OpenSearchVectorSearch.from_documents(
    docs,
    embeddings,
    opensearch_url="http://localhost:9200",
    engine="faiss",
    space_type="innerproduct",
    ef_construction=256,
    m=48,
)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(query)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].page_content)
```

## 使用脚本评分的 similarity\_search

使用自定义参数的 `脚本评分` 的 `similarity_search`

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docsearch = OpenSearchVectorSearch.from_documents(
    docs, embeddings, opensearch_url="http://localhost:9200", is_appx_search=False
)

query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(
    "What did the president say about Ketanji Brown Jackson",
    k=1,
    search_type="script_scoring",
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].page_content)
```

## 使用无痛脚本的 similarity\_search

使用自定义参数的 `无痛脚本` 的 `similarity_search`

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docsearch = OpenSearchVectorSearch.from_documents(
    docs, embeddings, opensearch_url="http://localhost:9200", is_appx_search=False
)
filter = {"bool": {"filter": {"term": {"text": "smuggling"}}}}
query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.similarity_search(
    "What did the president say about Ketanji Brown Jackson",
    search_type="painless_scripting",
    space_type="cosineSimilarity",
    pre_filter=filter,
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].page_content)
```

## 最大边际相关性搜索 (MMR)

如果你想查找一些相似的文档，但同时也希望获得多样化的结果，MMR 是你应该考虑的方法。最大边际相关性优化了与查询的相似性以及所选文档之间的多样性。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query = "What did the president say about Ketanji Brown Jackson"
docs = docsearch.max_marginal_relevance_search(query, k=2, fetch_k=10, lambda_param=0.5)
```

## 使用预先存在的 OpenSearch 实例

也可以使用一个预先存在的 OpenSearch 实例，其中文档已经包含向量。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 这只是一个示例，你需要更改这些值以指向另一个 opensearch 实例
docsearch = OpenSearchVectorSearch(
    index_name="index-*",
    embedding_function=embeddings,
    opensearch_url="http://localhost:9200",
)

# 你可以指定自定义字段名称，以匹配你用于存储嵌入、文档文本值和元数据的字段
docs = docsearch.similarity_search(
    "Who was asking about getting lunch today?",
    search_type="script_scoring",
    space_type="cosinesimil",
    vector_field="message_embedding",
    text_field="message",
    metadata_field="message_metadata",
)
```

## 使用 AOSS (Amazon OpenSearch Service 无服务器)

这是一个使用 `faiss` 引擎和 `efficient_filter` 的 `AOSS` 示例。

我们需要安装几个 `python` 包。

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import boto3
from opensearchpy import RequestsHttpConnection
from requests_aws4auth import AWS4Auth

service = "aoss"  # 必须将服务设置为 'aoss'
region = "us-east-2"
credentials = boto3.Session(
    aws_access_key_id="xxxxxx", aws_secret_access_key="xxxxx"
).get_credentials()
awsauth = AWS4Auth("xxxxx", "xxxxxx", region, service, session_token=credentials.token)

docsearch = OpenSearchVectorSearch.from_documents(
    docs,
    embeddings,
    opensearch_url="host url",
    http_auth=awsauth,
    timeout=300,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection,
    index_name="test-index-using-aoss",
    engine="faiss",
)

docs = docsearch.similarity_search(
    "What is feature selection",
    efficient_filter=filter,
    k=200,
)
```

## 使用 AOS (Amazon OpenSearch Service)

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 这只是一个示例，展示如何使用 Amazon OpenSearch Service，你需要设置适当的值。
import boto3
from opensearchpy import RequestsHttpConnection

service = "es"  # 必须将服务设置为 'es'
region = "us-east-2"
credentials = boto3.Session(
    aws_access_key_id="xxxxxx", aws_secret_access_key="xxxxx"
).get_credentials()
awsauth = AWS4Auth("xxxxx", "xxxxxx", region, service, session_token=credentials.token)

docsearch = OpenSearchVectorSearch.from_documents(
    docs,
    embeddings,
    opensearch_url="host url",
    http_auth=awsauth,
    timeout=300,
    use_ssl=True,
    verify_certs=True,
    connection_class=RequestsHttpConnection,
    index_name="test-index",
)

docs = docsearch.similarity_search(
    "What is feature selection",
    k=200,
)
```

***

<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/vectorstores/opensearch.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
