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

索引存储存放轻量的索引元数据(即构建索引时额外产生的状态信息)。

更多细节见 API 参考。

简单索引存储

默认情况下,LlamaIndex 使用一个由内存键值存储支撑的简单索引存储。调用 index_store.persist()(以及 SimpleIndexStore.from_persist_path(...))即可分别把它持久化到磁盘和从磁盘加载。

MongoDB 索引存储

你也可以用 MongoDB 作为索引存储的存储后端。开始前,请先执行以下命令安装集成包:

终端窗口

pip install llama-index-storage-index-store-mongodb

然后用下面的代码创建一个 MongoIndexStore、连上你的 MongoDB 部署,并构建或加载一个 VectorStoreIndex:

from llama_index.storage.index_store.mongodb import MongoIndexStore
from llama_index.core import (
    VectorStoreIndex,
    StorageContext,
    load_index_from_storage,
)

# create (or load) index store
index_store = MongoIndexStore.from_uri(uri="<mongodb+srv://...>")

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

# build index from your parsed nodes
index = VectorStoreIndex(nodes, storage_context=storage_context)

# or alternatively, load an existing index
index = load_index_from_storage(storage_context)

MongoIndexStore 连接到 MongoDB 数据库,并为你的索引元数据初始化新的集合(或加载已有的集合)。

你可以使用自行管理的 MongoDB 部署,也可以使用 MongoDB Atlas 集群。如何获取连接串,见 MongoDB 文档中的 Connection Strings。

注:实例化 MongoIndexStore 时可以配置 db_name、namespace 与 collection_suffix。否则默认为 db_name="db_docstore"、namespace="index_store"、collection_suffix="/data",此时 MongoIndexStore 会写入 db_docstore 数据库中的 index_store/data 集合。

使用 MongoIndexStore 时无需调用 storage_context.persist()(或 index_store.persist()),因为数据默认就会持久化。

要重新连上你的集合并重载索引,只需用相同的 db_name、namespace 与 collection_suffix 取值再初始化一个 MongoIndexStore。

更完整的例子见 MongoDB docstore 演示。

Redis 索引存储

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

from llama_index.storage.index_store.redis import RedisIndexStore
from llama_index.core import VectorStoreIndex

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

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

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

# or alternatively, load index
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

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

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

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

更完整的例子见这里

Couchbase 索引存储

Couchbase 也可以作为索引存储的存储后端。

from llama_index.storage.index_store.couchbase import CouchbaseIndexStore
from llama_index.core import VectorStoreIndex

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 (or load) docstore and add nodes
index_store = CouchbaseIndexStore.from_couchbase_client(
    client=cluster,
    bucket_name="llama-index",
    scope_name="_default",
    namespace="default",
)

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

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

# or alternatively, load index
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

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

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

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

Tablestore 索引存储

与文档存储类似,我们也可以把 Tablestore 作为索引存储的存储后端。

from llama_index.storage.index_store.tablestore import TablestoreIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# create (or load) index store
index_store = TablestoreIndexStore.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>",
)

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

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

# or alternatively, load index
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

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

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

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

更完整的例子见这里

Google AlloyDB 索引存储

与文档存储类似,我们也可以把 AlloyDB 作为索引存储的存储后端。本教程演示的是同步接口,所有同步方法都有对应的异步方法。

终端窗口

pip install llama-index
pip install llama-index-alloydb-pg
pip install llama-index-llms-vertex
from llama_index_alloydb_pg import AlloyDBEngine, AlloyDBIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# 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_index_store_table(
    table_name=TABLE_NAME,
)

index_store = AlloyDBIndexStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

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

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

# or alternatively, load index
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

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

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

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

更详细的指南见这里

Google Cloud SQL for PostgreSQL 索引存储

与文档存储类似,我们也可以把 Cloud SQL for PostgreSQL 作为索引存储的存储后端。本教程演示的是同步接口,所有同步方法都有对应的异步方法。

终端窗口

pip install llama-index
pip install llama-index-cloud-sql-pg
from llama_index_cloud_sql_pg import PostgresEngine, PostgresIndexStore
from llama_index.core import StorageContext, VectorStoreIndex

# 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_index_store_table(
    table_name=TABLE_NAME,
)

index_store = PostgresIndexStore.create_sync(
    engine=engine,
    table_name=TABLE_NAME,
)

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

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

# or alternatively, load index
from llama_index.core import load_index_from_storage

index = load_index_from_storage(storage_context)

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

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

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

更详细的指南见这里

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