管理索引
在本节中,我们将说明如何获取索引列表、创建索引、删除索引和描述索引。
要了解与索引相关的概念,请参见索引。
⚠️警告
Starter(免费)计划上的索引将在7天的不活动后被删除。为了
防止这种情况,请发送任何API请求或登录控制台。这将计算为
活动。
获取有关您的索引的信息
列出您的Pinecone索引:
下面分别是Python、JavaScript和Curl代码
pinecone.list_indexes()
await pinecone.listIndexes();
curl -i https://controller.YOUR_ENVIRONMENT.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY'
获取名为“pinecone-index”的索引的配置和当前状态:
下面分别是Python、JavaScript和Curl代码
pinecone.describe_index("pinecone-index")
await pinecone.describeIndex(indexName);
curl -i -X GET https://controller.YOUR_ENVIRONMENT.pinecone.io/databases/example-index \
-H 'Api-Key: YOUR_API_KEY'
创建索引
创建索引的最简单方法如下。这将给你一个具有单个pod的索引,该pod将使用余弦相似度执行近似最近邻(ANN)搜索:
下面分别是Python、JavaScript和Curl代码
pinecone.create_index("example-index", dimension=128)
await pinecone.createIndex({
name: "example-index",
dimension: 128,
});
curl -i -X POST https://controller.YOUR_ENVIRONMENT.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "example-index",
"dimension": 128
}'
可以按照以下方式创建更复杂的索引。这将创建一个以欧几里得距离度量相似度并在4个大小为x1
的s1(存储优化型)pod上运行的索引:
下面分别是Python、JavaScript和Curl代码
pinecone.create_index("example-index", dimension=128, metric="euclidean", pods=4, pod_type="s1.x1")
await pinecone.createIndex({
name: "example-index",
dimension: 128,
metric: "euclidean",
pods: 4,
podType: "s1.x1",
});
curl -i -X POST https://controller.YOUR_ENVIRONMENT.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "example-index",
"dimension": 128,
"metric": "euclidean",
"pods": 4,
"pod_type": "p1.x1"
}'
从集合创建索引
要从集合创建索引,请使用create_index
操作,并提供一个source_collection
参数,其中包含您希望创建索引的集合名称。新索引是可查询和可写的。
从集合创建索引通常需要约10分钟。当向量数量达到100万数量级时,从集合创建p2索引可能需要几个小时。
示例
以下示例从名为example-collection
的集合中创建一个具有128维的索引,并将其命名为example-index
。
下面分别是Python、JavaScript和Curl代码
pinecone.create_index("example-index", dimension=128, source_collection="example-collection")
await pinecone.createIndex({
name: "example-index",
dimension: 128,
sourceCollection: "example-collection",
});
curl -i -X POST https://controller.us-west1-gcp.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "example-index",
"source_collection":"example-collection"}
}'
有关每个pod类型和大小的更多信息,请参见索引。
要自定义索引的参数列表,请参阅create_index API 参考文档。
更改 Pod 大小
默认的pod大小是x1
。在创建索引之后,您可以增加索引的pod大小。
增加索引的 Pod 大小不会导致停机。在扩容过程中,读写操作将继续不间断。目前,您无法减小索引的 Pod 大小。您的副本数和总 Pod 数保持不变,但每个 Pod 的大小会发生变化。重设大小大约需要 10 分钟。
要了解更多关于 Pod 大小的信息,请参阅索引。
增加索引的 Pod 大小
要更改现有索引的pod大小,请使用configure_index操作,并在pod_type
参数后附加新的大小,用句点(.)分隔。
例子
以下示例假定my_index
的大小为x1
,并将其更改为x2
。
下面分别是Python、JavaScript和Curl代码
pinecone.configure_index("my_index", pod_type="s1.x2")
await client.configureIndex("my_index", {
pod_type: "s1.x2",
});
curl -i -X PATCH https://controller.us-west1-gcp.pinecone.io/databases/example-index \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"pod_type": "s1.x2"
}
}'
检查pod大小变更的状态
要检查pod大小更改的状态,请使用describe_index操作。结果中的status
字段包含键值对"state":"ScalingUp"
或"state":"ScalingDown"
,以表示调整大小过程中的状态,以及键值对"state":"Ready"
表示进程完成后的状态。
直到调整大小过程完成,describe_index_stats
提供的索引完整度度量可能不准确。
例子
以下示例使用describe_index
获取索引example-index
的索引状态。 status
字段包含键值对"state":"ScalingUp"
,表示调整大小过程仍在进行中。
下面分别是Python、JavaScript和Curl代码
pinecone.describe_index("example-index")
await pinecone.describeIndex({
name: "example-index",
});
curl -i -X GET https://controller.us-west1-gcp.pinecone.io/databases/example-index \
-H 'Api-Key: YOUR_API_KEY'
结果:
JSON
{
"database": {
"name": "example-index",
"dimensions": "768",
"metric": "cosine",
"pods": 6,
"replicas": 2,
"shards": 3,
"pod_type": "p1.x2",
"index_config": {},
"status": {
"ready": true,
"state": "ScalingUp"
}
}
}
副本
你可以增加索引的副本数量,以提高吞吐量(QPS)。所有索引都从replicas=1开始。
例子
以下示例使用configure_index
操作将索引example-index
的副本数设置为4。
下面分别是Python、JavaScript和Curl代码
pinecone.configure_index("example-index", replicas=4)
await pinecone.configureIndex("example-index", {
replicas: 4,
});
curl -i -X PATCH https://controller.us-west1-gcp.pinecone.io/databases/example-index \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"replicas": 4
}'
更多详情请参见configure_index API参考。
选择性元数据索引
默认情况下,Pinecone索引所有元数据。当您索引元数据字段时,您可以使用这些字段过滤向量搜索查询。当您存储未索引的元数据字段时,您可以保持内存利用率低,尤其是当您拥有许多唯一的元数据值时,因此可以在每个pod中容纳更多的向量。
创建新索引时,您可以使用metadata_config
参数指定要索引的元数据字段。
下面分别是Python、JavaScript和Curl代码
metadata_config = {
"indexed": ["metadata-field-name"]
}
pinecone.create_index("example-index", dimension=128,
metadata_config=metadata_config)
pinecone.createIndex({
name: "example-index",
dimension: 128,
metadata_config: {
indexed: ["metadata-field-name"],
},
});
curl -i -X POST https://controller.YOUR_ENVIRONMENT.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "example-index",
"dimension": 128,
"metadata_config": {
"indexed": ["metadata-field-name"]
}
}'
metadata_config
参数的值是一个包含要索引的元数据字段名称的JSON对象。
JSON
{
"indexed": [
"metadata-field-1",
"metadata-field-2",
"metadata-field-n"
]
}
当提供metadata_config
对象时,Pinecone只索引该对象中存在的元数据字段,任何未包含在metadata_config
对象中的元数据字段都不会被索引。
当元数据字段被索引时,您可以使用该元数据字段过滤查询;如果元数据字段未被索引,则元数据过滤将忽略该字段。
示例
以下示例创建仅索引genre
元数据字段的索引。针对该索引的针对genre
元数据字段的过滤查询可能会返回结果;针对其他元数据字段的过滤查询将表现为这些字段不存在一样。
下面分别是Python、JavaScript和Curl代码
metadata_config = {
"indexed": ["genre"]
}
pinecone.create_index("example-index", dimension=128,
metadata_config=metadata_config)
pinecone.createIndex({
name: "example-index",
dimension: 128,
metadata_config: {
indexed: ["genre"],
},
});
curl -i -X POST https://controller.us-west1-gcp.pinecone.io/databases \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"name": "example-index",
"dimension": 128,
"metadata_config": {
"indexed": ["genre"]
}
}'
删除索引
此操作将删除与索引关联的所有数据和计算资源。
ℹ️注意
创建索引后,它将作为服务运行,直到您删除它。用户需要支付运行索引的费用,因此我们建议您删除任何不再使用的索引,以最小化成本。
删除一个名为 "pinecone-index" 的 Pinecone 索引:
下面分别是Python、JavaScript和Curl代码
pinecone.delete_index("example-index")
pinecone.deleteIndex("example-index");
curl -i -X DELETE https://controller.YOUR_ENVIRONMENT.pinecone.io/databases/example-index \
-H 'Api-Key: YOUR_API_KEY'
更新时间:8 天前