def post(self, request, *args, **kwargs): from corehq.apps.es.case_search import CaseSearchES if not case_search_enabled_for_domain(self.domain): raise BadRequest("Domain does not have case search enabled") query = json.loads(request.POST.get('q')) case_type = query.get('type') owner_id = query.get('owner_id') search_params = query.get('parameters', []) xpath = query.get("xpath") search = CaseSearchES() search = search.domain(self.domain).size(10) if case_type: search = search.case_type(case_type) if owner_id: search = search.owner(owner_id) for param in search_params: value = re.sub(param.get('regex', ''), '', param.get('value')) if '/' in param.get('key'): query = '{} = "{}"'.format(param.get('key'), value) search = search.xpath_query(self.domain, query, fuzzy=param.get('fuzzy')) else: search = search.case_property_query( param.get('key'), value, clause=param.get('clause'), fuzzy=param.get('fuzzy'), ) if xpath: search = search.xpath_query(self.domain, xpath) include_profile = request.POST.get("include_profile", False) if include_profile: search = search.enable_profiling() search_results = search.run() return json_response({ 'values': search_results.raw_hits, 'count': search_results.total, 'took': search_results.raw['took'], 'query': search_results.query.dumps(pretty=True), 'profile': json.dumps(search_results.raw.get('profile', {}), indent=2), })
def post(self, request, *args, **kwargs): from corehq.apps.es.case_search import CaseSearchES if not case_search_enabled_for_domain(self.domain): raise BadRequest("Domain does not have case search enabled") query = json.loads(request.POST.get('q')) case_type = query.get('type') owner_id = query.get('owner_id') search_params = query.get('parameters', []) query_addition = query.get("customQueryAddition", None) include_closed = query.get("includeClosed", False) xpath = query.get("xpath") search = CaseSearchES() search = search.domain(self.domain).size(10) if not include_closed: search = search.is_closed(False) if case_type: search = search.case_type(case_type) if owner_id: search = search.owner(owner_id) for param in search_params: value = re.sub(param.get('regex', ''), '', param.get('value')) search = search.case_property_query( param.get('key'), value, clause=param.get('clause'), fuzzy=param.get('fuzzy'), ) if query_addition: addition = CaseSearchQueryAddition.objects.get(id=query_addition, domain=self.domain) new_query = merge_queries(search.get_query(), addition.query_addition) search = search.set_query(new_query) if xpath: search = search.xpath_query(self.domain, xpath) search_results = search.run() return json_response({ 'values': search_results.raw_hits, 'count': search_results.total, 'took': search_results.raw['took'], 'query': search_results.query.dumps(pretty=True), })