def _apply_filter(self, search_es, criteria): if criteria.key == CASE_SEARCH_XPATH_QUERY_KEY: if not criteria.is_empty: if criteria.has_multiple_terms: for value in criteria.value: search_es = search_es.filter( build_filter_from_xpath(self.query_domains, value)) return search_es else: return search_es.filter( build_filter_from_xpath(self.query_domains, criteria.value)) elif criteria.key == 'owner_id': if not criteria.is_empty: return search_es.filter(case_search.owner(criteria.value)) elif criteria.key == CASE_SEARCH_BLACKLISTED_OWNER_ID_KEY: if not criteria.is_empty: return search_es.filter( case_search.blacklist_owner_id(criteria.value_as_list)) elif criteria.key == COMMCARE_PROJECT: if not criteria.is_empty: return search_es.filter(filters.domain(criteria.value)) elif criteria.key not in UNSEARCHABLE_KEYS: return search_es.add_query(self._get_case_property_query(criteria), queries.MUST) return search_es
def _get_case_property_or_missing_query(self, criteria): if criteria.is_empty: return case_property_missing(criteria.key) if criteria.is_ancestor_query: missing_filter = build_filter_from_xpath(self.query_domains, f'{criteria.key} = ""') else: missing_filter = case_property_missing(criteria.key) return filters.OR(self._get_query(criteria), missing_filter)
def xpath_query(self, domain, xpath): """Search for cases using an XPath predicate expression. Enter an arbitrary XPath predicate in the context of the case. Also supports related case lookups. e.g you can do things like: - case properties: "first_name = 'dolores' and last_name = 'abernathy'" - date ranges: "first_came_online >= '2017-08-12' or died <= '2020-11-15" - numeric ranges: "age >= 100 and height < 1.25" - related cases: "mother/first_name = 'maeve' or parent/parent/host/age = 13" """ from corehq.apps.case_search.filter_dsl import build_filter_from_xpath return self.filter(build_filter_from_xpath(domain, xpath))
def _get_query(self, criteria): if criteria.is_daterange: return self._get_daterange_query(criteria) value = self._remove_ignored_patterns(criteria.key, criteria.value) fuzzy = criteria.key in self._fuzzy_properties if criteria.is_ancestor_query: query = f'{criteria.key} = "{value}"' return build_filter_from_xpath(self.query_domains, query, fuzzy=fuzzy) else: return case_property_query(criteria.key, value, fuzzy=fuzzy)
def xpath_query(self, domain, xpath): """Search for cases using an XPath predicate expression. Enter an arbitrary XPath predicate in the context of the case. Also supports related case lookups. e.g you can do things like: - case properties: "first_name = 'dolores' and last_name = 'abernathy'" - date ranges: "first_came_online >= '2017-08-12' or died <= '2020-11-15" - numeric ranges: "age >= 100 and height < 1.25" - related cases: "mother/first_name = 'maeve' or parent/parent/host/age = 13" """ from corehq.apps.case_search.filter_dsl import build_filter_from_xpath return self.filter(build_filter_from_xpath(domain, xpath))
def _get_xpath_filter(domain, xpath): try: return build_filter_from_xpath(domain, xpath) except CaseFilterError as e: raise UserError(f'Bad XPath: {e}')