Autocomplete server-side implementation

后端 未结 10 637
感动是毒
感动是毒 2020-12-28 16:03

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

10条回答
  •  时光取名叫无心
    2020-12-28 16:55

    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.

提交回复
热议问题