Skip to content

Opensearch

OpensearchReader #

Bases: BaseReader

从Opensearch索引中读取文档。

然后可以在下游的Llama索引数据结构中使用这些文档。

Parameters:

Name Type Description Default
endpoint str

不带端口的集群URL(http/https)

required
index str

索引名称(必填)

required
basic_auth set

基本认证用户名和密码

None
Source code in llama_index/readers/opensearch/base.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class OpensearchReader(BaseReader):
    """从Opensearch索引中读取文档。

然后可以在下游的Llama索引数据结构中使用这些文档。

Args:
    endpoint (str): 不带端口的集群URL(http/https)
    index (str): 索引名称(必填)
    basic_auth (set): 基本认证用户名和密码"""

    def __init__(
        self, host: str, port: int, index: str, basic_auth: Optional[set] = None
    ):
        """使用参数进行初始化。"""
        from opensearchpy import OpenSearch

        self._opster_client = OpenSearch(
            hosts=[{"host": host, "port": port}],
            http_compress=True,  # enables gzip compression for request bodies
            http_auth=basic_auth,
            use_ssl=True,
            verify_certs=False,
            ssl_assert_hostname=False,
            ssl_show_warn=False,
        )
        self._index = index

    def load_data(
        self,
        field: str,
        query: Optional[dict] = None,
        embedding_field: Optional[str] = None,
    ) -> List[Document]:
        """从Opensearch索引中读取数据。

Args:
    field (str): 从文档中检索文本的字段
    query (Optional[dict]): Opensearch JSON查询DSL对象。
        例如:
        { "query" : {"match": {"message": {"query": "this is a test"}}}}
    embedding_field (Optional[str]): 如果在该索引中存储了嵌入,可以使用该字段
        来设置返回的文档列表上的嵌入字段。

Returns:
    List[Document]: 文档列表。
"""
        res = self._opster_client.search(body=query, index=self._index)
        documents = []
        for hit in res["hits"]["hits"]:
            value = hit["_source"][field]
            _ = hit["_source"].pop(field)
            embedding = hit["_source"].get(embedding_field or "", None)
            documents.append(
                Document(text=value, extra_info=hit["_source"], embedding=embedding)
            )
        return documents

load_data #

load_data(
    field: str,
    query: Optional[dict] = None,
    embedding_field: Optional[str] = None,
) -> List[Document]

从Opensearch索引中读取数据。

Parameters:

Name Type Description Default
field str

从文档中检索文本的字段

required
query Optional[dict]

Opensearch JSON查询DSL对象。 例如: { "query" : {"match": {"message": {"query": "this is a test"}}}}

None
embedding_field Optional[str]

如果在该索引中存储了嵌入,可以使用该字段 来设置返回的文档列表上的嵌入字段。

None

Returns:

Type Description
List[Document]

List[Document]: 文档列表。

Source code in llama_index/readers/opensearch/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    def load_data(
        self,
        field: str,
        query: Optional[dict] = None,
        embedding_field: Optional[str] = None,
    ) -> List[Document]:
        """从Opensearch索引中读取数据。

Args:
    field (str): 从文档中检索文本的字段
    query (Optional[dict]): Opensearch JSON查询DSL对象。
        例如:
        { "query" : {"match": {"message": {"query": "this is a test"}}}}
    embedding_field (Optional[str]): 如果在该索引中存储了嵌入,可以使用该字段
        来设置返回的文档列表上的嵌入字段。

Returns:
    List[Document]: 文档列表。
"""
        res = self._opster_client.search(body=query, index=self._index)
        documents = []
        for hit in res["hits"]["hits"]:
            value = hit["_source"][field]
            _ = hit["_source"].pop(field)
            embedding = hit["_source"].get(embedding_field or "", None)
            documents.append(
                Document(text=value, extra_info=hit["_source"], embedding=embedding)
            )
        return documents