From 7f5c03d681b70139041f0845c596f0a9be0a779d Mon Sep 17 00:00:00 2001 From: SarruSharma <63207249+SarruSharma@users.noreply.github.com> Date: Thu, 15 Apr 2021 21:21:11 -0400 Subject: [PATCH] Update autocomplete.py --- backend/controller/autocomplete.py | 36 +++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/backend/controller/autocomplete.py b/backend/controller/autocomplete.py index 6576cba..a5441ac 100644 --- a/backend/controller/autocomplete.py +++ b/backend/controller/autocomplete.py @@ -4,6 +4,7 @@ from pydantic import BaseModel import langid + langid.set_languages(['de', 'en']) # ISO 639-1 codes # @@ -24,19 +25,34 @@ def addQuestionToAutocomplete(question: str): # todo: if it already exists; we need to increment count; body = { 'phrase': question, - 'count' : 1 + 'count': 1 } - res = api.elasticsearch_client.index(index=DB_INDEX_AUTOCOMPLETE,body=body) + res = api.elasticsearch_client.index(index=DB_INDEX_AUTOCOMPLETE, body=body) + + +class Target: + def get_numsearch(self): + return Request.search + +class AskAdapter(Target): + def get(self): + return int(self.get_numsearch()) +if __name__ == '__main__': + # for testing purposes here + # assume that search has a value for this + obj = AskAdapter + print("testing of using " + obj.get()) + @router.get("/query/autocomplete") def ask(search: str): interim = api.elasticsearch_client.search(index=DB_INDEX_AUTOCOMPLETE, body= { - '_source':['phrase'], - 'query':{ + '_source': ['phrase'], + 'query': { "bool": { "must": [{ "match": { @@ -51,8 +67,8 @@ def ask(search: str): } }, 'size': 10, - 'sort' :[ - {'count' : {'order' : 'desc' }} + 'sort': [ + {'count': {'order': 'desc'}} ] }) @@ -61,12 +77,10 @@ def ask(search: str): for i in range(resultCount): result.append(interim['hits']['hits'][i]['_source']['phrase']) - lang, score = langid.classify(search) return { - "results":result, - "language": lang - } - + "results": result, + "language": lang + }