Skip to content

Commit

Permalink
added operator filter for supabase (#29475)
Browse files Browse the repository at this point in the history
Description
This PR adds support for MongoDB-style $in operator filtering in the
Supabase vectorstore implementation. Currently, filtering with $in
operators returns no results, even when matching documents exist. This
change properly translates MongoDB-style filters to PostgreSQL syntax,
enabling efficient multi-document filtering.
Changes

Modified similarity_search_by_vector_with_relevance_scores to handle
MongoDB-style $in operators
Added automatic conversion of $in filters to PostgreSQL IN clauses
Preserved original vector type handling and numpy array conversion
Maintained compatibility with existing postgrest filters
Added support for the same filtering in
similarity_search_by_vector_returning_embeddings

Issue
Closes #27932

Implementation Notes
No changes to public API or function signatures
Backwards compatible - behavior unchanged for non-$in filters
More efficient than multiple individual queries for multi-ID searches
Preserves all existing functionality including numpy array conversion
for vector types

Dependencies
None

Additional Notes
The implementation handles proper SQL escaping for filter values
Maintains consistent behavior with other vectorstore implementations
that support MongoDB-style operators
Future extensions could support additional MongoDB-style operators ($gt,
$lt, etc.)

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
  • Loading branch information
Anash3 and ccurme authored Jan 29, 2025
1 parent 585f467 commit 12bcc85
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libs/community/langchain_community/vectorstores/supabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ def similarity_search_by_vector_with_relevance_scores(
postgrest_filter: Optional[str] = None,
score_threshold: Optional[float] = None,
) -> List[Tuple[Document, float]]:
# Convert MongoDB-style filter to PostgreSQL syntax if needed
if filter:
for key, value in filter.items():
if isinstance(value, dict) and "$in" in value:
# Extract the list of values for the $in operator
in_values = value["$in"]
# Create a PostgreSQL IN clause
values_str = ",".join(f"'{str(v)}'" for v in in_values)
new_filter = f"metadata->>{key} IN ({values_str})"

# Combine with existing postgrest_filter if present
if postgrest_filter:
postgrest_filter = f"({postgrest_filter}) and ({new_filter})"
else:
postgrest_filter = new_filter

match_documents_params = self.match_args(query, filter)
query_builder = self._client.rpc(self.query_name, match_documents_params)

Expand Down

0 comments on commit 12bcc85

Please sign in to comment.