search

How to Add Auto Complete to Your Google Custom Search Engine

This tutorial will show you how to use the "Popular Queries" feed from your Google Custom Search Engine (CSE) as a data source for a jQuery autocomplete.

Preface

Google CSE Homepage

Google’s Custom Search Engine (CSE) allows you to create a robust search feature for your Web site. They offer a free,
ad-supported version and a premium business version that starts at $100 per year. Additionally, CSE provides a wide range of metrics, ranging from integration with
Google Analytics to a feed of popular search queries.

This tutorial will show you how to use PHP and jQuery to add an auto complete feature to CSE’s default search box using the popular search queries feed as the data source.

In order to successfully use this technique on your site, you’ll need your own Google CSE and a decent amount of search traffic (to ensure we have a nice set of data for
our auto complete list).

Don’t worry if you don’t meet all of these requirements—you can still follow along. Google often cites MacWorld’s CSE implementation
as an example, so I’ll be using their search feed in this tutorial. Feel free to do the same if you’d like.

Let’s get started.

Step 1: Create Your Search Page

The first thing we’ll do is add the CSE’s default search code to a new XHTML page. You can find this by logging into your control panel and clicking “code.” It will
look something like this.

	<form action="http://www.google.com/cse" id="cse-search-box">
	  <div>
	    <input type="hidden" name="cx" value="003198751674731024891:ovffo1orlum" />
	    <input type="hidden" name="ie" value="UTF-8" />
	    <input type="text" name="q" size="31" />
	    <input type="submit" name="sa" value="Search" />
	  </div>
	</form>
	<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script>

Save this document in a new folder as search.html and open it in your browser. Search for something to make sure the search box works.

Google CSE Search Results

Step 2: Adding the jQuery Auto Complete Function

Although the jQuery UI has an auto complete function built in, you might find the
auto complete plugin
created by Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, and Jörn Zaefferer is a little easier to use. Download
jquery.autocomplete.zip and unzip it.

The plugin’s archive contains a variety of different scripts for many implementations. While the best practice would be to move the scripts and stylesheets we’re
going to use to appropriately named folders inside of our page’s root, in the interest of simplicity, let’s just drag the
“jquery-autocomplete” folder into the folder our search.html is in.

The plugin comes with a demo illustrating how the auto complete could be used with city names. Let’s make sure jQuery and our plugin are working properly
by hooking our Google search box up to the list of cities. In search.html, add the following inside the <head> tag.

	<script type="text/javascript" src="jquery-autocomplete/lib/jquery.js"></script>
	<script type="text/javascript" src="jquery-autocomplete/jquery.autocomplete.js"></script>
	<script type="text/javascript" src="jquery-autocomplete/demo/localdata.js"></script>
	<link rel="stylesheet" type="text/css" href="jquery-autocomplete/jquery.autocomplete.css" />

<script type="text/javascript"> $().ready(function() { $("#cse_search").autocomplete(cities); }); </script>

We’ll also need to slightly modify CSE’s default search code by adding an id attribute to the search box. We’ll call it “cse_search.”

    <input type="text" id="cse_search" name="q" size="31" />  

Save search.html and open it in your browser. In the search box, start typing the name of a city; you should see the autocomplete menu.

Starting to enter the name of a city will offer suggestions.

Step 3: Getting the Data Set

In the previous step, we included the “jquery-autocomplete/demo/localdata.js” script. If you open the file and look at it, you’ll see a few different
JavaScript arrays. These are the arrays used to populate auto complete lists in the plugin’s demo files. When we initialized jQuery and instructed the plugin
to auto complete our “cse_search” box, we also told it to get its data from the cities array:

	$().ready(function() {
		$("#cse_search").autocomplete(cities);
	});

Now we need to instruct jQuery to use our popular queries feed as its data source. But how?

We’ll use a little PHP to pull in the popular queries feed, parse it, and echo out a valid JavaScript array. By including the PHP file as we would
a regular JavaScript file, it will be executed behind the scenes and the Web browser will think it’s reading a static JS file.

Additionally, we’re also going to supplement our popular queries feed with terms that we specify. The terms we specify here may not be searched often
enough to show up as a “popular query,” but they still may be useful to have in our auto complete list. For instance, terms for which you’ve created
Google subscribed links or terms that monetize well with
AdSense for Search.

Create a file inside the “jquery-autocomplete” folder called searchdata.php and paste in the following:

	<?php
		/* This script parses the Popular Queries feed from Google's CSE product and outputs
		   the recent queries in a JavaScript array for use with the jQuery Autocomplete plugin. */

		// There may be some search queries you want to include in the autocomplete box
		// that aren't necessarily popular searches and therefore don't show up in your 
		// Google CSE feed.  You can add those terms to this array to ensure they show up.
		// You will want to make sure you enter the terms in lowercase. 
		$data = array(
			"steve jobs",
			"macbook pro",
			"macbook air",
			"itunes",
			"ipod"
		);

		// Load the Popular Queries RSS Feed from Google's CSE using SimpleXML
		// The URL is available by clicking "Statistics" from inside your CSE control panel.
		if (!$s = simplexml_load_file("http://www.google.com/coop/api/003198751674731024891/cse/ovffo1orlum/queries?sig=__GaZojo71AtdDbVHqJ9KDPhwXAhk=")) {
			exit();											// cheap error handling.
		}

		// Create an array of all the popular queries. 
		foreach($s->item as $item) {
			$search_term = strtolower(trim($item->title)); 	// trim() is used to remove whitespaces.
			if (!in_array($search_term, $data)) {           // ensure there are no duplicates. 
				$data[] = $search_term;					    // add $search_term to data array.
			}
		}	
		sort($data); //alphabetize the array

		// Format the data for JavaScript output.
		foreach($data as $search_term) {
			$js_data[] = "\"" . $search_term . "\"";	
		}	
		
		// Let's inform the browser that we're sending JavaScript.
		header("Content-type: text/javascript\n\n");
		
		// Next we'll escape from PHP and create a JavaScript array. Inside the array, we'll return to
		// PHP and use implode() to return a comma-separated string of all the data inside $js_data.
	?>
	var searchdata = [<?php echo implode($js_data, ", "); ?>];
Output of searchdata.php

If you’re using your own CSE feed, you’ll want to replace the URL on line 7. In this example, I used the overall popular queries feed for MacWorld.com.
You can use your own overall popular queries feed by going to your CSE Manage Page > Statistics >
Overall. Other available options are popular query feeds by day, week, and month.

Next, we’ll need to remove the demo’s localdata.js script from search.html and replace it with our searchdata.php file:

	Replace:
	<script type="text/javascript" src="jquery-autocomplete/demo/localdata.js"></script>
	
	With:
	<script type="text/javascript" src="jquery-autocomplete/searchdata.php"></script>

We’ll also need to slightly modify our initialization code:

	Replace:
	$("#cse_search").autocomplete(cities);
	
	With:
	$("#cse_search").autocomplete(searchdata);

Now let’s upload everything to the server and give search.html a shot. If everything is working like it’s supposed to, your auto complete
should be working perfectly.

A working examples of the CSE RSS-powered autocomplete.

A Word on Caching

Sites that receive a significant amount of traffic may want to consider caching the search array. Having the server parse the feed
each time someone types into the search box will use a significant amount of resources. You can cache the results by replacing your
searchdata.php file with the following:

Note: The script will create the cache for the first time, but it must have write access to the directory you’re
going to store it in.

	<?php
		/* This script parses the Popular Queries feed from Google's CSE product and outputs
		   the recent queries in a JavaScript array for use with the jQuery Autocomplete plugin. */

		// Set information about the cache
		$cache_path	= "cache/searchdata-cache.txt";   // the directory must be writeable by the server
		$cache_time = 3600;							  // seconds to keep the cache for

		// Determine if the cache is there
		$cache_exists = @file_exists($cache_path);	  // returns true or false

		// Let's inform the browser that we're sending JavaScript.
		header("Content-type: text/javascript\n\n");

		// If there is a cache and it's old, delete it. If it's current, use it.
		if ($cache_exists) {
			$cache_age = filectime($cache_path);
			if ($cache_age < (time() - $cache_time)) {
				unlink($cache_path);				  // delete the cache
			} else {
				include($cache_path);				  // load the cache
				exit();								  // no need to continue processing
			}
		}

		// There may be some search queries you want to include in the autocomplete box
		// that aren't necessarily popular searches and therefore don't show up in your 
		// Google CSE feed.  You can add those terms to this array to ensure they show up.
		// You will want to make sure you enter the terms in lowercase. 

		$data = array(
			"steve jobs",
			"macbook pro",
			"macbook air",
			"itunes",
			"ipod"
		);

		// Load the Popular Queries RSS Feed from Google's CSE using SimpleXML
		// The URL is available by clicking "Statistics" from inside your CSE control panel.
		if (!$s = simplexml_load_file("http://www.google.com/coop/api/003198751674731024891/cse/ovffo1orlum/queries?sig=__GaZojo71AtdDbVHqJ9KDPhwXAhk=")) {
			exit();											// cheap error handling.
		}

		// Create an array of all the popular queries. 
		foreach($s->item as $item) {
			$search_term = strtolower(trim($item->title)); 	// trim() is used to remove whitespaces.
			if (!in_array($search_term, $data)) {           // ensure there are no duplicates. 
				$data[] = $search_term;					    // add $search_term to data array.
			}
		}	
		sort($data); //alphabetize the array

		// Format the data for JavaScript output.
		foreach($data as $search_term) {
			$js_data[] = "\"" . $search_term . "\"";	
		}	

		// Setup the cache
		$fp = fopen($cache_path, "w");

		// Create the JavaScript array
		$js_array = "var searchdata = [" . implode($js_data, ", ") . "];";

		// Write the array to the cache
		fwrite($fp, $js_array);

		// Close the cache
		fclose($fp);

		// Include the cache file.
		include($cache_path);
	?>
  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.clipcroma.com Jônatan Fróes

    Thanks….

  • http://johanbrook.wordpress.com John

    Quite nice tutorial. The autocomplete feature is very handy, and unobstrusive.

  • http://URL(Optional) Eddie

    nice one.. will test this out tomorrow :D

  • http://freewarecollection.co.cc Andrew

    It’s really interesting to deal interactively with existing search form like Google CSE. Thanks!

  • http://URL(Optional) Chris

    Great job! Very well written and a great use of the autocomplete plugin, thanks!

  • http://www.freshclickmedia.com Shane

    Nice tutorial that will be of interest to lots of readers I’m sure :)

  • http://snedekerdesignz.com/blog/ Craigsnedeker

    Really good tutorial! I love it!

  • http://www.designshard.com Max | Design Shard

    Really cool feature, agreed with @John about being unobtrusive ( doesnt rely on it to work)

  • http://weborbus.com Balaji

    Great tips!!!!!!!!!!!!!!!

  • http://URL(Optional) secruza

    Really nice tutorial!

  • http://laminbarrow.com Lamin

    This is quite practical. I mean we could really use stuff like this. Thanks for your thoughts and effort putting this together. :)

  • Pingback: Useful Links (29/10/2008) | Apramana

  • http://URL(Optional) Tiago Golvea

    It’s a great idea !!!

  • http://www.ben-griffiths.com Ben Griffiths

    Really great tut, thanks :D

  • http://www.1pixelbrush.com Dan

    Awesome….

  • http://depi.sk depi

    Wow, that’s cool idea. I should try it when I get some free time.

  • http://mokshasolutions.com Moksha

    thanks,

  • http://URL(Optional) Kevin

    The demo on this page worked once, then it quit working. I even restarted the browser, deleted cache, and tried it in safari, opera and ff. It doesn’t work.

    I didn’t get it to work even when following the tutorial step by step. (And no, I’m not a newb)

    Might want to double check.

  • http://webpick.insicdesigns.com insic

    This is what im looking for. thanks

  • Pingback: NETTUTS.com: How to Add Auto Complete to Your Google Custom Search Engine : WebNetiques

  • Pingback: NETTUTS.com: How to Add Auto Complete to Your Google Custom Search Engine : Dragonfly Networks

  • Pingback: AndySowards.com :: Web Development Nerdy Daily Links For 10/30/2008 | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?

  • http://www.twosocks.com.au TwoSocks – website design

    while this feature is quite nice it has its problems. If you type in some text that is doesn’t reakonise than it places the cursor immediately back at the beginning of the sentence, works fine for words it knows but fails miserable with words it doesn’t, so you couldn’t actually implement this on your site!

  • http://www.skoov.ru/assortiment.php VedernikovSergey

    Вентилируемые фасады, ЦВП, ЦСП, утеплители, гибкие связи,
    стеклопластиковая и базальтовая арматура, этафом,опалубка, закладные в монолит
    облегченный утеплитель, легкий утеплитель, утепление бетона, укрытие бетона, сосульки,
    техноэласт, доставка, опт, сертификаты, дизайн, интерьеры
    skoov.ru/assortiment.php

  • Pingback: BlogBuzz October 31, 2008 | Webmaster-Source

  • brian

    nice and helpful tutorials…

    http://www.livbit.com

  • Pingback: Links of Interest - CSS-Tricks

  • Pingback: Propiedad Privada » Blog Archive » Varios enlaces sobre Desarrollo Web

  • Pingback: » feed expo 2008-11-05 // 702

  • http://tendou86.blogspot.com/ Takumi86

    I have own resource code for making this but there is a big difference with the one you posted so i’m gonna save this onto notepad for future reference, thanks

  • Pingback: How to Add Auto Complete to Your Google Custom Search Engine - NETTUTS | Latest Blog Posts

  • http://idoitforu.com p3rf3ctg3ntl3m4n

    thanks for great tips

  • Steve

    OK – why in the world can’t I add to the $data array using a simple PHP query?

    Take out my query it works fine; pop it back in underneath where you’re manually adding to the array in searchdata.php (in the beginning), and it fails to work.

    I’m pulling my hair out!

  • Steve

    Nevermind! I’m so dumb sometimes – you have to call your connection.php inside searchdata.php if you want to call a query or else it dies.

  • Pingback: Bookmarks for December 29th through January 18th → Stevey.com

  • http://www.blogher.com/haystackprofile/viewprofile/cheap375phentermine Awese
  • Puneet Pandey

    Hi,

    It is a nice tutorial, I am a ruby on rails developer, I am trying to implement the searchdata.php file in ruby, but couple of things has made me stuck at d middle, could you please lemme help!!

    Regards
    Puneet

  • Sohan Sharma

    ur effort is really cool… appreciate ….

  • Ryan

    A simple XML parser for those of us stuck without PHP:
    http://www.dotnetindex.com/articles/8053-Simple-XML-RSS-Parser.asp

  • http://mussiqa.net Girard J

    Nothing less Nothing More full detailed tutorial
    Thanks

  • http://www.arthurmart.com Arthur

    Thank you much for schooling us.. Awesome

  • http://www.ahmed-kamal.com/ Ahmed Kamal

    Thanks buddy, this article was quite helpful for me, go on, we want more…

  • http://theworldisnotflat.com/profile/horsesexvideo hunttibu

    Zombie walk!, dog sex woman , [URL=http://thephoenix.com/COMMUNITY/members/dogsexwoman.aspx]dog sex woman[/URL] , http://thephoenix.com/COMMUNITY/members/dogsexwoman.aspx dog sex woman ,
    free zoo tube sex , [URL=http://www.hairmaxforum.com/forum/member.php?u=18184]free zoo tube sex[/URL] , http://www.hairmaxforum.com/forum/member.php?u=18184 free zoo tube sex ,

  • http://www.nissansilvia.com/forums/index.php?showuser=66305 hunttib

    MilkandCookies!, bachelors degrees , [URL=http://www.nissansilvia.com/forums/index.php?showuser=66305]bachelors degrees[/URL] , http://www.nissansilvia.com/forums/index.php?showuser=66305 bachelors degrees ,
    buy movies online , [URL=http://www.ambrosiasw.com/forums/index.php?showuser=43428]buy movies online[/URL] , http://www.ambrosiasw.com/forums/index.php?showuser=43428 buy movies online ,

  • http://qwqw irfan

    i donot agree for this tutorial….

  • Pingback: How to Add Auto Complete to Your Google Custom Search Engine | CgBaran Tuts

  • http://tuts.cgbaran.com CgBaran Tuts

    nice tutorial thanks

  • http://www.wpdigger.com/ wpdigger

    Quite nice tutorial.Very well written and a great use of the auto complete plug-in, thanks!ery well written and a great use of the auto complete plug-in,

  • http://www.pooble.co.cc POOBLE Globally Ahead – Your Global Search Partner

    well thanks for such a nice tutorial , i ‘m running a search based website. a nd i hope my visitors will find it very useful , thanks to you, can’t we do it using html , in case someone don’t have php web hos,t reply thanks

  • http://www.olivewhite.com Olive White

    Really great script! I recommend it! Thanks.