跳到主要内容

指定过滤器

您可以基于元数据限制您的向量搜索。 Pinecone允许您将元数据键值对附加到索引中的向量,并在查询索引时指定过滤器表达式。

使用元数据过滤的搜索仅检索与过滤器匹配的最近邻结果数量。对于大多数情况,搜索延迟甚至低于未过滤的搜索。

有关元数据过滤的背景信息,请参见:向量搜索中缺少的WHERE子句

支持的元数据类型

您可以将元数据有效负载与索引中的每个向量相关联,作为JSON对象中的键值对,其中键是字符串,值是以下之一:

  • 字符串

  • 数字(整数或浮点数,转换为64位浮点数)

  • 布尔值(true,false)

  • 字符串列表

ℹ️注意

高基数会消耗更多内存: Pinecone将元数据索引以允许进行过滤。如果元数据包含许多唯一值-例如每个向量的唯一标识符-则索引将消耗大量内存。考虑使用选择性元数据索引,以避免索引不需要用于过滤的高基数元数据。

⚠️警告

不支持空元数据值。我们建议您删除元数据有效负载中的该键,而不是设置一个键来保存空值。

例如,以下将是有效的元数据有效负载:

JSON

{
"genre": "action",
"year": 2020,
"length_hrs": 1.5
}

{
"color": "blue",
"fit": "straight",
"price": 29.99,
"is_jeans": true
}

支持的元数据大小

Pinecone支持每个向量40kb的元数据。

元数据查询语言

ℹ️注意

Pinecone 的过滤查询语言基于 MongoDB 的查询和投影操作符。我们目前支持这些选择器的子集。

元数据过滤器可以与 AND 和 OR 结合使用:

  • $eq - Equal to (number, string, boolean)
  • $ne - Not equal to (number, string, boolean)
  • $gt - Greater than (number)
  • $gte - Greater than or equal to (number)
  • $lt - Less than (number)
  • $lte - Less than or equal to (number)
  • $in - In array (string or number)
  • $nin - Not in array (string or number)

使用字符串数组作为元数据值或元数据过滤器

带有元数据负载的向量...

JSON

{ "genre": ["comedy", "documentary"] }

...means the "genre" takes on both values.

例如,以下过滤器的查询将匹配向量:

JSON

{"genre":"comedy"}

{"genre": {"$in":["documentary","action"]}}

{"$and": [{"genre": "comedy"}, {"genre":"documentary"}]}

具有以下过滤器的查询将匹配向量:

JSON

{ "$and": [{ "genre": "comedy" }, { "genre": "drama" }] }

以下过滤器的查询将匹配向量,因为它们是无效的。它们将导致查询编译错误:

# INVALID QUERY:
{"genre": ["comedy", "documentary"]}

# INVALID QUERY:
{"genre": {"$eq": ["comedy", "documentary"]}}

将元数据插入索引

在插入向量时,可以将元数据包含在upsert请求中。

例如,以下是如何插入表示电影的具有元数据的向量到索引中:

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

import pinecone

pinecone.init(api_key="your-api-key", environment="us-west1-gcp")
index = pinecone.Index("example-index")

index.upsert([
("A", [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], {"genre": "comedy", "year": 2020}),
("B", [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2], {"genre": "documentary", "year": 2019}),
("C", [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3], {"genre": "comedy", "year": 2019}),
("D", [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4], {"genre": "drama"}),
("E", [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], {"genre": "drama"})
])

await index.upsert({
vectors: [
{
id: "A",
values: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
metadata: { genre: "comedy", year: 2020 },
},
{
id: "B",
values: [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
metadata: { genre: "documentary", year: 2019 },
},
{
id: "C",
values: [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
metadata: { genre: "comedy", year: 2019 },
},
{
id: "D",
values: [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],
metadata: { genre: "drama" },
},
{
id: "E",
values: [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
metadata: { genre: "drama" },
},
],
});

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": "A",
"values": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
"metadata": {"genre": "comedy", "year": 2020}
},
{
"id": "B",
"values": [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
"metadata": {"genre": "documentary", "year": 2019}
},
{
"id": "C",
"values": [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
"metadata": {"genre": "comedy", "year": 2019}
},
{
"id": "D",
"values": [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],
"metadata": {"genre": "drama"}
},
{
"id": "E",
"values": [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
"metadata": {"genre": "drama"}
}
]
}'

使用元数据过滤查询索引

可以在查询中包含元数据过滤表达式,以限制搜索仅匹配过滤表达式的向量。

例如,我们可以搜索电影索引以查找2019年的纪录片。还使用了include_metadata标志,以便向响应中包含向量元数据。

⚠️警告

出于性能原因,当top_k>1000时,不要返回向量数据和元数据。top_k超过1000的查询不应包含> include_metadata=Trueinclude_data=True

PythonJavaScript

index.query(
vector=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
filter={
"genre": {"$eq": "documentary"},
"year": 2019
},
top_k=1,
include_metadata=True
)

# Returns:
# {'matches': [{'id': 'B',
# 'metadata': {'genre': 'documentary', 'year': 2019.0},
# 'score': 0.0800000429,
# 'values': []}],
# 'namespace': ''}

const queryResponse = await index.query({
vector: [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
filter: { genre: { $in: ["comedy", "documentary", "drama"] } },
topK: 1,
includeMetadata: true,
});
console.log(queryResponse.data);
// Returns:
// {'matches': [{'id': 'B',
// 'metadata': {'genre': 'documentary', 'year': 2019.0},
// 'score': 0.0800000429,
// 'values': []}],
// 'namespace': ''}

curl

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 '{
"vector": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
"filter": {"genre": {"$in": ["comedy", "documentary", "drama"]}},
"topK": 1,
"includeMetadata": true
}'

# Output:
# {
# "matches": [
# {
# "id": "B",
# "score": 0.0800000429,
# "values": [],
# "metadata": {
# "genre": "documentary",
# "year": 2019
# }
# }
# ],
# "namespace": ""
# }

更多过滤表达式示例

喜剧、纪录片或戏剧:

JSON

{
"genre": { "$in": ["comedy", "documentary", "drama"] }
}

2020年的戏剧:

JSON

{
"genre": { "$eq": "drama" },
"year": { "$gte": 2020 }
}

2020年的戏剧(等同于上一个示例):

JSON

{
"$and": [{ "genre": { "$eq": "drama" } }, { "year": { "$gte": 2020 } }]
}

2020年的戏剧或电影:

JSON

{
"$or": [{ "genre": { "$eq": "drama" } }, { "year": { "$gte": 2020 } }]
}

Deleting vectors by metadata filter

To specify vectors to be deleted by metadata values, pass a metadata filter expression to the delete operation. This deletes all vectors matching the metadata filter expression.

Example

This example deletes all vectors with genre "documentary" and year 2019 from an index.

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

index.delete(
filter={
"genre": {"$eq": "documentary"},
"year": 2019
}
)

await index._delete({
filter: {
genre: { $eq: "documentary" },
year: 2019,
},
});

curl -i -X POST https://YOUR_INDEX-YOUR_PROJECT.svc.YOUR_ENVIRONMENT.pinecone.io/vectors/delete \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"filter": {"genre": {"$in": ["comedy", "documentary", "drama"]}}
}'

Updated 3 months ago