What is a fast and efficient way to implement the server-side component for an autocomplete feature in an html input box?
I am writing a service to autocomplete use
With a set that large I would try something like a Lucene index to find the terms you want, and set a timer task that gets reset after every key stroke, with a .5 second delay. This way if a user types multiple characters fast it doesn't query the index every stroke, only when the user pauses for a second. Useability testing will let you know how long that pause should be.
Timer findQuery = new Timer();
...
public void keyStrokeDetected(..) {
findQuery.cancel();
findQuery = new Timer();
String text = widget.getEnteredText();
final TimerTask task = new TimerTask() {
public void run() {
...query Lucene Index for matches
}
};
findQuery.schedule(task, 350); //350 ms delay
}
Some pseduocode there, but that's the idea. Also if the query terms are set the Lucene Index can be pre-created and optimized.