Despite some nice improvements on the area of search within EE as of late (hello Super Search), for most sites it’s still hard to beat search results based on a crawl of your actual site (as opposed to the contents of its database). Google’s Custom Search Engine is good, but it’s kind of fugly, and requires JavaScript to work.
So, I wrote a new plugin called (drumroll please) Google Search Results that leverages Google’s AJAX Web Search API to load search results for your domain (or your Custom Search Engine) natively in EE.
Simply create a search form with a text input named ‘q’ and submit it via POST or GET to a template on your site containing the Google Search Results tag pair.
NOTE: Currently the Google AJAX Search API limits queries to 8 results per page, with a total of 64 results for any given query.
The README in the download explains all the tag parameters and variables in detail, but here’s a quick example of what your search results template might look like:
{exp:google_search_results site="yoursite.com"}
{if search_results}
You searched for <strong>{keywords}</strong> and got
{total_search_results} {if total_search_results == "1"}result{if:else}results{/if}.
{/if}
{results}
{if count == "1"}<ul>{/if}
<li>
<h3><a href="http://{url}">{title}</a></h3>
{excerpt}
<small>{url} <a href="http://{cached_url}">(cached)</a></small>
</li>
{if count == total_page_results}</ul>{/if}
{/results}
{if no_search_results}
Sorry, no results for <strong>{keywords}</strong>.
{/if}
{if paginate}
Page {page_number} of {total_pages} : {pagination}
{/if}
{if last_results_page && results_overflow}
<a href="http://{google_results_url}">View all {total_google_search_results} results on Google</a>
{/if}
{/exp:google_search_results}This plugin has been tested with EE 1.6.8. An EE 2.0 version will come, I’m sure. If you’re running PHP < 5.2, you’ll need to upload the included JSON.php file to your /system/lib/ directory. Also, cURL support is required!
Download Google Search Results from GitHub (or view the repo)
Currently the Google AJAX Search API limits queries to 8 results per page, with a total of 64 results for any given query.
That’s bad.
Yahoo BOSS Search plugin does not have such limitations.
Haha, well, it’s not suitable for all sites, but for many smaller sites it’s a good fit. And, the future of BOSS search has some uncertainties (like possible fees).
Just to clarify, the 64 result limit is not a limitation of the plugin, but of the official Google API.
Nice work Derek and I’m already using it on a site and it’s working out well except that it appears that it’s returning results from some pages and not others, i.e. there’s two pages where a specific terms exists but only one is being returned. I’ve added the filter=”0” parameter but it hasn’t changed anything.
The plugin is working really well for the most part with index.php removed when I have the form post instead of get and add the remove_query_indicator=”y” to the tag but when I click on pagination links after each link the url gets longer and longer.
For example, on the first click I get
http://site.com/results/&q=query&p=2
but if I click another pagination link I get
http://site.com/results/&q=query&p=2/&q=query&p=4
where the numbers are the pagination links I clicked on. Is anyone else having this problem or is there a solution for this?
Easy to set up and get working, but I’m getting errors from the plugin file when the search returns no results. Here’s what I get:
Notice: Undefined index: HTTP_REFERER in /www/eh13088/public_html/gcsys/plugins/pi.google_search_results.php on line 94
Notice: Undefined property: stdClass::$estimatedResultCount in /www/eh13088/public_html/gcsys/plugins/pi.google_search_results.php on line 124
Notice: Undefined property: stdClass::$pages in /www/eh13088/public_html/gcsys/plugins/pi.google_search_results.php on line 133
Notice: Undefined variable: pagination in /www/eh13088/public_html/gcsys/plugins/pi.google_search_results.php on line 213
Here’s my code:
Any ideas?
The plugin is working really well for the most part with index.php removed when I have the form post instead of get and add the remove_query_indicator=“y” to the tag but when I click on pagination links after each link the url gets longer and longer.
Can you try removing the ‘remove_query_indicator’ argument and see if the same thing happens with your pagination URLs?
Easy to set up and get working, but I’m getting errors from the plugin file when the search returns no results.
Can you try the version posted here and see if that fixes these errors?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.