在 Vector Search 中,您可以使用布林規則,將向量比對搜尋限制在索引的子集中。布林述詞會告知 Vector Search 要忽略索引中的哪些向量。本頁面將說明篩選功能運作方式、提供範例,以及如何根據向量相似度有效查詢資料。
使用向量搜尋時,您可以依類別和數值限制結果。新增限制或「篩選」索引結果有許多用途,例如:
提升結果關聯性:向量搜尋是強大的工具,可找出語意相似的項目。篩選功能可從搜尋結果中移除不相關的項目,例如語言、類別、價格或日期範圍不正確的項目。
減少結果數量:向量搜尋可能會傳回大量結果,尤其是針對大型資料集。篩選功能可減少結果數量,方便管理,同時仍會傳回最相關的結果。
區隔結果:篩選功能可根據使用者的個人需求和偏好設定,提供個人化的搜尋結果。舉例來說,使用者可能只想查看自己過去給予高評價的項目,或是特定價格範圍內的項目。
向量屬性
在向量資料庫中進行向量相似度搜尋時,每個向量都會由零或多個屬性描述。這些屬性稱為權杖限制的「權杖」,以及數值限制的「值」。這些限制可能來自多個屬性類別,也就是命名空間。
在下列範例應用程式中,向量會標記 color
、price
和 shape
:
color
、price
和shape
是命名空間。red
和blue
是color
命名空間的權杖。square
和circle
是shape
命名空間的權杖。100
和50
是price
命名空間中的值。
指定向量屬性
- 如要指定「紅圈」:
{color: red}, {shape: circle}
。 - 如要指定「紅色或藍色正方形」:
{color: red, blue}, {shape: square}
。 - 如要指定沒有顏色的物件,請在
restricts
欄位中省略「color」命名空間。 - 如要為物件指定數值限制,請記下命名空間和類型適當欄位中的值。整數值應在
value_int
中指定,浮點值應在value_float
中指定,雙精度值應在value_double
中指定。特定命名空間只能使用一種號碼類型。
如要瞭解指定這項資料時使用的結構定義,請參閱「在輸入資料中指定命名空間和權杖」。
查詢
- 查詢會在命名空間之間表示 AND 邏輯運算子,並在每個命名空間內表示 OR 邏輯運算子。指定
{color: red, blue}, {shape: square, circle}
的查詢會比對所有符合(red || blue) && (square || circle)
的資料庫點。 - 指定
{color: red}
的查詢會比對任何種類的所有red
物件,且不會限制shape
。 - 查詢中的數值限制需要
namespace
、value_int
、value_float
和value_double
其中一個數值,以及op
運算子。 - 運算子
op
為LESS
、LESS_EQUAL
、EQUAL
、GREATER_EQUAL
和GREATER
其中之一。舉例來說,如果使用LESS_EQUAL
運算子,只要資料點的值小於或等於查詢中使用的值,即符合條件。
下列程式碼範例會找出範例應用程式中的向量屬性:
[
{
"namespace": "price",
"value_int": 20,
"op": "LESS"
},
{
"namespace": "length",
"value_float": 0.3,
"op": "GREATER_EQUAL"
},
{
"namespace": "width",
"value_double": 0.5,
"op": "EQUAL"
}
]
拒絕清單
為啟用更多進階情境,Google 支援一種稱為「拒絕清單權杖」的否定形式。如果查詢將權杖加入拒絕清單,系統會排除含有拒絕清單權杖的任何資料點。如果查詢命名空間只有遭拒的權杖,則所有未明確遭拒的點都會相符,與空白命名空間與所有點相符的方式完全相同。
資料點也可以將權杖加入拒絕清單,排除指定該權杖的任何查詢。
舉例來說,請使用指定權杖定義下列資料點:
A: {} // empty set matches everything B: {red} // only a 'red' token C: {blue} // only a 'blue' token D: {orange} // only an 'orange' token E: {red, blue} // multiple tokens F: {red, !blue} // deny the 'blue' token G: {red, blue, !blue} // An unlikely edge-case H: {!blue} // deny-only (similar to empty-set)
系統的運作方式如下:
- 空白查詢命名空間是相符的萬用字元。舉例來說,Q:
{}
符合 DB:{color:red}
。 空白資料點命名空間並非萬用字元,舉例來說,Q:
{color:red}
不符合 DB:{}
。
在輸入資料中指定命名空間和權杖或值
如要瞭解如何整體建構輸入資料,請參閱「輸入資料格式和結構」。
以下分頁標籤說明如何指定與每個輸入向量相關聯的命名空間和權杖。
JSON
為每個向量記錄新增名為
restricts
的欄位,以包含物件陣列,每個物件都是命名空間。- 每個物件都必須有名為
namespace
的欄位。這個欄位是TokenNamespace.namespace
,也就是命名空間。 - 如果存在,欄位
allow
的值會是字串陣列。這個字串陣列就是TokenNamespace.string_tokens
清單。 - 如果存在,欄位
deny
的值會是字串陣列。這個字串陣列就是TokenNamespace.string_denylist_tokens
清單。
- 每個物件都必須有名為
以下是兩筆 JSON 格式的記錄範例:
[
{
"id": "42",
"embedding": [
0.5,
1
],
"restricts": [
{
"namespace": "class",
"allow": [
"cat",
"pet"
]
},
{
"namespace": "category",
"allow": [
"feline"
]
}
]
},
{
"id": "43",
"embedding": [
0.6,
1
],
"sparse_embedding": {
"values": [
0.1,
0.2
],
"dimensions": [
1,
4
]
},
"restricts": [
{
"namespace": "class",
"allow": [
"dog",
"pet"
]
},
{
"namespace": "category",
"allow": [
"canine"
]
}
]
}
]
為每個向量記錄新增名為
numeric_restricts
的欄位,其中包含物件陣列,每個物件都是數值限制。- 每個物件都必須有名為
namespace
的欄位。這個欄位是NumericRestrictNamespace.namespace
,也就是命名空間。 - 每個物件都必須包含
value_int
、value_float
和value_double
其中之一。 - 每個物件都不得有
op
欄位。這個欄位僅供查詢。
- 每個物件都必須有名為
以下是兩筆 JSON 格式的記錄範例:
[
{
"id": "42",
"embedding": [
0.5,
1
],
"numeric_restricts": [
{
"namespace": "size",
"value_int": 3
},
{
"namespace": "ratio",
"value_float": 0.1
}
]
},
{
"id": "43",
"embedding": [
0.6,
1
],
"sparse_embedding": {
"values": [
0.1,
0.2
],
"numeric_restricts": [
{
"namespace": "weight",
"value_double": 0.3
}
]
}
}
]
Avro
Avro 記錄使用下列結構定義:
{
"type": "record",
"name": "FeatureVector",
"fields": [
{
"name": "id",
"type": "string"
},
{
"name": "embedding",
"type": {
"type": "array",
"items": "float"
}
},
{
"name": "sparse_embedding",
"type": [
"null",
{
"type": "record",
"name": "sparse_embedding",
"fields": [
{
"name": "values",
"type": {
"type": "array",
"items": "float"
}
},
{
"name": "dimensions",
"type": {
"type": "array",
"items": "long"
}
}
]
}
]
},
{
"name": "restricts",
"type": [
"null",
{
"type": "array",
"items": {
"type": "record",
"name": "Restrict",
"fields": [
{
"name": "namespace",
"type": "string"
},
{
"name": "allow",
"type": [
"null",
{
"type": "array",
"items": "string"
}
]
},
{
"name": "deny",
"type": [
"null",
{
"type": "array",
"items": "string"
}
]
}
]
}
}
]
},
{
"name": "numeric_restricts",
"type": [
"null",
{
"type": "array",
"items": {
"name": "NumericRestrict",
"type": "record",
"fields": [
{
"name": "namespace",
"type": "string"
},
{
"name": "value_int",
"type": [ "null", "int" ],
"default": null
},
{
"name": "value_float",
"type": [ "null", "float" ],
"default": null
},
{
"name": "value_double",
"type": [ "null", "double" ],
"default": null
}
]
}
}
],
"default": null
},
{
"name": "crowding_tag",
"type": [
"null",
"string"
]
}
]
}
CSV
符記限制
針對每個向量記錄,新增以半形逗號分隔的格式配對
name=value
,指定權杖命名空間限制。如果命名空間中有多個值,則可能會重複使用相同名稱。舉例來說,
color=red,color=blue
代表以下TokenNamespace
:{ "namespace": "color" "string_tokens": ["red", "blue"] }
針對每個向量記錄,新增以半形逗號分隔的
name=!value
格式配對,指定權杖命名空間限制的排除值。舉例來說,
color=!red
代表以下TokenNamespace
:{ "namespace": "color" "string_blacklist_tokens": ["red"] }
數值限制
針對每個向量的記錄,請新增以半形逗號分隔的格式配對,並加上數字類型後置字元,指定數字命名空間限制。
#name=numericValue
數字類型後置字元為 int 的
i
、float 的f
,以及 double 的d
。同一名稱不應重複,因為每個命名空間應只關聯一個值。舉例來說,
#size=3i
代表以下NumericRestrictNamespace
:{ "namespace": "size" "value_int": 3 }
#ratio=0.1f
代表這項NumericRestrictNamespace
:{ "namespace": "ratio" "value_float": 0.1 }
#weight=0.3d
代表這項NumericRestriction
:{ "namespace": "weight" "value_double": 0.3 }
以下是資料點範例,其中包含
id: "6"
、embedding: [7, -8.1]
、sparse_embedding: {values: [0.1, -0.2, 0.5]
、dimensions: [40, 901, 1111]}}
、擁擠標記test
、權杖允許清單color: red, blue
、權杖拒絕清單color: purple
,以及含浮點數0.1
的數值限制ratio
:6,7,-8.1,40:0.1,901:-0.2,1111:0.5,crowding_tag=test,color=red,color=blue,color=!purple, ratio=0.1f
後續步驟
- 瞭解如何查詢索引,找出最近鄰。