0

I'm trying to create an autocomplete demonstration using some data in elasticsearch. I have a simple code that takes an input, then returns a list of recommended autocompletes.

while True:
    search = input("start typing")

    body = {
         "size": 5,
         "query": {                                                     
             "multi_match": {
                 "query": "'"+search+"'"                         
             }                               
         }                
     }
    res = es.search(index="autocomplete", body=body) //getting the suggestions
    for hit in res['hits']['hits']:
        doc = hit["_source"]["company_name"]
        print(doc) //printing the suggestions

Right now it works like this: Say I want to search for "Google". terminal will say start typing! and I'll type "G", press enter and I'll get a list of autocomplete suggestions, then it will say start typing! and I'll type "Go" and get a new list of suggestions, then next time I type "Goo", etc. just trying to see how the autocomplete suggestions change as I type more and more of the word.

I would like to get it to where if I type or change anything, it will automatically run the autocomplete code, so I can just type "G", it runs, then I type "o" which adds it to the previous "G" so it runs for "Go", then I type another "o", "g","l","e". Basically I want it to run like a normal autocomplete for Google search, where as you keep typing it will update the suggestions. Is there a way to do this in python?

Brian Guan
  • 193
  • 2
  • 12

1 Answers1

0

I would like to get it to where if I type or change anything, it will automatically run the autocomplete code

No, there is no out-of-the-box way to do this in Python; both the input and raw_input require the user to press Enter. Here are a few StackOverflow posts which discuss possible solutions:

The solution you choose will depend on whether you need cross platform support or not.

dstricks
  • 1,465
  • 12
  • 23