Skip to content

Opensearch

OpensearchVectorStore #

Bases: BasePydanticVectorStore

# Opensearch/Opensearch向量存储。

# Args:
#     client (OpensearchVectorClient):用于数据插入/查询的向量索引客户端。

# 示例:
#     `pip install llama-index-vector-stores-opensearch`

#     ```python
#     from llama_index.vector_stores.opensearch import (
#         OpensearchVectorStore,
#         OpensearchVectorClient,
#     )

#     # 集群的http端点(需要opensearch来使用向量索引)
#     endpoint = "http://localhost:9200"
#     # 用于演示VectorStore实现的索引
#     idx = "gpt-index-demo"

#     # OpensearchVectorClient默认将文本存储在此字段中
#     text_field = "content"
#     # OpensearchVectorClient默认将嵌入存储在此字段中
#     embedding_field = "embedding"

#     # OpensearchVectorClient封装了启用向量搜索的单个opensearch索引的逻辑
#     client = OpensearchVectorClient(
#         endpoint, idx, 1536, embedding_field=embedding_field, text_field=text_field
#     )

#     # 初始化向量存储
#     vector_store = OpensearchVectorStore(client)
#     ```
Source code in llama_index/vector_stores/opensearch/base.py
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
class OpensearchVectorStore(BasePydanticVectorStore):
    """```python
# Opensearch/Opensearch向量存储。

# Args:
#     client (OpensearchVectorClient):用于数据插入/查询的向量索引客户端。

# 示例:
#     `pip install llama-index-vector-stores-opensearch`

#     ```python
#     from llama_index.vector_stores.opensearch import (
#         OpensearchVectorStore,
#         OpensearchVectorClient,
#     )

#     # 集群的http端点(需要opensearch来使用向量索引)
#     endpoint = "http://localhost:9200"
#     # 用于演示VectorStore实现的索引
#     idx = "gpt-index-demo"

#     # OpensearchVectorClient默认将文本存储在此字段中
#     text_field = "content"
#     # OpensearchVectorClient默认将嵌入存储在此字段中
#     embedding_field = "embedding"

#     # OpensearchVectorClient封装了启用向量搜索的单个opensearch索引的逻辑
#     client = OpensearchVectorClient(
#         endpoint, idx, 1536, embedding_field=embedding_field, text_field=text_field
#     )

#     # 初始化向量存储
#     vector_store = OpensearchVectorStore(client)
#     ```
```"""

    stores_text: bool = True
    _client: OpensearchVectorClient = PrivateAttr(default=None)

    def __init__(
        self,
        client: OpensearchVectorClient,
    ) -> None:
        """初始化参数。"""
        super().__init__()
        self._client = client

    @property
    def client(self) -> Any:
        """获取客户端。"""
        return self._client

    def add(
        self,
        nodes: List[BaseNode],
        **add_kwargs: Any,
    ) -> List[str]:
        """将节点添加到索引中。

Args:
    nodes: List[BaseNode]: 带有嵌入的节点列表。
"""
        return asyncio.get_event_loop().run_until_complete(
            self.async_add(nodes, **add_kwargs)
        )

    async def async_add(
        self,
        nodes: List[BaseNode],
        **add_kwargs: Any,
    ) -> List[str]:
        """异步将节点添加到索引中。

Args:
    nodes: List[BaseNode]: 带有嵌入的节点列表。
"""
        await self._client.index_results(nodes)
        return [result.node_id for result in nodes]

    def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
        """使用ref_doc_id删除节点。

Args:
    ref_doc_id(str):应删除节点的文档的doc_id。
"""
        asyncio.get_event_loop().run_until_complete(
            self.adelete(ref_doc_id, **delete_kwargs)
        )

    async def adelete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
        """使用ref_doc_id异步删除节点。

Args:
    ref_doc_id(str):应删除节点的文档的doc_id。
"""
        await self._client.delete_by_doc_id(ref_doc_id)

    def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
        """查询前k个最相似节点的索引。

Args:
    query (VectorStoreQuery): 存储查询对象。
"""
        return asyncio.get_event_loop().run_until_complete(self.aquery(query, **kwargs))

    async def aquery(
        self, query: VectorStoreQuery, **kwargs: Any
    ) -> VectorStoreQueryResult:
        """异步查询索引以获取最相似的前k个节点。

Args:
    query (VectorStoreQuery): 存储查询对象。
"""
        query_embedding = cast(List[float], query.query_embedding)

        return await self._client.aquery(
            query.mode,
            query.query_str,
            query_embedding,
            query.similarity_top_k,
            filters=query.filters,
        )

client property #

client: Any

获取客户端。

add #

add(nodes: List[BaseNode], **add_kwargs: Any) -> List[str]

将节点添加到索引中。

Parameters:

Name Type Description Default
nodes List[BaseNode]

List[BaseNode]: 带有嵌入的节点列表。

required
Source code in llama_index/vector_stores/opensearch/base.py
482
483
484
485
486
487
488
489
490
491
492
493
494
    def add(
        self,
        nodes: List[BaseNode],
        **add_kwargs: Any,
    ) -> List[str]:
        """将节点添加到索引中。

Args:
    nodes: List[BaseNode]: 带有嵌入的节点列表。
"""
        return asyncio.get_event_loop().run_until_complete(
            self.async_add(nodes, **add_kwargs)
        )

async_add async #

async_add(
    nodes: List[BaseNode], **add_kwargs: Any
) -> List[str]

异步将节点添加到索引中。

Parameters:

Name Type Description Default
nodes List[BaseNode]

List[BaseNode]: 带有嵌入的节点列表。

required
Source code in llama_index/vector_stores/opensearch/base.py
496
497
498
499
500
501
502
503
504
505
506
507
    async def async_add(
        self,
        nodes: List[BaseNode],
        **add_kwargs: Any,
    ) -> List[str]:
        """异步将节点添加到索引中。

Args:
    nodes: List[BaseNode]: 带有嵌入的节点列表。
"""
        await self._client.index_results(nodes)
        return [result.node_id for result in nodes]

delete #

delete(ref_doc_id: str, **delete_kwargs: Any) -> None

使用ref_doc_id删除节点。

Source code in llama_index/vector_stores/opensearch/base.py
509
510
511
512
513
514
515
516
517
    def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
        """使用ref_doc_id删除节点。

Args:
    ref_doc_id(str):应删除节点的文档的doc_id。
"""
        asyncio.get_event_loop().run_until_complete(
            self.adelete(ref_doc_id, **delete_kwargs)
        )

adelete async #

adelete(ref_doc_id: str, **delete_kwargs: Any) -> None

使用ref_doc_id异步删除节点。

Source code in llama_index/vector_stores/opensearch/base.py
519
520
521
522
523
524
525
    async def adelete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
        """使用ref_doc_id异步删除节点。

Args:
    ref_doc_id(str):应删除节点的文档的doc_id。
"""
        await self._client.delete_by_doc_id(ref_doc_id)

query #

query(
    query: VectorStoreQuery, **kwargs: Any
) -> VectorStoreQueryResult

查询前k个最相似节点的索引。

Parameters:

Name Type Description Default
query VectorStoreQuery

存储查询对象。

required
Source code in llama_index/vector_stores/opensearch/base.py
527
528
529
530
531
532
533
    def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
        """查询前k个最相似节点的索引。

Args:
    query (VectorStoreQuery): 存储查询对象。
"""
        return asyncio.get_event_loop().run_until_complete(self.aquery(query, **kwargs))

aquery async #

aquery(
    query: VectorStoreQuery, **kwargs: Any
) -> VectorStoreQueryResult

异步查询索引以获取最相似的前k个节点。

Parameters:

Name Type Description Default
query VectorStoreQuery

存储查询对象。

required
Source code in llama_index/vector_stores/opensearch/base.py
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
    async def aquery(
        self, query: VectorStoreQuery, **kwargs: Any
    ) -> VectorStoreQueryResult:
        """异步查询索引以获取最相似的前k个节点。

Args:
    query (VectorStoreQuery): 存储查询对象。
"""
        query_embedding = cast(List[float], query.query_embedding)

        return await self._client.aquery(
            query.mode,
            query.query_str,
            query_embedding,
            query.similarity_top_k,
            filters=query.filters,
        )