我正在尝试使用 pymongo 执行矢量搜索,这是我的索引定义:
{
"fields": [
{
"numDimensions": 1536,
"path": "embeddings",
"similarity": "cosine",
"type": "vector"
},
{
"path": "company",
"type": "filter"
},
{
"path": "age",
"type": "filter"
}
]
}
这是我的Python代码
db.collection.aggregate([
{
"$vectorSearch": {
"index": "test_index",
"path": "embeddings",
"queryVector": embeddings,
"numCandidates": 100,
"limit": 5
}
}
])
当我运行此代码时,出现错误:
pymongo.errors.OperationFailure: PlanExecutor error during aggregation :: caused by :: embeddings is not indexed as knnVector
我尝试将索引定义更新为
"type": "knnVector"
,但我仍然遇到相同的错误,知道如何解决这个问题吗?
您现在可能已经明白了,但可能会引发错误,因为您的集合中没有名为
"embeddings"
的键。默认嵌入键名为“embedding”,如下图所示。
因此,要解决此错误,请尝试将
"embedding"
作为 "path"
值传递。
db.collection.aggregate([
{
"$vectorSearch": {
"index": "test_index",
"path": "embedding", # <--- this should not be "embeddings"
"queryVector": embeddings,
"numCandidates": 100,
"limit": 5
}
}
])
如果您无意中创建了
search
索引而不是 vectorSearch
索引,也可能会出现此错误。要解决这种情况下的问题,请创建一个 vectorSearch
索引。最简单的方法似乎是从用户界面。官方文档有this walkthrough;然而,我发现的最清晰的例子是here。