def _search_table(*, search_term: str, page_index: int, filters: Dict, search_type: str) -> Dict[str, Any]: """ Call the search service endpoint and return matching results Search service logic defined here: https://github.com/lyft/amundsensearchlibrary/blob/master/search_service/api/table.py :return: a json output containing search results array as 'results' """ # Default results tables = { 'page_index': int(page_index), 'results': [], 'total_results': 0, } results_dict = { 'search_term': search_term, 'msg': '', 'tables': tables, } try: if has_filters(filters=filters): query_json = generate_query_json(filters=filters, page_index=page_index, search_term=search_term) url_base = app.config[ 'SEARCHSERVICE_BASE'] + SEARCH_TABLE_FILTER_ENDPOINT response = request_search( url=url_base, headers={'Content-Type': 'application/json'}, method='POST', data=json.dumps(query_json)) else: url_base = app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_ENDPOINT url = f'{url_base}?query_term={search_term}&page_index={page_index}' response = request_search(url=url) status_code = response.status_code if status_code == HTTPStatus.OK: results_dict['msg'] = 'Success' results = response.json().get('results') tables['results'] = [ map_table_result(result) for result in results ] tables['total_results'] = response.json().get('total_results') else: message = 'Encountered error: Search request failed' results_dict['msg'] = message logging.error(message) results_dict['status_code'] = status_code return results_dict except Exception as e: message = 'Encountered exception: ' + str(e) results_dict['msg'] = message logging.exception(message) return results_dict
def search_table_query_string() -> Response: """ TODO (ttannis): Update this docstring after amundsensearch documentation is merged Calls the search service to execute a search. The request data is transformed to the json payload defined [link] """ request_json = request.get_json() search_term = get_query_param(request_json, 'term', '"term" parameter expected in request data') page_index = get_query_param(request_json, 'pageIndex', '"pageIndex" parameter expected in request data') filters = request_json.get('filters', {}) # Default results tables = { 'page_index': int(page_index), 'results': [], 'total_results': 0, } results_dict = { 'search_term': search_term, 'msg': '', 'tables': tables, } try: query_json = generate_query_json(filters=filters, page_index=page_index, search_term=search_term) except Exception as e: message = 'Encountered exception generating query json: ' + str(e) results_dict['msg'] = message logging.exception(message) return make_response(jsonify(results_dict), HTTPStatus.INTERNAL_SERVER_ERROR) try: # TODO (ttannis): Change actual endpoint name after amundsensearch PR is merged url = app.config['SEARCHSERVICE_BASE'] + '/search_table' response = request_search(url=url, headers={'Content-Type': 'application/json'}, method='POST', data=json.dumps(query_json)) status_code = response.status_code if status_code == HTTPStatus.OK: results_dict['msg'] = 'Success' results = response.json().get('results') tables['results'] = [map_table_result(result) for result in results] tables['total_results'] = response.json().get('total_results') else: message = 'Encountered error: Search request failed' results_dict['msg'] = message logging.error(message) results_dict['status_code'] = status_code return make_response(jsonify(results_dict), status_code) except Exception as e: message = 'Encountered exception: ' + str(e) results_dict['msg'] = message logging.exception(message) return make_response(jsonify(results_dict), HTTPStatus.INTERNAL_SERVER_ERROR)
def test_generate_query_json(self) -> None: """ Verify that the returned diction correctly transforms the parameters :return: """ query_json = generate_query_json(filters=self.test_filters, page_index=self.test_page_index, search_term=self.test_term) self.assertEqual(query_json.get('page_index'), int(self.test_page_index)) self.assertEqual(query_json.get('search_request'), { 'type': 'AND', 'filters': self.expected_transformed_filters }) self.assertEqual(query_json.get('query_term'), self.test_term)