Chapter 10: Search
Setting Up Google Enabled Entries
Problem
You want to integrate Google search results into your entry.
Solution
Provide Movable Type with your Google API Key and use the
MTGoogleSearchResult template tag.
Discussion
Movable Type is capable of making Google search queries and inserting the results into site layouts. This built-in functionality is geared toward inserting related links into an entry or a specific topic such as an author's name rather than providing unfettered access to the full power of the Google search engine. This is more a limitation of the Google API than anything related to MT's implementation.
This functionality requires that your system has the
SOAP::Lite and LWP::UserAgent modules from CPAN.
They also require a Google API key be obtained and configured to activate
each weblog. Doing so is relatively straightforward process.
- 1. To obtain a Google API key go to http://www.google.com/apis/ to register for an account. Once registered you will receive a license key from Google that is good for 1000 queries a day.
- 2. Go into the Movable Type CMS and click the Edit Configuration link next to the weblog you want to enable or, within the weblog, click the Settings button in the Weblog Toolbar.
- 3. Under the General tab of the Settings screen enter your Google API Key and then press Save Changes.
The Google search features of this weblog have been enabled.
You can reuse your Google API key in multiple weblogs. Each weblog's usage will count towards the key's daily quota.
Integrating Google results into Movable Type is achieved through two
action packed tags -- MTGoogleSearch and
MTGoogleSearchResult.
MTGoogleSearch creates the search context by using the
Google API to obtain results for a particular query or for pages that are
similar to a particular entry. Once retrieved, the contents of this tag are
looped through for each result. The type and parameters of the search are
determined by the following supported attributes.
queryIssues a specific search to Google.
relatedReturn results that are similar to a certain page. The value of this parameter can be a URL or 1. Passing a value of 1 will cause your Site URL to be used. (You should have set your Site URL in your Weblog configuration -- if not you'll have to do so to make use of this option.)
resultsAn integer between 1 and 10 specifying the number of search results to insert.
titleTakes a boolean value. If set to 1 (true), the title of the entry in context will be used as the query.
excerptTakes a boolean value. If set to 1 (true), the excerpt of the entry in context will be used as the query.
keywordsTakes a boolean value. If set to 1 (true), the keywords of the entry in context will be used as the query.
NOTE: query, related, title, and
excerpt are mutually exclusive of each other.
MTGoogleSearchResult represents a field of an individual
Google search result. This tag is used in the context of a
MTGoogleSearch tag.
The tag recognizes a single attribute of property that
specifies the field of the search result to be displayed. Values for
property can be: "summary," "URL," "snippet," and "title." The
default property is title.
Let's look at some example uses of these tags:
To list the top five search results for the query "movable type," insert this template code:
<MTGoogleSearch query="movable type" results="5">
<a href="<$MTGoogleSearchResult property="URL"$>">
<$MTGoogleSearchResult property="title"$></a><br />
</MTGoogleSearch>
Alternatively, to insert a list of related links based on a site's home page, insert this template code:
<MTGoogleSearch related="1">
<a href="<$MTGoogleSearchResult property="URL"$>">
<$MTGoogleSearchResult property="title"$></a><br />
</MTGoogleSearch>
Google search results on a per entry basis can be inserted into an Individual Search Archive template like so:
<MTGoogleSearch title="1" results="3">
<p><a href="<$MTGoogleSearchResult property="URL"$>">
<$MTGoogleSearchResult property="title"$></a><br />
<$MTGoogleSearchResult property="snippet"$></p>
</MTGoogleSearch>
This of course assumes that entry titles are sufficiently descriptive to return relative results from Google.
Impact on Performance and Other Limitations
If you use the Google search tag in your Individual Archive Template, a call will be made to Google for each entry. This will significantly impact the speed of the rebuilding process. Depending on the number of posts in the weblog or how often entries are rebuilt, perhaps to display a new comment, the daily quota of Google permits could be exceeded.



