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

eubopen: Implement first version of autocomplete endpoint

parent 8d8d2866
......@@ -2,6 +2,7 @@
Module with the functions to generate suggestions for eubopen
"""
from app.es_data import es_data
from app.config import RUN_CONFIG
def get_suggestions_for_term(term):
......@@ -9,19 +10,25 @@ def get_suggestions_for_term(term):
:param term: term for which to do the suggestion
:return: the results for the suggestion query
"""
autocomplete_config = RUN_CONFIG.get('eubopen', {}).get('search', {}).get('suggestions', {}).get('entities', {})
return {
'text_suggestions': get_text_suggestions(term)
}
all_suggestions = {}
for entity_key, entity_config in autocomplete_config.items():
entity_suggestions = get_suggestions(term, entity_config)
all_suggestions[entity_key] = entity_suggestions
return all_suggestions
def get_text_suggestions(term):
def get_suggestions(term, entity_config):
"""
:param term: term for which to get the text suggestions
:return: a list of texts with the corresponding suggestions
:param term: term for which to get suggestions
:param entity_config: config of the entity for the suggestions
:return: the suggestions found
"""
index_name = 'chembl_molecule'
index_name = entity_config['index_name']
size = entity_config['size']
suggestion_query = {
'size': 0,
......@@ -30,7 +37,7 @@ def get_text_suggestions(term):
'prefix': term,
'completion': {
'field': '_metadata.es_completion',
'size': 3
'size': size
}
}
}
......@@ -38,5 +45,18 @@ def get_text_suggestions(term):
raw_text_suggestions = es_data.get_es_response(index_name, suggestion_query)
raw_options = raw_text_suggestions["suggest"]["autocomplete"][0]["options"]
phrases = [option["text"] for option in raw_options]
return phrases
options = [parse_option(option) for option in raw_options]
return options
def parse_option(raw_option):
"""
:param raw_option: raw option to parse
:return: a simplified version of the suggestion.
"""
return {
'_id': raw_option['_id'],
'item_label': raw_option['_source'].get('pref_name'),
'text': raw_option['text']
}
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