Skip to content

Support for jsonb array comparision for $in $nin operator #59

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

Open
wants to merge 1 commit into
base: main
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
10 changes: 8 additions & 2 deletions langchain_postgres/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,15 @@ def _handle_field_filter(
queried_field = self.EmbeddingStore.cmetadata[field].astext

if operator in {"$in"}:
return queried_field.in_([str(val) for val in filter_value])
return func.jsonb_exists_any(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will result in a bug when working with integer fields which is a more common use case.

Adding a unit-test in a separate PR and will rebase.

self.EmbeddingStore.cmetadata[field],
filter_value
)
elif operator in {"$nin"}:
return ~queried_field.in_([str(val) for val in filter_value])
return ~func.jsonb_exists_any(
self.EmbeddingStore.cmetadata[field],
filter_value
)
elif operator in {"$like"}:
return queried_field.like(filter_value)
elif operator in {"$ilike"}:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit_tests/fixtures/filtering_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,31 @@
{"name": {"$in": ["adam", "bob"]}},
[1, 2],
),
(
{"tags": {"$in": ["c", "d"]}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jernkuan would you mind checking if this works against

"location": [1.0, 2.0],

field as well?

[2, 3],
),
(
{"tags": {"$in": ["b", "d"]}},
[1, 2, 3],
),
# Test nin
(
{"name": {"$nin": ["adam", "bob"]}},
[3],
),
(
{"tags": {"$nin": ["c", "d"]}},
[1],
),
(
{"tags": {"$nin": ["d"]}},
[1, 2],
),
(
{"tags": {"$nin": ["e", "f"]}},
[1, 2, 3],
),
]

TYPE_5_FILTERING_TEST_CASES = [
Expand Down
Loading