Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do autocomplete suggestions work?

Tags:

autocomplete

For example, if you type something in upper-right google/yahoo search box in firefox there will be some kind 'suggested auto complete' sort of thing.

Another example is in youtube search box and Stackoverflow tags edit box just below this question preview. How do they work? What technology behind 'em?

like image 281
mhd Avatar asked Dec 08 '08 10:12

mhd


People also ask

Are Google Autocomplete suggestions Personalised?

If you're signed in to your Google Account and have Personal results turned on, you might also get personalized predictions and recommendations in Google Search. If you don't want to get these predictions and recommendations, turn off Personal results.

What are autocomplete suggestions?

Autocomplete is a search feature where the search engine predicts the user's query and provides suggestions as the user types. The user can select any of the autocomplete suggestions and be taken to results without having to manually type every character.

How is predictive search implemented?

To create predictive search suggestions, you overlap two containers (a search box and a predictive box), then send your user's current query to your suggestions index at every keystroke. Based on the query, the predictive box displays and consistently updates the first result from the suggestions.


2 Answers

What technology behind 'em?

In case you are wondering which data structure is being used underneath then its called "trie" and for using less space compared to tries you can use "DAFSA"

How do they work?

both are implemented as a tree, where each node of tree corresponds to one character in a string and the character which appears before is parent of character which appears later e.g. The strings "tap", "taps", "top", and "tops" stored in a Trie (left) and a DAFSA (right),so as you begin to type tap..the tree is traversed based on the characters typed and shows the suggestions based on some weight assigned to each word, weight may be assigned based on usage frequency of the word.

The strings "tap", "taps", "top", and "tops" stored in a Trie (left) and a DAFSA (right), EOW stands for End-of-word.

Looking up string in worst case is O(m) time where m is the length of string.

Image is being referenced from the wikipedia articel : DAFSA,trie

like image 80
Eklavyaa Avatar answered Sep 27 '22 16:09

Eklavyaa


It's quite simple.

Client side:

  1. Grab keystrokes in form field
  2. On keystroke make an AJAX request to server
    1. If another keystroke is entered immediately, cancel current AJAX request as it is obsolete now
    2. Make a new AJAX requested with updated characters in form field
  3. Show server response to client

Server side:

  1. All words are already bucketed alphabetically
  2. If client request comes in for "ove" find all words starting with ove, ordered by popularity
  3. Return top matches to client
like image 42
aleemb Avatar answered Sep 27 '22 16:09

aleemb