def setUpClass(cls): super(BaseLinkedCaseClaimTest, cls).setUpClass() cls.search_config = CaseSearchConfig(domain=cls.domain, enabled=True) cls.search_config.save() fuzzy_property = FuzzyProperties(domain=cls.domain, case_type='case', properties=['a', 'b', 'c']) fuzzy_property.save() ignore_patterns = IgnorePatterns(domain=cls.domain, case_type='case', case_property='a', regex='+') ignore_patterns.save() cls.search_config.fuzzy_properties.add(fuzzy_property) cls.search_config.ignore_patterns.add(ignore_patterns)
def test_add_ignore_pattern_queries(self): rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' word', ) # remove ' word' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' gone', ) # remove ' gone' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='special_id', regex='-', ) # remove '-' from the special id case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='phone_number', regex='+', ) # remove '+' from the phone_number case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() criteria = OrderedDict([ ('phone_number', '+91999'), ('special_id', 'abc-123-546'), ('name', "this word should be gone"), ('other_name', "this word should not be gone"), ]) expected = { "query": { "filtered": { "filter": { "and": [{ "term": { "domain.exact": "swashbucklers" } }, { "term": { "type.exact": "case_type" } }, { "term": { "closed": False } }, { "match_all": {} }] }, "query": { "bool": { "must": [{ "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ({ "term": { "case_properties.key.exact": "phone_number" } }, { "term": { "case_properties.value.exact": "91999" } }), }, "query": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ({ "term": { "case_properties.key.exact": "special_id" } }, { "term": { "case_properties.value.exact": "abc123546" } }) }, "query": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ({ "term": { "case_properties.key.exact": "name" } }, { "term": { "case_properties.value.exact": "this should be" } }) }, "query": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ({ "term": { "case_properties.key.exact": "other_name" } }, { "term": { "case_properties.value.exact": ("this word should not be gone" ) } }) }, "query": { "match_all": {} } } } } }] } } } }, "size": CASE_SEARCH_MAX_RESULTS, } self.checkQuery( CaseSearchCriteria(DOMAIN, 'case_type', criteria).search_es, expected, )
def test_custom_values(self): config, created = CaseSearchConfig.objects.get_or_create(pk='domain', enabled=True) rc = IgnorePatterns( domain='domain', case_type='case_type', case_property='thing_1', regex=' removed', ) rc.save() config.ignore_patterns.add(rc) initial_custom_query = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop removed", "name": "Jon Snow", } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop" } } } } } } } self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, config.ignore_patterns.filter(domain='domain', case_type='case_type')))
def test_add_ignore_pattern_queries(self): rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' word', ) # remove ' word' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' gone', ) # remove ' gone' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='special_id', regex='-', ) # remove '-' from the special id case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='phone_number', regex='+', ) # remove '+' from the phone_number case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() criteria = OrderedDict([ ('phone_number', '+91999'), ('special_id', 'abc-123-546'), ('name', "this word should be gone"), ('other_name', "this word should not be gone"), ]) expected = { "query": { "bool": { "filter": [{ 'terms': { 'domain.exact': [DOMAIN] } }, { "terms": { "type.exact": ["case_type"] } }, { "term": { "closed": False } }, { "match_all": {} }], "must": { "bool": { "must": [{ "nested": { "path": "case_properties", "query": { "bool": { "filter": [{ "bool": { "filter": [{ "term": { "case_properties.key.exact": "phone_number" } }, { "term": { "case_properties.value.exact": "91999" } }] } }], "must": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "bool": { "filter": [{ "bool": { "filter": [{ "term": { "case_properties.key.exact": "special_id" } }, { "term": { "case_properties.value.exact": "abc123546" } }] } }], "must": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "bool": { "filter": [{ "bool": { "filter": [{ "term": { "case_properties.key.exact": "name" } }, { "term": { "case_properties.value.exact": "this should be" } }] } }], "must": { "match_all": {} } } } } }, { "nested": { "path": "case_properties", "query": { "bool": { "filter": [{ "bool": { "filter": [{ "term": { "case_properties.key.exact": "other_name" } }, { "term": { "case_properties.value.exact": "this word should not be gone" } }] } }], "must": { "match_all": {} } } } } }] } } } }, "sort": ["_score", "_doc"], "size": CASE_SEARCH_MAX_RESULTS } self.checkQuery(get_case_search_query(DOMAIN, ['case_type'], criteria), expected, validate_query=False)
def test_custom_values(self): config, created = CaseSearchConfig.objects.get_or_create(pk='domain', enabled=True) rc = IgnorePatterns( domain='domain', case_type='case_type', case_property='thing_1', regex=' removed', ) rc.save() config.ignore_patterns.add(rc) initial_custom_query = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop removed", "name": "Jon Snow", } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop" } } } } } } } self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, config.ignore_patterns.filter(domain='domain', case_type='case_type') ) )
def test_add_ignore_pattern_queries(self): rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' word', ) # remove ' word' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='name', regex=' gone', ) # remove ' gone' from the name case property rc.save() self.config.ignore_patterns.add(rc) rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='special_id', regex='-', ) # remove '-' from the special id case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() rc = IgnorePatterns( domain=DOMAIN, case_type='case_type', case_property='phone_number', regex='+', ) # remove '+' from the phone_number case property rc.save() self.config.ignore_patterns.add(rc) self.config.save() criteria = OrderedDict([ ('phone_number', '+91999'), ('special_id', 'abc-123-546'), ('name', "this word should be gone"), ('other_name', "this word should not be gone"), ]) expected = {"query": { "filtered": { "filter": { "and": [ { "term": { "domain.exact": "swashbucklers" } }, { "term": { "type.exact": "case_type" } }, { "term": { "closed": False } }, { "match_all": {} } ] }, "query": { "bool": { "must": [ { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ( { "term": { "case_properties.key.exact": "phone_number" } }, { "term": { "case_properties.value.exact": "91999" } } ), }, "query": { "match_all": { } } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ( { "term": { "case_properties.key.exact": "special_id" } }, { "term": { "case_properties.value.exact": "abc123546" } } ) }, "query": { "match_all": { } } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ( { "term": { "case_properties.key.exact": "name" } }, { "term": { "case_properties.value.exact": "this should be" } } ) }, "query": { "match_all": { } } } } } }, { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "and": ( { "term": { "case_properties.key.exact": "other_name" } }, { "term": { "case_properties.value.exact": ( "this word should not be gone" ) } } ) }, "query": { "match_all": { } } } } } } ] } } } }, "size": CASE_SEARCH_MAX_RESULTS, } self.checkQuery( CaseSearchCriteria(DOMAIN, 'case_type', criteria).search_es, expected, )