Skip to content

Commit

Permalink
fix filter conditions with like/startswith/endswith (#2408)
Browse files Browse the repository at this point in the history
use `regex.search` instead of `regex.match` which is not
looking for the whole thing to match but it can match anywhere
in the value.

we're adding `.*` before and after the regex, but that doesn't work
with filters like `Foo|Bar|Baz` as those `.*` are only applied to the
begging and then `Bar` would only match if it's the only content
in the field.

SDESK-6755
  • Loading branch information
petrjasek authored Dec 9, 2022
1 parent 07ceff7 commit f1c0e72
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __init__(self, operator):
self.elastic_operator = "query_string"

def does_match(self, article_value, filter_value):
return filter_value.match(article_value) is None
return filter_value.search(article_value) is None

def contains_not(self):
return True
Expand Down Expand Up @@ -174,7 +174,7 @@ def __init__(self, operator):
self.elastic_operator = "query_string"

def does_match(self, article_value, filter_value):
return filter_value.match(article_value) is not None
return filter_value.search(article_value) is not None


class MatchOperator(FilterConditionOperator):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FilterConditionValue:
FilterConditionOperatorsEnum.startswith: "^{}",
FilterConditionOperatorsEnum.like: ".*{}.*",
FilterConditionOperatorsEnum.notlike: ".*{}.*",
FilterConditionOperatorsEnum.endswith: ".*{}",
FilterConditionOperatorsEnum.endswith: ".*{}$",
}

elastic_mapper = {
Expand Down
11 changes: 10 additions & 1 deletion tests/content_filters/filter_condition_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def test_get_mongo_value(self):
self.assertEqual(f.value.get_mongo_value(f.field), re.compile("^test", re.IGNORECASE))

f = FilterCondition("headline", "endswith", "test")
self.assertEqual(f.value.get_mongo_value(f.field), re.compile(".*test", re.IGNORECASE))
self.assertEqual(f.value.get_mongo_value(f.field), re.compile(".*test$", re.IGNORECASE))

def test_does_match_with_eq(self):
f = FilterCondition("urgency", "eq", "1")
Expand Down Expand Up @@ -558,6 +558,15 @@ def test_does_match_with_like_partial(self):
self.assertFalse(f.does_match(self.articles[4]))
self.assertFalse(f.does_match(self.articles[5]))

def test_does_match_with_like_complex(self):
f = FilterCondition("headline", "like", "Foo|Bar|Baz|Test Multiword")
self.assertTrue(f.does_match({"headline": "Bar"}))
self.assertTrue(f.does_match({"headline": "Bar Something"}))
self.assertTrue(f.does_match({"headline": "Something Bar"}))
self.assertTrue(f.does_match({"headline": "Something Baz"}))
self.assertFalse(f.does_match({"headline": "Something Test"}))
self.assertTrue(f.does_match({"headline": "Something Test Multiword"}))

def test_does_match_with_startswith_filter(self):
f = FilterCondition("headline", "startswith", "Sto")
self.assertTrue(f.does_match(self.articles[0]))
Expand Down

0 comments on commit f1c0e72

Please sign in to comment.