Learning Movable Type: MT Keyword Search


Movable Type comes with a default search capability that your site visitors can use to search for keywords in your weblog entries. You can add functionality to the search form that people see on your site, and make adjustments to the templates that govern how the results are displayed.

Keyword Search Templates

Suppose you want to change the way the search results look when someone uses the default MT search function to do a search on your website.

Movable Type's templates that govern the MT keyword search results pages operate differently than the other weblog templates. (You may have noticed this if you have changed the name of your default weblog stylesheet away from styles-site.css.) There is one default keyword search template that governs the search results pages for all of the weblogs on one MT installation. The template is located in a folder called "search_templates", located with your MT cgi files on your server.

That default search template looks for a file called "styles-site.css" associated with each weblog to serve up the search result pages for each of the weblogs. If it finds the file, it serves up a search results page that is in keeping with the style of the weblog from which the search is placed (assuming the weblog is using the style-site.css as its stylesheet). If it doesn't find the file, then a completely unformatted, unstyled search results page is what is delivered.

To make make a global change to the search result page that affects all of your weblogs (assuming you have more than one) you can download the file from your server, edit the HTML in the search_templates/default.tmpl with a text editor, and then reload the file, setting the permissions to 644 (see FTP, File Formats, and Permissions). For example, you may want to remove or rename the Match Case and Regex Search check boxes that appear with your search results. (To do that, see the instructions in the What is a Regex Search? tutorial.)

If you have multiple blogs, to make a change to the search results page for only one of your weblogs, you can create an alternate search template. To do this,

1. Copy and paste the default search template from search_templates/default.tmpl to a blank page using a text editor. Make the changes you want, for example, pointing the template to a different stylesheet other than styles-site.css. Save the file with a new name, load it to your sever into the search_templates folder, and set the permissions to 644. You may encounter difficulty saving a file with a .tmpl extension on your desktop (I do on my Mac!). In this case you can save it as a .txt file, upload it using your FTP program, and then change the name of it on the server.

2. Add the alternative templates to your MT configuration. Again you do this not through the web, but by editing the mt.cfg file which is on your server with the MT cgi files. Scroll down around 4/5 of the way through that file until you find

# AltTemplate work work.tmpl
# AltTemplate play play.tmpl
Add a line with the # removed that looks uses your file name in place of either "work" or "play". For example, if your alternate template is named linksearch.tmpl, then the line would look like:
AltTemplate linksearch linksearch.tmpl
Save the new mt.cfg file and reload to your server. Set the permissions to 755.

3. Make changes to your search form. On the search form that you have in your Main Index sidebar, you need to add a "hidden" input type, directing the search form to your new alternate template. To do so for your "linksearch" template, add the following code to the form:

<input type="hidden" name="Template" value="linksearch" />
Save and rebuild your index template.

The search results page offers your site visitors another opportunity to search your site. This search form searches all of your weblogs if you have more than one. If you would like this search form to only search the originating weblog, add the following line to the search form section on the Alternate template:

<input type="hidden" name="IncludeBlogs" value="your_blog_ID_goes_here" />

If you are unsure of your blog ID, open a weblog edit window for that particular blog in MT and look in the browser URL window. You'll see it identified.

Search Form

The Movable Type Users Manual gives a good overview of the different search form options for your weblog visitors here. All you have to do is add a few line of code to the search form in your sidebar (in the Main Index and Archive templates) and your visitors can search entries, comments, or both. They can search by date authored, entry title, and a range of dates. There are other options as well. See the MT documentation for the specific code. Arvind at Movalog does a good job of explaining the search options in Tweeking Searches.


Searching Multiple Blogs at Once

With the MT search form you can search two (or more) of your weblogs (for example your main website blog and your sideblog) at the same time and have search results pull from both blogs. To do this, add the following lines to your search form code:

<input type="hidden" name="IncludeBlogs" value="1" />
<input type="hidden" name="IncludeBlogs" value="3" />

Replace the value numbers with the blog IDs of the two blogs that you want to search. In this example, the search form would search blogs with the IDs 1 and 3. To see what blog ID your blog is, while in the MT edit window for the blog, look at the URL in the browser you are using. If your search form includes the following line:

<input type="hidden" name="IncludeBlogs" value="<$MTBlogID$>" />

replace the line with the code naming the actual blog IDs.


Limiting Search to Specific Blogs, on a Global Basis

If there are certain blogs on your installation that you never want included in your search results, find the following section in your mt-config.cgi (or mt.cfg):

# To restrict the blogs included in a search on your site, you can use the
# IncludeBlogs and ExcludeBlogs settings. IncludeBlogs lists the blogs that
# will be included in the search, and ExcludeBlogs lists blogs that will be
# excluded from the search. Do not try to use both--IncludeBlogs will
# override ExcludeBlogs. The default is to search all blogs. Separate blog
# IDs with commas.

# ExcludeBlogs 6,7,8
# IncludeBlogs 2

Put the blog numbers in that you want to exclude, and uncomment that line. Or, on the IncludeBlogs line put the blog numbers that you want to include and uncomment that line. Don't uncomment both lines, only do one or the other.

Save and upload the mt-confgi.cgi file back to your server. Remember to set the permissions of this file to 644.


Google Search

You can add a Google Search bar to your Index or Archive pages or you can even replace the MT search form on the MT search result page with a Google Search bar. By adding Google search to your search results page you can give your visitors the option of using the Google search engine to search your site if they haven't found what they needed with the MT Search function. Go to http://www.google.com/searchcode.html to get the Google search code.


Links:
MT Manual on Public Search
Tweaking Searches - by Arvind at Movalog
Using Keywords and tags to help visitors find related content - from Al-Muhajabah
More Fun with Keywords and Tags - also from Al-Muhajabah
Fast Search - a php search plugin by Mark Carey


Posted by Elise Bauer on September 2, 2004 2:36 AM to Learning Movable Type http://www.learningmovabletype.com/