Commit 8d8d2866 authored by David Mendez's avatar David Mendez
Browse files

eubopen: Add first implementation of text suggestions for search bar

parent d810ebd6
......@@ -3,13 +3,14 @@ Services for the eubopen search
"""
import random
from app.eubopen.search.suggestions import suggestions
def get_autocomplete_results(term):
"""
:param term: term for which to do the autocomplete
:return: the results for the autocomplete query
"""
print('term: ', term)
all_suggestions = [
{
'key': random.randint(0, 1000),
......@@ -45,6 +46,4 @@ def get_autocomplete_results(term):
indexes = list(range(0, len(all_suggestions)))
random.shuffle(indexes)
picked_indexes = indexes[:random.randint(0, len(all_suggestions))]
return {
'suggestions': [all_suggestions[picked_idx] for picked_idx in picked_indexes]
}
return suggestions.get_suggestions_for_term(term)
"""
Module with the functions to generate suggestions for eubopen
"""
from app.es_data import es_data
def get_suggestions_for_term(term):
"""
:param term: term for which to do the suggestion
:return: the results for the suggestion query
"""
return {
'text_suggestions': get_text_suggestions(term)
}
def get_text_suggestions(term):
"""
:param term: term for which to get the text suggestions
:return: a list of texts with the corresponding suggestions
"""
index_name = 'chembl_molecule'
suggestion_query = {
'size': 0,
'suggest': {
'autocomplete': {
'prefix': term,
'completion': {
'field': '_metadata.es_completion',
'size': 3
}
}
}
}
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
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