3

So I'd like to know what is the general algorithm to implementing an instant search that is not load intensive. Not specifically on the web but even in a desktop/winforms application.

Correct me if Im wrong but one cannot send async calls on every key stroke right? (Not sure how google instant manages this) It would create an insane load on the database/store etc.

Ive been thinking of something like this:

  1. Fire timer every xxx milliseconds
  2. On fire, Disable input, Disable timer, and send an async call to search.
  3. When the call returns, display results, enable input, enable timer

Is this how it it generally handled, or is there a better way?

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113
  • i think you should only fire after the person _stops_ typing for some time, rather than disabling input. – lijie Nov 25 '10 at 14:29
  • 2
    You don't want to disable input while waiting for the search to return - many people can type faster than you can get search results back from a server, and this would be REALLY annoying. – Alex Brown Nov 25 '10 at 14:30
  • True, so you mean call async search every xxx milliseconds? In general itself I find instant annoying sometime, you end up querying for phrases you didn't complete, all that load just so you don't press enter. Maybe Im being a little lazy! lol Still my people think it would be impressive and look better! – gideon Nov 25 '10 at 14:41

1 Answers1

2

Search queries are generally quite small, so the increased load on the server may not be as significant as you think. Sending a query on every keystroke should be fine as long as you keep a limit on the length of queries.

Anyway, it's the server that knows how loaded it is, so the place to put the load management is on the server side. For example, you could follow a strategy something like this:

On the client:

  • When the search text changes, send it to the server.
  • When the server sends some results, update the page.

On the server, when a query is received from a client:

  • If I am already handling a query from that client, cancel the old query.
  • If I have a queued query from that client, discard it.
  • Add the new query to a queue of pending search queries, unless the queue is full.
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • What you say about the server makes sense, but makes for a hairy situation in terms of implementation. Lets say with an wcf data service (OData) exposing data to clients, I would have to some kind of wrapper service over it to check for queued queries and cancelling them right? – gideon Nov 25 '10 at 17:27