跳到主要内容

使用命名空间

Pinecone允许您将索引中的向量划分为命名空间。然后,查询和其他操作仅限于一个命名空间,因此不同的请求可以搜索索引的不同子集。

例如,您可能想为按内容索引的文章定义一个命名空间,为按标题索引的文章定义另一个命名空间。有关完整示例,请参见:语义文本搜索(示例)

每个索引都由一个或多个命名空间组成。每个向量存在于恰好一个命名空间中。

命名空间由一个命名空间名称唯一标识,几乎所有操作都接受它作为参数,以便将它们的工作限制在指定的命名空间中。当您没有为操作指定命名空间名称时,Pinecone会使用""(空字符串)作为默认命名空间名称。

创建命名空间

可以在向量上插入时指定目标命名空间。如果该命名空间不存在,则会隐式创建它。

下面的示例将在不存在时创建一个"my-first-namespace"命名空间:

下面分别是Python、JavaScript和Curl代码

# Upsert vectors while creating a new namespace
index.upsert(vectors=[('id-1', [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])],
namespace='my-first-namespace')

await index.upsert({
vectors: [
{
id: "id-1",
values: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
},
],
namespace: "my-first-namespace",
});

curl -i -X POST https://YOUR_INDEX-YOUR_PROJECT.svc.YOUR_ENVIRONMENT.pinecone.io/vectors/upsert \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"vectors": [
{
"id": "id-1",
"values": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
}
],
"namespace": "my-first-namespace"
}'

然后,您可以提交查询和其他操作,将该命名空间作为参数指定。例如,要查询命名空间"my-first-namespace"中的向量:

下面分别是Python、JavaScript和Curl代码

index.query(vector=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
top_k=1,
namespace='my-first-namespace')

const queryResponse = await index.query({
namespace: "my-first-namespace",
topK: 1,
vector: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
});
console.log(queryResponse.data);

curl -i -X POST https://YOUR_INDEX-YOUR_PROJECT.svc.YOUR_ENVIRONMENT.pinecone.io/query \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"topK": 1,
"vector": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
"namespace": "my-first-namespace"
}'

创建多个命名空间

您可以创建多个命名空间。例如,将数据插入到不同的命名空间中:

下面分别是Python和Curl代码

import numpy as np

# Create three sets of 8-dimensional vectors
vectors_a = np.random.rand(15, 8).tolist()
vectors_b = np.random.rand(20, 8).tolist()
vectors_c = np.random.rand(30, 8).tolist()

# Create ids
ids_a = map(str, np.arange(15).tolist())
ids_b = map(str, np.arange(20).tolist())
ids_c = map(str, np.arange(30).tolist())

# Insert into separate namespaces
index.upsert(vectors=zip(ids_a,vectors_a),namespace='namespace_a')
index.upsert(vectors=zip(ids_b,vectors_b),namespace='namespace_b')

# if no namespaces are specified, the index uses the default namespace
index.upsert(vectors=zip(ids_c,vectors_c))

# At this point, index.describe_index_stats() returns:
# {'dimension': 8,
# 'namespaces': {'': {'vector_count': 30},
# 'namespace_a': {'vector_count': 15},
# 'namespace_b': {'vector_count': 20}}}

# No example

跨所有命名空间的操作

所有向量操作都适用于单个命名空间,唯一的例外是:

DescribeIndexStatistics操作返回关于索引中所有命名空间的内容的每个命名空间统计信息。了解更多详情 更新时间 4个月前