Limiting Search To Specific Fields 1.0

May 31, 2003 - updated February 8, 2006

MovableType allows you to search for entries, comments, etc. but does not allow you to search specific parts of entries, i.e. just the Title or the Keywords. This hack enables you to specify which entry parts you want to search.

lib/MT/App/Search.pm Modification

This modification allows you to specify which parts of an entry are searched.

  1. Open the file Search.pm in the lib/MT/App directory.
  2. Find the section that says:
    sub _search_hit {
        my($app, $entry) = @_;
        my @text_elements;
        if ($app->{searchparam}{SearchElement} ne 'comments') {
            @text_elements = ($entry->title, $entry->text, $entry->text_more,
                              $entry->keywords);
        }
        if ($app->{searchparam}{SearchElement} ne 'entries') {
            my $comments = $entry->comments;
            for my $comment (@$comments) {
                push @text_elements, $comment->text, $comment->author,
                                     $comment->url;
            }
        }
        return 1 if $app->is_a_match(join("\n", map $_ || '', @text_elements));
    }
    
  3. Replace it with the following code:
    sub _search_hit {
        my($app, $entry) = @_;
        my @text_elements;
        if ($app->{searchparam}{SearchElement} ne 'comments') {
            if(my @fields = $app->{query}->param('SearchField')) {
                foreach my $field (@fields) {
                    push @text_elements, $entry->$field;
                }
            }
            @text_elements = ($entry->title, $entry->text, $entry->text_more,
                              $entry->keywords) unless @text_elements;
        }
        if ($app->{searchparam}{SearchElement} ne 'entries') {
            my $comments = $entry->comments;
            for my $comment (@$comments) {
                push @text_elements, $comment->text, $comment->author,
                                     $comment->url;
            }
        }
        return 1 if $app->is_a_match(join("\n", map $_ || '', @text_elements));
    }
    
  4. Modifying your Search Form

    After you modify the Search.pm file, you can specify which parts of an entry are search by modifying your search form to include SearchField controls that specify which fields are to be seached. Valid SearchField values are title, text text_more and keywords.

    To specify that only entry titles are searched, add the following to your search form:

    <input type="hidden" name="SearchField" value="title">
    

    If you want to let the user choose which fields to seach, you could use check box controls (if the users leaves all unchecked, every field in each entry will be searched):

    <input type="checkbox" name="SearchField" value="title" /> Search Titles
    <input type="checkbox" name="SearchField" value="text" /> Search Text
    <input type="checkbox" name="SearchField" value="text_more" /> Search Extended Text
    <input type="checkbox" name="SearchField" value="keywords" /> Search Keywords