Reputation: 160
I am working on a Django application that requires the implementation of live search i.e. filtering and displaying the search result every time the form input is changed.
For example:
Consider I wanted to search my database for books when a book "name" is provided. Now, I want to suggest books which has a "name" similar to the "name" provided as input.
And, I want these suggestions to be live - every time the input field for "name" is changed, my application should provide new and more accurate suggestions.
How should I go about doing that?
I have two solutions in mind.
Use jQuery and do AJAX calls to fetch new suggestions, every time the content of the input field for "name" is changed.
Fetch all the books available in the database and store it in a JS array and then use this array to provide suggestions.
I think solution - 2 would not be feasible for large databases as we will not able to create arrays of such large sizes.
So, solution - 1 is the only option left for large databases.
Or are there any other ways to implement this "live search" option ?
If yes, then what are they?
else, what are the ways in which I can optimize solution - 1 ?
Thanks!
Upvotes: 3
Views: 5952
Reputation: 8570
For small data 100s of records, maybe 1000s if they are not too big, approach 2 is okay.
For larger data, approach 1 is better: Have a look at the typeahead library: https://twitter.github.io/typeahead.js/ - you can create an api using http://www.django-rest-framework.org/
For large data I use elasticsearch to mirror the data from the models. This library is a good way to get started: https://github.com/jaddison/django-simple-elasticsearch
Upvotes: 2