Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter pattern applied to autocomplete.py #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions backend/controller/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pydantic import BaseModel

import langid

langid.set_languages(['de', 'en']) # ISO 639-1 codes

#
Expand All @@ -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": {
Expand All @@ -51,8 +67,8 @@ def ask(search: str):
}
},
'size': 10,
'sort' :[
{'count' : {'order' : 'desc' }}
'sort': [
{'count': {'order': 'desc'}}
]
})

Expand All @@ -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
}