Skip to content

Commit 12bcc85

Browse files
Anash3ccurme
andauthored
added operator filter for supabase (#29475)
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 <[email protected]>
1 parent 585f467 commit 12bcc85

File tree

1 file changed

+16
-0
lines changed
  • libs/community/langchain_community/vectorstores

1 file changed

+16
-0
lines changed

libs/community/langchain_community/vectorstores/supabase.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ def similarity_search_by_vector_with_relevance_scores(
226226
postgrest_filter: Optional[str] = None,
227227
score_threshold: Optional[float] = None,
228228
) -> List[Tuple[Document, float]]:
229+
# Convert MongoDB-style filter to PostgreSQL syntax if needed
230+
if filter:
231+
for key, value in filter.items():
232+
if isinstance(value, dict) and "$in" in value:
233+
# Extract the list of values for the $in operator
234+
in_values = value["$in"]
235+
# Create a PostgreSQL IN clause
236+
values_str = ",".join(f"'{str(v)}'" for v in in_values)
237+
new_filter = f"metadata->>{key} IN ({values_str})"
238+
239+
# Combine with existing postgrest_filter if present
240+
if postgrest_filter:
241+
postgrest_filter = f"({postgrest_filter}) and ({new_filter})"
242+
else:
243+
postgrest_filter = new_filter
244+
229245
match_documents_params = self.match_args(query, filter)
230246
query_builder = self._client.rpc(self.query_name, match_documents_params)
231247

0 commit comments

Comments
 (0)