Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: Escapes AzureCosmosDBNoSqlVectorStore hybrid & full text queries that contain single quotes #29339

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,20 @@ def _construct_query(
raise ValueError(
"search text cannot be None for FULL_TEXT_RANK queries."
)

search_text = search_text.replace("'", "\\'")
terms = [f"'{term}'" for term in search_text.split()]
query += f""" ORDER BY RANK FullTextScore(c.{self._text_key},
[{", ".join(f"'{term}'" for term in search_text.split())}])"""
[{", ".join(terms)}])"""
elif query_type == CosmosDBQueryType.VECTOR:
query += " ORDER BY VectorDistance(c[@embeddingKey], @embeddings)"
elif query_type == CosmosDBQueryType.HYBRID:
if search_text is None:
raise ValueError("search text cannot be None for HYBRID queries.")
search_text = search_text.replace("'", "\\'")
terms = [f"'{term}'" for term in search_text.split()]
query += f""" ORDER BY RANK RRF(FullTextScore(c.{self._text_key},
[{", ".join(f"'{term}'" for term in search_text.split())}]),
[{", ".join(terms)}]),
VectorDistance(c.{self._embedding_key}, {embeddings}))"""
else:
query += ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ def test_from_documents_full_text_and_hybrid(
assert len(output) == 5
assert "Standard Poodles" in output[0].page_content

# Full text search successfully queries for data with a single quote
output = store.similarity_search(
"'Retrievers'", k=5, query_type=CosmosDBQueryType.FULL_TEXT_RANK
)

assert output
assert len(output) == 5
assert "Retrievers" in output[0].page_content

# Full text search BM25 ranking with filtering
pre_filter = PreFilter(
conditions=[
Expand All @@ -321,6 +330,15 @@ def test_from_documents_full_text_and_hybrid(
assert len(output) == 5
assert "Border Collies" in output[0].page_content

# Hybrid search successfully queries for data with a single quote
output = store.similarity_search(
"'outdoor activities'", k=5, query_type=CosmosDBQueryType.HYBRID
)

assert output
assert len(output) == 5
assert "Border Collies" in output[0].page_content

# Hybrid search RRF ranking with filtering
pre_filter = PreFilter(
conditions=[
Expand Down
Loading