def get(self, request, doc_id): results = [] errorMsg = { "message": "error executing ElasticSearch query; " + "please check the logs" } errorMsgJSON = json.dumps(errorMsg) try: queries = XSEQueries( XDSConfiguration.objects.first().target_xse_host, XDSConfiguration.objects.first().target_xse_index, user=request.user) response = queries.more_like_this(doc_id=doc_id) results = queries.get_results(response) except HTTPError as http_err: logger.error(http_err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") except Exception as err: logger.error(err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") else: logger.info(results) return HttpResponse(results, content_type="application/json")
def get(self, request): results = [] filters = {} page_num = 1 if (request.GET.get('p')) and (request.GET.get('p') != ''): page_num = int(request.GET['p']) if (request.GET.get('Course.CourseTitle') and request.GET.get('Course.CourseTitle') != ''): filters['Course.CourseTitle'] = request.GET['Course.CourseTitle'] if (request.GET.get('Course.CourseProviderName') and request.GET.get('Course.CourseProviderName') != ''): filters['Course.CourseProviderName'] = \ request.GET['Course.CourseProviderName'] if (request.GET.get('CourseInstance.CourseLevel') and request.GET.get('CourseInstance.CourseLevel') != ''): filters['CourseInstance.CourseLevel'] = \ request.GET['CourseInstance.CourseLevel'] errorMsg = { "message": "error executing ElasticSearch query; " + "Please contact an administrator" } errorMsgJSON = json.dumps(errorMsg) try: queries = XSEQueries( XDSConfiguration.objects.first().target_xse_host, XDSConfiguration.objects.first().target_xse_index, user=request.user) response = queries.search_by_filters(page_num=page_num, filters=filters) results = queries.get_results(response) except HTTPError as http_err: logger.error(http_err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") except Exception as err: logger.error(err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") else: logger.info(results) return HttpResponse(results, content_type="application/json")
def get(self, request): results = [] keyword, filters = self.get_request_attributes(request) if keyword != '': errorMsg = { "message": "error executing ElasticSearch query; " + "Please contact an administrator" } errorMsgJSON = json.dumps(errorMsg) try: search_filters = SearchFilter.objects.filter(active=True) # only add the filters that are defined in the configuration, # the rest is ignored for curr_filter in search_filters: if (request.GET.get(curr_filter.field_name)) and \ (request.GET.get(curr_filter.field_name) != ''): filters[curr_filter.field_name] = \ request.GET.getlist(curr_filter.field_name) queries = XSEQueries( XDSConfiguration.objects.first().target_xse_host, XDSConfiguration.objects.first().target_xse_index, user=request.user) response = queries.search_by_keyword(keyword=keyword, filters=filters) results = queries.get_results(response) except HTTPError as http_err: logger.error(http_err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") except Exception as err: logger.error(err) return HttpResponseServerError(errorMsgJSON, content_type="application/json") else: logger.info(results) return HttpResponse(results, content_type="application/json") else: error = {"message": "Request is missing 'keyword' query paramater"} errorJson = json.dumps(error) return HttpResponseBadRequest(errorJson, content_type="application/json")
def test_get_results(self): """Test that calling get results on a Response Object returns a \ dictionary with hits and a total""" with patch('elasticsearch_dsl.response') as response_obj: response_obj.return_value = {"hits": {"total": {"value": 1}}} response_obj.hits.total.value = 1 with patch('elasticsearch_dsl.response.hit.to_dict') as to_dict, \ patch('es_api.utils.queries.SearchFilter.objects') as sfObj, \ patch('elasticsearch_dsl.response.aggregations.to_dict')\ as agg: agg.return_value = {} sfObj.return_value = [] to_dict.return_value = {"key": "value"} query = XSEQueries('test', 'test') result_json = query.get_results(response_obj) result_dict = json.loads(result_json) self.assertEqual(result_dict.get("total"), 1) self.assertEqual(len(result_dict.get("hits")), 0) self.assertEqual(len(result_dict.get("aggregations")), 0)