Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typeahead.js with a large database

Tags:

I have a large database of 10,000 addresses and 5,000 people.

I want to let users search the database for either an address or a user. I'd like to use Twitter's typeahead to suggest results as they enter text.

See the NBA example here: http://twitter.github.io/typeahead.js/examples.

I understand that prefetching 15,000 items would not be optimal from a speed and load standpoint. What would be a better way to try and achieve this?

like image 258
alias51 Avatar asked Aug 06 '13 15:08

alias51


People also ask

How do you set a value in Typeahead?

jQuery#typeahead('val', val) Sets the value of the typeahead. This should be used in place of jQuery#val.

What is jQuery Typeahead?

The jQuery Typeahead plugin provides autocomplete preview on search inputs similar to google search with builtin options and deep customization. It is a simple clientside library that will improve the user experience on your website search input! The jQuery Typeahead plugin is released under the MIT License.

What is Typeahead field?

Typeahead is a feature of computers and software (and some typewriters) that enables users to continue typing regardless of program or computer operation—the user may type in whatever speed is desired, and if the receiving software is busy at the time it will be called to handle this later.


1 Answers

Since no one made any answer, I will go ahead with my suggestion then.

I think the best fit for your big database is using remote with typeahead.js. Quick example:

$('#user-search').typeahead({     name: 'user-search',     remote: '/search.php?query=%QUERY' // you can change anything but %QUERY }); 

What it does is when you type characters in the input#user-search it will send AJAX request to the page search.php with query as the content of the input.

On search.php you can catch this query and look it up in your DB:

$query = $_GET['query'].'%'; // add % for LIKE query later  // do query $stmt = $dbh->prepare('SELECT username FROM users WHERE username LIKE = :query'); $stmt->bindParam(':query', $query, PDO::PARAM_STR); $stmt->execute();  // populate results $results = array(); foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $row) {     $results[] = $row; }  // and return to typeahead return json_encode($results); 

Of course since your DB is quite big, you should optimize your SQL query to query faster, maybe cache the result, etc.

On the typeahead side, to reduce load to query DB, you can specify minLength or limit:

$('#user-search').typeahead({     name: 'user-search',     remote: '/search.php?query=%QUERY',     minLength: 3, // send AJAX request only after user type in at least 3 characters     limit: 10 // limit to show only 10 results }); 

So it doesn't really matter how big your DB is, this approach should work nicely.

This is an example in PHP but of course it should be the same for whatever backend you have. Hope you get the basic idea.

like image 84
Hieu Nguyen Avatar answered Oct 20 '22 20:10

Hieu Nguyen