fix: upgrade MongoDB vector store from deprecated knnVector to GA vectorSearch (#3995)
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
kartik-mem0
Claude Opus 4.6
parent
5dabf24809
commit
7cebaba0a2
@@ -26,7 +26,7 @@ class OutputData(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class MongoDB(VectorStoreBase):
|
class MongoDB(VectorStoreBase):
|
||||||
VECTOR_TYPE = "knnVector"
|
VECTOR_TYPE = "vector"
|
||||||
SIMILARITY_METRIC = "cosine"
|
SIMILARITY_METRIC = "cosine"
|
||||||
|
|
||||||
def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str):
|
def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str):
|
||||||
@@ -69,17 +69,16 @@ class MongoDB(VectorStoreBase):
|
|||||||
else:
|
else:
|
||||||
search_index_model = SearchIndexModel(
|
search_index_model = SearchIndexModel(
|
||||||
name=self.index_name,
|
name=self.index_name,
|
||||||
|
type="vectorSearch",
|
||||||
definition={
|
definition={
|
||||||
"mappings": {
|
"fields": [
|
||||||
"dynamic": False,
|
{
|
||||||
"fields": {
|
"type": self.VECTOR_TYPE,
|
||||||
"embedding": {
|
"path": "embedding",
|
||||||
"type": self.VECTOR_TYPE,
|
"numDimensions": self.embedding_model_dims,
|
||||||
"dimensions": self.embedding_model_dims,
|
"similarity": self.SIMILARITY_METRIC,
|
||||||
"similarity": self.SIMILARITY_METRIC,
|
}
|
||||||
}
|
]
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
collection.create_search_index(search_index_model)
|
collection.create_search_index(search_index_model)
|
||||||
@@ -141,7 +140,7 @@ class MongoDB(VectorStoreBase):
|
|||||||
"$vectorSearch": {
|
"$vectorSearch": {
|
||||||
"index": self.index_name,
|
"index": self.index_name,
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
"numCandidates": limit,
|
"numCandidates": min(limit * 20, 10000),
|
||||||
"queryVector": vectors,
|
"queryVector": vectors,
|
||||||
"path": "embedding",
|
"path": "embedding",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,17 +48,16 @@ def test_initalize_create_col(mongo_vector_fixture):
|
|||||||
search_index_model = args[0].document
|
search_index_model = args[0].document
|
||||||
assert search_index_model == {
|
assert search_index_model == {
|
||||||
"name": "test_collection_vector_index",
|
"name": "test_collection_vector_index",
|
||||||
|
"type": "vectorSearch",
|
||||||
"definition": {
|
"definition": {
|
||||||
"mappings": {
|
"fields": [
|
||||||
"dynamic": False,
|
{
|
||||||
"fields": {
|
"type": "vector",
|
||||||
"embedding": {
|
"path": "embedding",
|
||||||
"type": "knnVector",
|
"numDimensions": 1536,
|
||||||
"dimensions": 1536,
|
"similarity": "cosine",
|
||||||
"similarity": "cosine",
|
}
|
||||||
}
|
]
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
assert mongo_vector.collection == mock_collection
|
assert mongo_vector.collection == mock_collection
|
||||||
@@ -95,7 +94,7 @@ def test_search(mongo_vector_fixture):
|
|||||||
"$vectorSearch": {
|
"$vectorSearch": {
|
||||||
"index": "test_collection_vector_index",
|
"index": "test_collection_vector_index",
|
||||||
"limit": 2,
|
"limit": 2,
|
||||||
"numCandidates": 2,
|
"numCandidates": 40,
|
||||||
"queryVector": query_vector,
|
"queryVector": query_vector,
|
||||||
"path": "embedding",
|
"path": "embedding",
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user