我正在 mongodb 中构建聚合管道,但遇到了一些意外的行为。
管道如下:
[{
"$search":{
"index":"vector_index",
"knnBeta":{
"vector":[
-0.30345699191093445,
0.6833441853523254,
1.2565147876739502,
-0.6364057064056396
],
"path":"embedding",
"k":10,
"filter":{
"compound":{
"filter":[
{
"text":{
"path":"my.field.name",
"query":[
"value1",
"value2",
"value3",
"value4"
]
},
{
"text":{
"path":"my.field.name2",
"query":"something_else",
}
}
]
}
}
}
}
},
{
"$project":{
"score":{
"$meta":"searchScore"
},
"embedding":0
}
}
]
管道(应该)根据(向量索引,嵌入,向量)进行向量搜索(看起来工作正常。使用过滤器,特别是过滤器应该将向量搜索限制为
my.field.name
等于 value1
或value2
或 ...
和 my.field.name2
等于 something_else
。
相反,只有第二个过滤器起作用,或者至少看起来如此(第二个过滤器的值是单个字母)。
我也尝试使用
must
子句代替 compound
子句中的过滤器,但结果保持不变。
我还尝试删除第二个过滤(没有列表的过滤),但我仍然得到未过滤的结果。
我做错了什么吗?怎么才能正确呢?
compound
语法 为:
{
$search: {
"index": <index name>, // optional, defaults to "default"
"compound": {
<must | mustNot | should | filter>: [ { <clauses> } ],
"score": <options>
}
}
}
filter
阶段中的compound
字段应该是过滤器阶段的数组,但在您的情况下,它直接包含一个文本过滤器和一个字段。
要使用多个过滤器,您需要使用
must
子句,它是所有必须匹配的过滤器阶段的数组。
{
"$search": {
"index": "vector_index",
"knnBeta": {
"vector": [
-0.30345699191093445,
0.6833441853523254,
1.2565147876739502,
-0.6364057064056396
],
"path": "embedding",
"k": 10,
"filter": {
"compound": {
"must": [
{
"text": {
"path": "my.field.name",
"query": [
"value1",
"value2",
"value3",
"value4"
]
}
},
{
"text": {
"path": "my.field.name2",
"query": "something_else"
}
}
]
}
}
}
}
},
{
"$project": {
"score": {
"$meta": "searchScore"
},
"embedding": 0
}
}
每个单独的过滤器都是
must
数组中的一个单独文档,指定路径以及该路径的查询条件。