Skip to content

Commit

Permalink
Fix nested logic operators
Browse files Browse the repository at this point in the history
* Normalize logical filter conditions
  • Loading branch information
ajtran authored Jul 22, 2024
1 parent a30cb96 commit 2ed4be7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _parse_logical_condition(condition: Dict[str, Any]) -> Dict[str, Any]:
raise FilterError(msg)

operator = condition["operator"]
conditions = [_parse_comparison_condition(c) for c in condition["conditions"]]
conditions = [_normalize_filters(c) for c in condition["conditions"]]
if len(conditions) > 1:
conditions = _normalize_ranges(conditions)
if operator not in OPERATORS:
Expand Down
28 changes: 28 additions & 0 deletions integrations/astra/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,34 @@ def test_delete_documents_more_than_twenty_delete_ids(self, document_store: Astr
# No Document has been deleted
assert document_store.count_documents() == 0

def test_filter_documents_nested_filters(self, document_store, filterable_docs):
filter_criteria = {
"operator": "AND",
"conditions": [
{"field": "meta.page", "operator": "==", "value": "100"},
{
"operator": "OR",
"conditions": [
{"field": "meta.chapter", "operator": "==", "value": "abstract"},
{"field": "meta.chapter", "operator": "==", "value": "intro"},
],
},
],
}

document_store.write_documents(filterable_docs)
result = document_store.filter_documents(filters=filter_criteria)

self.assert_documents_are_equal(
result,
[
d
for d in filterable_docs
if d.meta.get("page") == "100"
and (d.meta.get("chapter") == "abstract" or d.meta.get("chapter") == "intro")
],
)

@pytest.mark.skip(reason="Unsupported filter operator not.")
def test_not_operator(self, document_store, filterable_docs):
pass
Expand Down

0 comments on commit 2ed4be7

Please sign in to comment.