Improving Twitter-typeahead.js performance with remote data using Django

I have a database with names of approximately 1.2M. I use Twitter typeahead.js to remotely receive autocomplete suggestions when typing in a name. In my local environment, it takes about 1-2 seconds until the results appear after you stop typing (autocomplete does not appear during input) and 2-5 + seconds in a deployed application on Heroku (using only 1 dino).

I am wondering why the reason is that it only shows suggestions after you stopped typing (and the delay is a few seconds), because my code is not optimized?

script on page:

<script type="text/javascript">
$(document).ready(function() {
  $("#navPersonSearch").typeahead({
    name: 'people',
    remote: 'name_autocomplete/?q=%QUERY'
  })
    .keydown(function(e) {
        if (e.keyCode === 13) {
            $("form").trigger('submit');
        }
    });
});
</script> 

A piece of code will open, because without it, my form will not be submitted for any reason when I press enter.

my django view:

def name_autocomplete(request):
    query = request.GET.get('q','')
    if(len(query) > 0):
        results = Person.objects.filter(short__istartswith=query)
        result_list = []
        for item in results:
            result_list.append(item.short)
    else:
        result_list = []

    response_text = json.dumps(result_list, separators=(',',':'))
    return HttpResponse(response_text, content_type="application/json")

Person . ?

+4
4

, Django, . :

( # 4 # 5 ).

1) "" Heroku? , . . , 8-9 , . https, .

2) waitLimitFn rateLimitWait remote. ?

3) , / . , , - , ( ?).

4) : . . NewRelic ( ). , . "" .

5) : ? , , "J" , , . . :

5.1) minLength . 3, 4.

5.2) , . 10, .

6) Django, , Django, . .

.

+3
        results = Person.objects.filter(short__istartswith=query)
        result_list = []
        for item in results:
            result_list.append(item.short)

, , : django. django, values_list. :

        results = Person.objects.filter(short__istartswith=query)
        result_list = results.values_list('short', flat=True)

, db, : , Person , , .

+1

Nitzan , , , , , Django ( , ).

- name_autocomplete, 10 , Typeahead. (, , , , Typeahead ).

, , Typeahead , , minLength.

, , , Python , .

, , , , , $('#navPersonSearch') typeahead:initialized typeahead:opened, , - .

0
source

You can use django haystack , and your server side code will look something like this:

def autocomplete(request):
sqs = SearchQuerySet().filter(content_auto=request.GET.get('q', ''))[:5]  # or how many names you need
suggestions = [result.first_name for result in sqs]
# you have to configure typeahead how to process returned data, this is a simple example
data = json.dumps({'q': suggestions})  
return HttpResponse(data, content_type='application/json')
0
source

All Articles