diff --git a/nboost/maps.py b/nboost/maps.py index 6b2c1b3d..695fa4ad 100644 --- a/nboost/maps.py +++ b/nboost/maps.py @@ -48,8 +48,8 @@ 'topk_path': '(body.size) | (url.query.size)', 'true_cids_path': 'body.nboost.cids', 'choices_path': 'body.hits.hits', - 'cvalues_path': '[*]._source.*', - 'cids_path': '[*]._id', + 'cvalues_path': '_source.*', + 'cids_path': '_id', 'capture_path': '/.*/_search', 'default_topk': 10 } diff --git a/nboost/proxy.py b/nboost/proxy.py index 07ab767f..a9add605 100644 --- a/nboost/proxy.py +++ b/nboost/proxy.py @@ -320,13 +320,17 @@ def get_request_paths(request, configs) -> Tuple[str, int, list]: def get_response_paths(response, configs) -> Tuple[list, list, list]: """Get the request jsonpaths noted in the configs""" choices = get_jsonpath(response, configs['choices_path']) + + if not isinstance(choices, list): + raise InvalidChoices('choices were not a list') + choices = flatten(choices) - cids = get_jsonpath(choices, configs['cids_path']) - cvalues = get_jsonpath(choices, configs['cvalues_path']) + cids = get_jsonpath(choices, '[*].' + configs['cids_path']) + cvalues = get_jsonpath(choices, '[*].' + configs['cvalues_path']) # check for errors if not len(choices) == len(cids) == len(cvalues): - raise InvalidChoices + raise InvalidChoices('number of choices, cids, and cvalues differ') return choices, cids, cvalues @@ -401,8 +405,8 @@ def loop(self, client_socket: socket.socket, address: Tuple[str, str]): self.proxy_send(client_socket, server_socket, buffer) self.proxy_recv(client_socket, server_socket) - except InvalidChoices: - self.logger.warning('Request (%s:%s): invalid choices', *address) + except InvalidChoices as exc: + self.logger.warning('Request (%s:%s): %s', *address, *exc.args) self.proxy_send(client_socket, server_socket, buffer) self.proxy_recv(client_socket, server_socket) diff --git a/tests/test_config_map.py b/tests/test_config_map.py index 52754fba..a8533b73 100644 --- a/tests/test_config_map.py +++ b/tests/test_config_map.py @@ -43,10 +43,10 @@ def test_response_1(self): choices = flatten(choices) self.assertEqual(1.4, choices[0]['_score']) - cvalues = get_jsonpath(choices, config['cvalues_path']) + cvalues = get_jsonpath(choices, '[*].' + config['cvalues_path']) self.assertEqual(["trying out Elasticsearch", "second result", "third result"], cvalues) - cids = get_jsonpath(choices, config['cids_path']) + cids = get_jsonpath(choices, '[*].' + config['cids_path']) self.assertEqual(['0', '1', '2'], cids)