title: How to make search autocomplete faster?
body: <p>I have implemented a basic auto-complete feature using jQuery autocomplete. I am querying DB every time which is making auto-complete thing quite slow. I am looking for ways to make it faster much like Quora.</p>
  
  <p>Here is the code from front-end:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  
  var URL2 = '&lt;?php e(SITE_URL); ?&gt;fronts/searchKeywords';
  
  jQuery(document).ready(function(){
  
  var CityKeyword = jQuery('#CityKeyword');
  
  CityKeyword.autocomplete({
  minLength    : 1,
  source        : URL2
  });
  
  });
   &lt;/script&gt;
  </code></pre>
  
  <p>Here is the code from server side:</p>
  
  <pre><code>function searchKeywords(){
  
      if ($this-&gt;RequestHandler-&gt;isAjax() ) {
          $this-&gt;loadModel('Expertise_area');
          Configure::write ( 'debug',0);
          $this-&gt;autoRender=false;
  
          $expertise=$this-&gt;Expertise_area-&gt;find('all',array(
                  'conditions'=&gt;array('Expertise_area.autocomplete_text LIKE'=&gt;'%'.$_GET['term'].'%'), 
                  'fields' =&gt; array('DISTINCT (Expertise_area.autocomplete_text) AS autocomplete_text'),
                  'limit'=&gt;5
          ));
          $i=0;
          if(!empty($expertise)){
              $len = strlen($_GET['term']);
              foreach($expertise as $valueproductname){
                  $pos = stripos($valueproductname['Expertise_area']['autocomplete_text'],$_GET['term']);
                  $keyvalue = "";
  
                  if($pos == 0) {
                      $keyvalue= "&lt;strong&gt;".substr($valueproductname['Expertise_area']['autocomplete_text'],$pos,$len)."&lt;/strong&gt;"
                              .substr($valueproductname['Expertise_area']['autocomplete_text'],$len);
                  }else {
                      $keyvalue= substr($valueproductname['Expertise_area']['autocomplete_text'],0,$pos)."&lt;strong&gt;"
                              .substr($valueproductname['Expertise_area']['autocomplete_text'],$pos,$len)."&lt;/strong&gt;"
                              .substr($valueproductname['Expertise_area']['autocomplete_text'],$pos+$len);
                  }
  
  
                  $response[$i]['value']=$valueproductname['Expertise_area']['autocomplete_text'];
                  $response[$i]['label']="&lt;span class=\"username\"&gt;".$keyvalue."&lt;/span&gt;";
                  $i++;
  
              }
              echo json_encode($response);
          }else{
          }
  }
  }
  </code></pre>
  
  <p>I have researched a bit and so far following solutions are worth looking at:</p>
  
  <ol>
  <li><p>Query data on page load and store it in COOKIE to be used in future.</p></li>
  <li><p>Implement some caching mechanism (memcache??). But my website is on Cakephp which does it internal cahcing if I am right. So will it be worth to go in this direction.</p></li>
  <li><p>Use some third party indexing mechanism like Solr, Lucene etc. Don't know much about this.</p></li>
  <li>Implement a much complex "Prefix Search" myself</li>
  </ol>
  
  <p>What is the right way to go about it? Please help me out here.</p>