Skip to content
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

fix filter conditions with like/startswith/endswith #2408

Merged
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
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