Skip to content

Commit 2f5f1fc

Browse files
authored
INTPYTHON-447 Ensure index is ready for retriever tests (#25)
1 parent 04e667b commit 2f5f1fc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

libs/mongodb/langchain_mongodb/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def _is_index_ready(collection: Collection, index_name: str) -> bool:
170170
bool : True if the index is present and READY false otherwise
171171
"""
172172
for index in collection.list_search_indexes(index_name):
173-
if index["type"] == "vectorSearch" and index["status"] == "READY":
173+
if index["status"] == "READY":
174174
return True
175175
return False
176176

libs/mongodb/tests/integration_tests/test_retrievers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from typing import Generator, List
3+
from time import sleep, time
34

45
import pytest
56
from langchain_core.documents import Document
@@ -10,7 +11,7 @@
1011
from langchain_mongodb import MongoDBAtlasVectorSearch
1112
from langchain_mongodb.index import (
1213
create_fulltext_search_index,
13-
create_vector_search_index,
14+
create_vector_search_index
1415
)
1516
from langchain_mongodb.retrievers import (
1617
MongoDBAtlasFullTextSearchRetriever,
@@ -160,6 +161,18 @@ def test_fulltext_retriever(
160161
search_field=PAGE_CONTENT_FIELD,
161162
)
162163

164+
# Wait for the search index to complete.
165+
search_content = dict(index=SEARCH_INDEX_NAME, wildcard=dict(query="*", path=PAGE_CONTENT_FIELD, allowAnalyzedField=True))
166+
n_docs = collection.count_documents({})
167+
t0 = time()
168+
while True:
169+
if (time() - t0) > TIMEOUT:
170+
raise TimeoutError(f'Search index {SEARCH_INDEX_NAME} did not complete in {TIMEOUT}')
171+
results = collection.aggregate([{ "$search": search_content }])
172+
if len(list(results)) == n_docs:
173+
break
174+
sleep(INTERVAL)
175+
163176
query = "When was the last time I visited new orleans?"
164177
results = retriever.invoke(query)
165178
assert "New Orleans" in results[0].page_content

0 commit comments

Comments
 (0)