Creating a Google Suggest-Style Search


Published on January 9, 2005 at 12:21 PM EST
Last updated on October 9, 2006 at 12:02 AM EST
In the Tutorials category.

Google Suggest offers suggestions while you’re typing in a search term. If you don’t know exactly what you’re looking for or how to spell it, the Suggest feature can make finding what you want easier. I’ve added the same…

suggest.gif

Google Suggest offers suggestions while you’re typing in a search term. If you don’t know exactly what you’re looking for or how to spell it, the Suggest feature can make finding what you want easier. I’ve added the same functionality to danandsherree.com. Try it—start typing something in the search box at the right and watch the suggestions appear! It was a pretty easy feature to institute.

In an amazing piece of coincidence, Arvind at Movalog has just announced Suggest Search. For better or worse, his approach works a bit differently than mine.

If you want to use my incarnation of Suggest Search you’ll first need a list of suggested words. I used the <MTEntryTitle> field to generate all of my search terms, but any field(s) could be used easily enough. Create a new Index Template named search_suggestions.php with the following code in it:

<?php

// Collect all the words to build the suggestion list out of.
$string = strtolower( '<MTEntries lastn="10000"><MTEntryTitle remove_html="1" encode_php="q">
</MTEntries>' );

// Separate phrases (titles) into suggestion words.
$rawkeywords = preg_split('/\s*[\s+\.|\?|,|(|)|\-+|\'|\"|=|;|×|\$|\/|:|{|}]\s*/i', $string);

// Remove duplicates (including uc/lc).
$keywords = array_unique ( $rawkeywords );

// Sort them alphabettically.
sort( $keywords );

//Build the JavaScript array.
echo 'var customarray = new Array( ';

foreach ($keywords as $value) {
  echo "'$value', "; }

echo "'' );";

?>

To do the real work, I used the JavaScript AutoComplete code found at The Code Project. Download actb.js, then upload to your site.

Replace your existing search form with this one, being sure the file pathnames (bolded) correctly point to your site.

<script language="javascript" type="text/javascript" src="/actb.js"></script>
<script language="javascript" type="text/javascript" src="/search_suggestions.php"></script>
<form method="get" action="<$MTCGIPath$><$MTSearchScript$>">
<input type="hidden" name="IncludeBlogs" value="<$MTBlogID$>" />
<label for="search" accesskey="4">Search this site:</label><br />
<input id="search" name="search" size="20" onfocus="actb(this,event,customarray);" autocomplete="off" /><br />
<input type="submit" value="Search" />
</form>

If everything went well, you should have a Google Suggest-style Search box now!

This article is tagged as: Template Tags, Tutorials

If you found this article useful, please consider supporting this site through a donation.

Comments

So far, there are 3 comments and Trackbacks on this entry. Add yours!

1

Uma novo jeito de fazer aplicações na Web está ficando popular e ameaça tomar o lugar do Flash e suas Rich Internet Applications.

”Antigo
2

I’ve tweaked the search box on the top of each page so that, when you begin typing, you will see…

3

Thank you for your amazing tutorial!!! You don’t understand how long I’ve been searching for a tutorial which does the Suggest feature and picks out keywords in the middle of a word. Simply amazing. Keep up the great work! (I still can’t believe this site was only on page 7 of my Google search).

Anyways, the tutorial was a bit confusing on how to enter searchable entries. I believe they should be inserted between the and tag. I seperated mine by commas and it worked fine. Make sure to upload all your files to a server before trying this out.

Thanks again!

Post a comment

 
 
 


TrackBack URL for Creating a Google Suggest-Style Search:
http://www.eatdrinksleepmovabletype.com/cgi-bin/mt/mt-tb.cgi/227