Skip to content

Commit

Permalink
Merge branch 'master' into ad/allow-LRUs-to-view-location-safe-tab-re…
Browse files Browse the repository at this point in the history
…ports
  • Loading branch information
AddisonDunn authored Aug 7, 2024
2 parents 2f7eb1e + ba26690 commit a35579b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 35 deletions.
1 change: 1 addition & 0 deletions corehq/apps/case_search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class Meta(object):
sync_cases_on_form_entry = models.BooleanField(blank=False, null=False, default=False)
fuzzy_properties = models.ManyToManyField(FuzzyProperties)
ignore_patterns = models.ManyToManyField(IgnorePatterns)
# Deprecated - this will be removed in a later PR
fuzzy_prefix_length = models.SmallIntegerField(blank=True, null=True, validators=[
MinValueValidator(0), MaxValueValidator(10),
])
Expand Down
11 changes: 2 additions & 9 deletions corehq/apps/case_search/tests/test_filter_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,9 @@ def test_fuzzy_match(self):
case_property_query("name", "jimmy", fuzzy=True)
)

def test_fuzzy_match_with_prefix(self):
self._test_xpath_query(
"fuzzy-match(name, 'jimmy')",
case_property_query("name", "jimmy", fuzzy=True, fuzzy_prefix_length=2),
config=CaseSearchConfig(domain="domain", fuzzy_prefix_length=2),
)

def _test_xpath_query(self, query_string, expected_filter, config=None):
def _test_xpath_query(self, query_string, expected_filter):
helper = QueryHelper("domain")
helper.config = config or CaseSearchConfig(domain="domain")
helper.config = CaseSearchConfig(domain="domain")
context = SearchFilterContext("domain", helper=helper)
parsed = parse_xpath(query_string)
built_filter = build_filter_from_ast(parsed, context)
Expand Down
3 changes: 1 addition & 2 deletions corehq/apps/case_search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,7 @@ def _get_query(self, criteria):
elif criteria.is_index_query:
return reverse_index_case_query(value, criteria.index_query_identifier)
else:
return case_property_query(criteria.key, value, fuzzy=fuzzy,
fuzzy_prefix_length=self.config.fuzzy_prefix_length)
return case_property_query(criteria.key, value, fuzzy=fuzzy)

def _remove_ignored_patterns(self, case_property, value):
for to_remove in self._patterns_to_remove[case_property]:
Expand Down
3 changes: 1 addition & 2 deletions corehq/apps/case_search/xpath_functions/query_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def fuzzy_match(node, context):
property_name = _property_name_to_string(node.args[0], node)
value = unwrap_value(node.args[1], context)

return case_property_query(property_name, value, fuzzy=True,
fuzzy_prefix_length=context.helper.config.fuzzy_prefix_length)
return case_property_query(property_name, value, fuzzy=True)


def _property_name_to_string(value, node):
Expand Down
8 changes: 2 additions & 6 deletions corehq/apps/es/case_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ def multiplex_to_adapter(domain):
return None


def case_property_query(case_property_name, value, fuzzy=False, multivalue_mode=None,
fuzzy_prefix_length=None):
def case_property_query(case_property_name, value, fuzzy=False, multivalue_mode=None):
"""
Search for all cases where case property with name `case_property_name`` has text value `value`
"""
Expand All @@ -245,15 +244,12 @@ def case_property_query(case_property_name, value, fuzzy=False, multivalue_mode=
if value == '':
return case_property_missing(case_property_name)
if fuzzy:
kwargs = {'fuzziness': 'AUTO'}
if fuzzy_prefix_length:
kwargs['prefix_length'] = fuzzy_prefix_length
return _base_property_query(
case_property_name,
filters.OR(
# fuzzy match. This portion of this query OR's together multi-word case
# property values and doesn't respect multivalue_mode
queries.fuzzy(value, PROPERTY_VALUE, **kwargs),
queries.fuzzy(value, PROPERTY_VALUE, fuzziness='AUTO', prefix_length=2),
# non-fuzzy match. added to improve the score of exact matches
queries.match(value, PROPERTY_VALUE, operator=multivalue_mode)
),
Expand Down
21 changes: 5 additions & 16 deletions corehq/apps/es/tests/test_case_search_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_multiple_case_search_queries(self):
"case_properties.value": {
"value": "polly",
"fuzziness": "AUTO",
"prefix_length": 2,
"max_expansions": 100
}
}
Expand Down Expand Up @@ -219,7 +220,7 @@ def test_blacklisted_owner_ids(self):

def test_fuzzy_property_query(self):
query = self.es.domain('swashbucklers').filter(
case_property_query("foo", "backbeard", fuzzy=True, fuzzy_prefix_length=2)
case_property_query("foo", "backbeard", fuzzy=True)
)
expected = {
"query": {
Expand Down Expand Up @@ -419,28 +420,16 @@ def test_simple_case_property_query(self):
)

def test_fuzzy_case_property_query(self):
self._assert_query_runs_correctly(
self.domain,
[
{'_id': 'c1', 'foo': 'redbeard'},
{'_id': 'c2', 'foo': 'blackbeard'},
],
CaseSearchES().domain(self.domain).case_property_query("foo", "backbeard", fuzzy=True),
None,
['c2']
)

def test_fuzzy_case_property_query_with_prefix(self):
self._assert_query_runs_correctly(
self.domain,
[
{'_id': 'c1', 'foo': 'redbeard'},
{'_id': 'c2', 'foo': 'blackbeard'},
{'_id': 'c3', 'foo': 'backbird'},
],
CaseSearchES().domain(self.domain).filter(
case_property_query("foo", "backbeard", fuzzy=True, fuzzy_prefix_length=2)
),
# 'backbird' is a fuzzy match - off by two edits
# 'blackbeard' is even closer, but it is omitted because of prefix_length=2
CaseSearchES().domain(self.domain).case_property_query("foo", "backbeard", fuzzy=True),
None,
['c3']
)
Expand Down

0 comments on commit a35579b

Please sign in to comment.