Skip to content

Commit 0026fd1

Browse files
authored
Python: Combine multiple InMemoryCollection filters with AND (#14540)
### Motivation and Context `VectorSearchOptions` documents that multiple filters are combined with AND. `InMemoryCollection._get_filtered_records` combined them with OR, so a record was kept as soon as one filter matched. Fixes #14538 ### Description Keep a record only when `all(...)` of the filters match. Single filters behave as before. Added two tests (string filters and callable filters). Both fail on main and pass with the change. Checked locally: `tests/unit/connectors/memory/test_in_memory.py` and `tests/unit/data` pass, ruff check and format are clean, mypy is clean on `in_memory.py`. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 50156e3 commit 0026fd1

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎python/semantic_kernel/connectors/in_memory.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,8 @@ def _get_filtered_records(self, options: VectorSearchOptions) -> dict[TKey, Attr
771771
raise VectorStoreOperationException(f"Error evaluating filter: {e}") from e
772772
filtered_records: dict[TKey, AttributeDict] = {}
773773
for key, record in self.inner_storage.items():
774-
for filter in callable_filters:
775-
if self._run_filter(filter, record):
776-
filtered_records[key] = record
774+
if all(self._run_filter(filter, record) for filter in callable_filters):
775+
filtered_records[key] = record
777776
return filtered_records
778777

779778
def _parse_and_validate_filter(self, filter_str: str) -> Callable:

‎python/tests/unit/connectors/memory/test_in_memory.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ async def test_valid_lambda_filter_attribute_access(collection):
327327
assert "2" in results
328328

329329

330+
async def test_multiple_filters_are_combined_with_and(collection):
331+
await collection.upsert([
332+
{"id": "1", "content": "a", "vector": [1, 2, 3, 4, 5]},
333+
{"id": "2", "content": "b", "vector": [1, 2, 3, 4, 5]},
334+
{"id": "3", "content": "a", "vector": [1, 2, 3, 4, 5]},
335+
])
336+
filters = ["lambda x: x.content == 'a'", "lambda x: x.id != '3'"]
337+
results = collection._get_filtered_records(type("opt", (), {"filter": filters})())
338+
assert list(results) == ["1"]
339+
340+
341+
async def test_multiple_callable_filters_are_combined_with_and(collection):
342+
await collection.upsert([
343+
{"id": "1", "content": "a", "vector": [1, 2, 3, 4, 5]},
344+
{"id": "2", "content": "b", "vector": [1, 2, 3, 4, 5]},
345+
{"id": "3", "content": "a", "vector": [1, 2, 3, 4, 5]},
346+
])
347+
filters = [lambda x: x.content == "a", lambda x: x.id != "3"]
348+
results = collection._get_filtered_records(type("opt", (), {"filter": filters})())
349+
assert list(results) == ["1"]
350+
351+
330352
async def test_invalid_filter_not_lambda(collection):
331353
with raises(VectorStoreOperationException, match="must be a lambda expression"):
332354
collection._get_filtered_records(type("opt", (), {"filter": "x.id == '1'"})())

0 commit comments

Comments
 (0)