Skip to content
Merged
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
5 changes: 2 additions & 3 deletions python/semantic_kernel/connectors/in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,9 +735,8 @@ def _get_filtered_records(self, options: VectorSearchOptions) -> dict[TKey, Attr
raise VectorStoreOperationException(f"Error evaluating filter: {e}") from e
filtered_records: dict[TKey, AttributeDict] = {}
for key, record in self.inner_storage.items():
for filter in callable_filters:
if self._run_filter(filter, record):
filtered_records[key] = record
if all(self._run_filter(filter, record) for filter in callable_filters):
filtered_records[key] = record
return filtered_records

def _parse_and_validate_filter(self, filter_str: str) -> Callable:
Expand Down
22 changes: 22 additions & 0 deletions python/tests/unit/connectors/memory/test_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ async def test_valid_lambda_filter_attribute_access(collection):
assert "2" in results


async def test_multiple_filters_are_combined_with_and(collection):
await collection.upsert([
{"id": "1", "content": "a", "vector": [1, 2, 3, 4, 5]},
{"id": "2", "content": "b", "vector": [1, 2, 3, 4, 5]},
{"id": "3", "content": "a", "vector": [1, 2, 3, 4, 5]},
])
filters = ["lambda x: x.content == 'a'", "lambda x: x.id != '3'"]
results = collection._get_filtered_records(type("opt", (), {"filter": filters})())
assert list(results) == ["1"]


async def test_multiple_callable_filters_are_combined_with_and(collection):
await collection.upsert([
{"id": "1", "content": "a", "vector": [1, 2, 3, 4, 5]},
{"id": "2", "content": "b", "vector": [1, 2, 3, 4, 5]},
{"id": "3", "content": "a", "vector": [1, 2, 3, 4, 5]},
])
filters = [lambda x: x.content == "a", lambda x: x.id != "3"]
results = collection._get_filtered_records(type("opt", (), {"filter": filters})())
assert list(results) == ["1"]


async def test_invalid_filter_not_lambda(collection):
with raises(VectorStoreOperationException, match="must be a lambda expression"):
collection._get_filtered_records(type("opt", (), {"filter": "x.id == '1'"})())
Expand Down
Loading