Skip to content

Commit

Permalink
Escapes AzureCosmosDBNoSqlVectorStore hybrid & full text queries that…
Browse files Browse the repository at this point in the history
… contain single quotes
  • Loading branch information
BenMcH committed Jan 22, 2025
1 parent 3f1d209 commit e2db28a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,17 @@ def _construct_query(
raise ValueError(
"search text cannot be None for FULL_TEXT_RANK queries."
)
terms = [f"'{term.replace("'", "\\'")}'" 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.")
terms = [f"'{term.replace("'", "\\'")}'" 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

0 comments on commit e2db28a

Please sign in to comment.