Commit a63af39a authored by David Mendez's avatar David Mendez
Browse files

ES data: allow to indicate source in query params

parent 6207e3cb
......@@ -62,7 +62,8 @@ def get_es_doc(index_name, doc_id):
:return: the json response with the es_doc data
"""
try:
json_response = es_proxy_service.get_es_doc(index_name, doc_id)
source = request.args.get('source')
json_response = es_proxy_service.get_es_doc(index_name, doc_id, source)
http_response = jsonify(json_response)
http_cache_utils.add_cache_headers_to_response(http_response)
return http_response
......
......@@ -22,3 +22,4 @@ class ESProxyDoc(CommonSchema):
"""
index_name = fields.String(required=True)
doc_id = fields.String(required=True)
source = fields.String()
......@@ -62,15 +62,16 @@ def get_es_data(index_name, raw_es_query, raw_context, raw_contextual_sort_data,
return response
def get_es_doc(index_name, doc_id):
def get_es_doc(index_name, doc_id, source=None):
"""
:param index_name: name of the index to which the doc belongs
:param doc_id: id of the document to search for
:param source: list of properties to get
:return: the json response from elasticsearch of the document
"""
try:
response = es_data.get_es_doc(index_name, doc_id)
response = es_data.get_es_doc(index_name, doc_id, source)
return response
except es_data.ESDataNotFoundError as error:
raise ESDataNotFoundError(str(error))
......
......@@ -104,14 +104,16 @@ def do_multisearch(body):
return result
def get_es_doc(index_name, doc_id):
def get_es_doc(index_name, doc_id, source=None):
"""
:param index_name: name of the intex to which the document belongs
:param doc_id: id of the document
:param source: list of properties to get
:return: the dict with the response from es corresponding to the document
"""
cache_key = f'document-{index_name}-{doc_id}-{RUN_CONFIG.get("cache_key_suffix")}'
properties_list = [] if source is None else source.split(',')
using_source = source is not None
cache_key = f'document-{index_name}-{doc_id}-{",".join(properties_list)}-{RUN_CONFIG.get("cache_key_suffix")}'
app_logging.debug(f'cache_key: {cache_key}')
equivalent_query = {
......@@ -121,6 +123,8 @@ def get_es_doc(index_name, doc_id):
}
}
}
if using_source:
equivalent_query['_source'] = properties_list
start_time = time.time()
cache_response = cache.fail_proof_get(key=cache_key)
......@@ -135,7 +139,8 @@ def get_es_doc(index_name, doc_id):
try:
start_time = time.time()
response = ES.get(index=index_name, id=doc_id)
source_param = properties_list if using_source else True # True means, get all fields
response = ES.get(index=index_name, id=doc_id, _source=source_param)
end_time = time.time()
time_taken = end_time - start_time
......
......@@ -91,6 +91,12 @@ paths:
required: true
type: 'string'
default: 'CHEMBL59'
- name: "source"
in: "query"
description: "Comma separated list of properties to return (Elasticsearch source)"
required: false
type: 'string'
default: 'molecule_chembl_id,pref_name'
responses:
'200':
description: "success"
......
......@@ -40,6 +40,7 @@ def run():
fun_test_organism_taxonomy_target_classification, fun_test_protein_target_classification,
fun_test_covid_entities_records, fun_test_database_summary, fun_test_entities_records,
fun_test_get_all_properties, fun_test_identify_separator, fun_test_entities_join_all_tests]:
test_module.run_test(ARGS.server_base_path, ARGS.delayed_jobs_server_base_path)
......
......@@ -13,8 +13,14 @@ def run_test(server_base_url, delayed_jobs_server_base_path):
"""
print('-------------------------------------------')
print('Testing getting the configuration of a group')
print('Testing getting a document by id')
print('-------------------------------------------')
url = f'{server_base_url}/es_data/get_es_document/chembl_molecule/CHEMBL59'
utils.assert_get_request_succeeds(url)
params = {
'source': "molecule_chembl_id,pref_name"
}
utils.assert_get_request_succeeds(url, params)
......@@ -68,15 +68,18 @@ def print_es_response(response_text, max_chars=200):
print(response_text)
def assert_get_request_succeeds(url_to_test):
def assert_get_request_succeeds(url_to_test, params=None):
"""
tests that doing a get to the url returns a 200 code
:param url_to_test: url to test
:param params: url params to include
"""
print('url: ', url_to_test)
print('params: ', params)
config_request = requests.get(url_to_test)
config_request = requests.get(url_to_test, params=params)
print('full url: ', config_request.request.url)
status_code = config_request.status_code
print(f'status_code: {status_code}')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment