What is the best way to implement AutoComplete on a server?

This question is easy for people. Making autocomplete beautiful on the client side of a web application is simple. There are many plugins.

But, on the flip side, on the server side, what's the best way to do this? I do not like the idea of ​​hitting the database with every clicked user.

I was thinking about sphinx or some kind of full-text search engine parallel to your site.

For example, if I have a PHP website (high traffic), I can create a parallel python script that receives HTTP requests from my "autocomplete text fields". Then, when the user presses a key on the client side, AJAX requests are sent to this python script, which can use a special strategy.

What is your approach?

Some conventions:

  • Try not to get into the database. I mean, querying and doing something SELECT * FROM foo WHERE bar LIKE "req%" is not a good answer. This may be a good strategy, but I know how to do it. *
  • Replicated data can be a good choice.
+4
source share
2 answers

I agree that you need to have a better solution. Apache solr has a “suggestion” function that you can use pretty well. If your data set is small, then put all the data into memory and just do a simple loop.

At the front end, I recommend using setTimeout () to wait about 200 ms before starting an ajax call. If another keystroke starts within 200 ms, cancel the last timeout and start another. This is really a clean solution, in which every keystroke would not hit a bit. I have used it in the past and it works very well.

This explains solr with jquery and how to properly create autocomplete. http://www.mattweber.org/2009/05/02/solr-autosuggest-with-termscomponent-and-jquery/

+8
source

You say in the comments that “This is a small data set” of keywords. Thus, it may be appropriate for the client to request the entire list as soon as the user begins to enter text in the field, and then JavaScript responds to changes in user input on the client side.

This server gets into the field on the page (and only if the user enters this field), and you can cache it on the server so that it rarely gets into the database.

Edit: Caching on the server is a big win, because the list is the same for every request and for all users, but even better, this means that you can cache the list in the client’s browser using the Expires or Etag with a suitable response period. Thus, the user can get unlimited autocomplete in just one (well-cached) server hit for the entire period of the browser cache.

0
source

All Articles