Skip to content
LlamaIndex 文档版本 跟随上游 源快照 1ab54ebb 更新于 2026-10-15

文档存储存放摄取后的文档块,也就是我们所说的 Node 对象。

更多细节见 API 参考。

简单文档存储

默认情况下,SimpleDocumentStore 把 Node 对象存在内存中。调用 docstore.persist()(以及 SimpleDocumentStore.from_persist_path(...))即可分别把它持久化到磁盘和从磁盘加载。

更完整的例子见这里

MongoDB 文档存储

我们支持把 MongoDB 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

from llama_index.storage.docstore.mongodb import MongoDocumentStore
from llama_index.core.node_parser import SentenceSplitter

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create (or load) docstore and add nodes
docstore = MongoDocumentStore.from_uri(uri="<mongodb+srv://...>")
docstore.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=docstore)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

在底层,MongoDocumentStore 会连接到一个固定的 MongoDB 数据库,并为你的节点初始化新的集合(或加载已有的集合)。

注:实例化 MongoDocumentStore 时可以配置 db_name 与 namespace,否则默认为 db_name="db_docstore" 与 namespace="docstore"。

注意:使用 MongoDocumentStore 时无需调用 storage_context.persist()(或 docstore.persist()),因为它的数据默认就会持久化。

要重新连上你的 MongoDB 集合并重载索引,只需用已有的 db_name 与 collection_name 再初始化一个 MongoDocumentStore。

更完整的例子见这里

Redis 文档存储

我们支持把 Redis 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

from llama_index.storage.docstore.redis import RedisDocumentStore
from llama_index.core.node_parser import SentenceSplitter

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create (or load) docstore and add nodes
docstore = RedisDocumentStore.from_host_and_port(
    host="127.0.0.1", port="6379", namespace="llama_index"
)
docstore.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=docstore)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

在底层,RedisDocumentStore 连接到一个 Redis 数据库,并把你的节点加入位于 {namespace}/docs 的命名空间下。

注:实例化 RedisDocumentStore 时可以配置 namespace,否则默认为 namespace="docstore"。

要重新连上你的 Redis 客户端并重载索引,只需用已有的 host、port 与 namespace 再初始化一个 RedisDocumentStore 即可。

更完整的例子见这里

Firestore 文档存储

我们支持把 Firestore 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

from llama_index.storage.docstore.firestore import FirestoreDocumentStore
from llama_index.core.node_parser import SentenceSplitter

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create (or load) docstore and add nodes
docstore = FirestoreDocumentStore.from_database(
    project="project-id",
    database="(default)",
)
docstore.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=docstore)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

在底层,FirestoreDocumentStore 连接 Google Cloud 上的 firestore 数据库,并把你的节点加入位于 {namespace}/docs 的命名空间下。

注:实例化 FirestoreDocumentStore 时可以配置 namespace,否则默认为 namespace="docstore"。

要重新连上你的 Firestore 数据库并重载索引,只需用已有的 project、database 与 namespace 再初始化一个 FirestoreDocumentStore 即可。

更完整的例子见这里

Couchbase 文档存储

我们支持把 Couchbase 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

from llama_index.storage.docstore.couchbase import CouchbaseDocumentStore
from llama_index.core.node_parser import SentenceSplitter

from couchbase.cluster import Cluster
from couchbase.auth import PasswordAuthenticator
from couchbase.options import ClusterOptions
from datetime import timedelta

# create couchbase client
auth = PasswordAuthenticator("DB_USERNAME", "DB_PASSWORD")
options = ClusterOptions(authenticator=auth)

cluster = Cluster("couchbase://localhost", options)

# Wait until the cluster is ready for use.
cluster.wait_until_ready(timedelta(seconds=5))

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create (or load) docstore and add nodes
docstore = CouchbaseDocumentStore.from_couchbase_client(
    client=cluster,
    bucket_name="llama-index",
    scope_name="_default",
    namespace="default",
)
docstore.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=docstore)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

在底层,CouchbaseDocumentStore 连接到一个 Couchbase 事务型数据库,并把你的节点加入指定 {bucket_name} 与 {scope_name} 下名为 {namespace}_data 的集合。

注:实例化 CouchbaseIndexStore 时可以配置 namespace、bucket 与 scope。默认使用的集合是 docstore_data。集合名除字母数字外,只允许在其中包含 -、_ 与 %。对其他特殊字符,该存储会自动转换成 _。

要重新连上你的 Couchbase 数据库并重载索引,只需用已有的 client、bucket_name、scope_name 与 namespace 再初始化一个 CouchbaseDocumentStore 即可。

Tablestore 文档存储

我们支持把 Tablestore 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

from llama_index.core import Document
from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter

from llama_index.storage.docstore.tablestore import TablestoreDocumentStore

# create parser and parse document into nodes
parser = SentenceSplitter()
documents = [
    Document(text="I like cat.", id_="1", metadata={"key1": "value1"}),
    Document(text="Mike likes dog.", id_="2", metadata={"key2": "value2"}),
]
nodes = parser.get_nodes_from_documents(documents)

# create (or load) doc_store and add nodes
docs_tore = TablestoreDocumentStore.from_config(
    endpoint="<tablestore_end_point>",
    instance_name="<tablestore_instance_name>",
    access_key_id="<tablestore_access_key_id>",
    access_key_secret="<tablestore_access_key_secret>",
)
docs_tore.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=docs_tore)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

在底层,TablestoreDocumentStore 连接到一个 Tablestore 数据库,并把你的节点加入名为 {namespace}_data 的表。

注:实例化 TablestoreDocumentStore 时可以配置 namespace。

要重新连上你的 Tablestore 数据库并重载索引,只需用已有的 endpoint、instance_name、access_key_id 与 access_key_secret 再初始化一个 TablestoreDocumentStore 即可。

更完整的例子见这里

Google AlloyDB 文档存储

我们支持把 AlloyDB 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

本教程演示的是同步接口。所有同步方法都有对应的异步方法。

终端窗口

pip install llama-index
pip install llama-index-alloydb-pg
pip install llama-index-llms-vertex
from llama_index.core import SummaryIndex
from llama_index_alloydb_pg import AlloyDBEngine, AlloyDBDocumentStore

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create an AlloyDB Engine for connection pool
engine = AlloyDBEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    cluster=CLUSTER,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)

# initialize a new table in AlloyDB
engine.init_doc_store_table(
    table_name=TABLE_NAME,
)

doc_store = AlloyDBDocumentStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

doc_store.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=doc_store)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

注:初始化新表并实例化 AlloyDBDocumentStore 时,可以同时配置 schema_name 与 table_name。schema_name 默认为 public。

在底层,AlloyDBDocumentStore 连接 Google Cloud 上的 alloydb 数据库,并把你的节点加入该 schema_name 下的一张表。

要重新连上你的 AlloyDB 数据库并重载索引,只需用一个 AlloyDBEngine(不再初始化新表)再实例化一个 AlloyDBDocumentStore 即可。

更详细的指南见这里

Google Cloud SQL for PostgreSQL 文档存储

我们支持把 Cloud SQL for PostgreSQL 作为另一种文档存储后端,它在 Node 对象被摄取的同时就持久化数据。

本教程演示的是同步接口。所有同步方法都有对应的异步方法。

终端窗口

pip install llama-index
pip install llama-index-cloud-sql-pg
from llama_index.core import SummaryIndex
from llama_index_cloud_sql_pg import PostgresEngine, PostgresDocumentStore

# create parser and parse document into nodes
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

# create an Postgres Engine for connection pool
engine = PostgresEngine.from_instance(
    project_id=PROJECT_ID,
    region=REGION,
    instance=INSTANCE,
    database=DATABASE,
    user=USER,
    password=PASSWORD,
)

# initialize a new table in cloud sql postgres
engine.init_doc_store_table(
    table_name=TABLE_NAME,
)

doc_store = PostgresDocumentStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

doc_store.add_documents(nodes)

# create storage context
storage_context = StorageContext.from_defaults(docstore=doc_store)

# build index
index = VectorStoreIndex(nodes, storage_context=storage_context)

注:初始化新表并实例化 PostgresDocumentStore 时,可以同时配置 schema_name 与 table_name。schema_name 默认为 public。

在底层,PostgresDocumentStore 连接 Google Cloud 上的 cloud sql for pg 数据库,并把你的节点加入该 schema_name 下的一张表。

要重新连上你的 Postgres 数据库并重载索引,只需用一个 PostgresEngine(不再初始化新表)再实例化一个 PostgresDocumentStore 即可。

更详细的指南见这里

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。