Skip to content

Commit

Permalink
Merge pull request #34019 from dimagi/ad/create-match-none-case-searc…
Browse files Browse the repository at this point in the history
…h-query

Create a `match-none` case search query function
  • Loading branch information
AddisonDunn committed Jan 24, 2024
2 parents b4f9ac3 + 0d05f73 commit 33229e9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
19 changes: 19 additions & 0 deletions corehq/apps/case_search/tests/test_case_search_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,22 @@ def test_match_all(self):
None,
['c1', 'c2', 'c3']
)

def test_match_none(self):
self._create_case_search_config()
cases = [
{'_id': 'c1', 'case_type': 'song', 'description': 'New York'},
{'_id': 'c2', 'case_type': 'song', 'description': 'Manchester'},
{'_id': 'c3', 'case_type': 'song', 'description': 'Manchester Boston'},
]
self._assert_query_runs_correctly(
self.domain,
cases,
get_case_search_query(
self.domain,
['song'],
{'_xpath_query': "match-none()"},
),
None,
[]
)
15 changes: 12 additions & 3 deletions corehq/apps/case_search/tests/test_filter_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ def test_date_comparison(self):
query = build_filter_from_ast(parsed, SearchFilterContext("domain"))
self.checkQuery(expected_filter, query, is_raw_query=True)


@freeze_time('2021-08-02')
def test_date_comparison__today(self):
parsed = parse_xpath("dob >= today()")
Expand Down Expand Up @@ -746,6 +745,14 @@ def test_match_all(self):
built_filter = build_filter_from_ast(parsed, SearchFilterContext("domain"))
self.checkQuery(built_filter, expected_filter, is_raw_query=True)

def test_match_none(self):
parsed = parse_xpath("match-none()")
expected_filter = {
"match_none": {}
}
built_filter = build_filter_from_ast(parsed, SearchFilterContext("domain"))
self.checkQuery(built_filter, expected_filter, is_raw_query=True)


@es_test(requires=[case_search_adapter], setup_class=True)
class TestFilterDslLookups(ElasticTestMixin, TestCase):
Expand Down Expand Up @@ -872,7 +879,8 @@ def test_parent_lookups(self):
}
built_filter = build_filter_from_ast(parsed, SearchFilterContext(self.domain))
self.checkQuery(built_filter, expected_filter, is_raw_query=True)
self.assertEqual([self.child_case1_id, self.child_case2_id], CaseSearchES().filter(built_filter).values_list('_id', flat=True))
self.assertEqual([self.child_case1_id, self.child_case2_id], CaseSearchES().filter(
built_filter).values_list('_id', flat=True))

def test_nested_parent_lookups(self):
parsed = parse_xpath("father/mother/house = 'Tyrell'")
Expand Down Expand Up @@ -909,7 +917,8 @@ def test_nested_parent_lookups(self):
}
built_filter = build_filter_from_ast(parsed, SearchFilterContext(self.domain))
self.checkQuery(built_filter, expected_filter, is_raw_query=True)
self.assertEqual([self.child_case1_id, self.child_case2_id], CaseSearchES().filter(built_filter).values_list('_id', flat=True))
self.assertEqual([self.child_case1_id, self.child_case2_id], CaseSearchES().filter(
built_filter).values_list('_id', flat=True))

def test_subcase_exists(self):
parsed = parse_xpath("subcase-exists('father', name='Margaery')")
Expand Down
6 changes: 4 additions & 2 deletions corehq/apps/case_search/xpath_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
within_distance,
phonetic_match,
starts_with,
match_all
match_all,
match_none
)
from .subcase_functions import subcase
from .ancestor_functions import ancestor_exists
Expand All @@ -33,5 +34,6 @@
'phonetic-match': phonetic_match,
'starts-with': starts_with,
'ancestor-exists': ancestor_exists,
'match-all': match_all
'match-all': match_all,
'match-none': match_none,
}
9 changes: 9 additions & 0 deletions corehq/apps/case_search/xpath_functions/query_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ def match_all(node, context):
serialize(node)
)
return filters.match_all()


def match_none(node, context):
if len(node.args):
raise XPathFunctionException(
_("'match-none()' does not take any arguments"),
serialize(node)
)
return filters.match_none()
4 changes: 4 additions & 0 deletions corehq/apps/es/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def match_all():
return {"match_all": {}}


def match_none():
return {"match_none": {}}


def prefix(field, value):
return {"prefix": {field: value}}

Expand Down

0 comments on commit 33229e9

Please sign in to comment.