Drupal6の標準のSearchモジュールで提供されるサイト内検索は日本語で検索した場合にうまくいかない部分がいくつかあります。ここではその解消方法を紹介します。
何が問題なの?
Drupal6の標準のサイト検索は英語などの単語をスペースで区切る言語の場合は良いのですが日本語のように単語の区切りが明快でない言語の場合にうまくいかない部分があります。一番大きな問題はうまくANDやOR、NOTなどの論理演算条件に従った検索ができない点ですが、これはDrupalの標準のサイト検索ではどうにもなりません。サイト内検索にGoogleなどより高度な言語解析処理のできる検索を利用したり、その他Namazuなどの日本語に対応した全文検索エンジンを利用するしかないかと思いますが、この記事ではそれは対象外としています。あくまで簡単にできる範囲で現在の日本語での検索のうまくない部分を直したいと思います。修正前
修正後
この上の2つを比べてもらうとわかるかと思いますが、以下の2点を今回修正します。
問題1)検索結果のページ説明文がちゃんと表示されないで「... ...」だけになってしまう場合がある。
標準のSearchモジュールのコードを詳しく解析しないとはっきりとはわかりませんがこのモジュールのsearch_excerpt()関数が日本語の場合うまく動作しないことがあるからではないでしょうか。特定の記事でこの現象が発生するようです。
問題2)検索結果のページ説明文の中で検索キーワードがハイライト表示(<strong>タグによる)されない。
これはスペースで区切られた言語でのみうまくハイライト表示されるようなコードになっているからです。
解決法
リサーチしたり標準モジュールのコードを解析した結果、これらを解決する方法をモジュール化することは難しいという結論に達しました。これはうまいhook関数やテンプレート関数が実装されていないことによります。もちろん、Drupalのコアにパッチをあてる方法もありますが、これもできれば避けたいので、Searchモジュールの結果表示に使用するテンプレートを利用してなんとかすることにしました。以下がそのテンプレートファイルです。
- <?php
- // $Id: search-result.tpl.php,v 1.1.2.1 2008/08/28 08:21:44 dries Exp $
- /**
- * @file search-result.tpl.php
- * Default theme implementation for displaying a single search result.
- *
- * This template renders a single search result and is collected into
- * search-results.tpl.php. This and the parent template are
- * dependent to one another sharing the markup for definition lists.
- *
- * Available variables:
- * - $url: URL of the result.
- * - $title: Title of the result.
- * - $snippet: A small preview of the result. Does not apply to user searches.
- * - $info: String of all the meta information ready for print. Does not apply
- * to user searches.
- * - $info_split: Contains same data as $info, split into a keyed array.
- * - $type: The type of search, e.g., "node" or "user".
- *
- * Default keys within $info_split:
- * - $info_split['type']: Node type.
- * - $info_split['user']: Author of the node linked to users profile. Depends
- * on permission.
- * - $info_split['date']: Last update of the node. Short formatted.
- * - $info_split['comment']: Number of comments output as "% comments", %
- * being the count. Depends on comment.module.
- * - $info_split['upload']: Number of attachments output as "% attachments", %
- * being the count. Depends on upload.module.
- *
- * Since $info_split is keyed, a direct print of the item is possible.
- * This array does not apply to user searches so it is recommended to check
- * for their existance before printing. The default keys of 'type', 'user' and
- * 'date' always exist for node searches. Modules may provide other data.
- *
- * <?php if (isset($info_split['comment'])) : ?>
- * <span class="info-comment">
- * <?php print $info_split['comment']; ?>
- * </span>
- * <?php endif; ?>
- *
- * To check for all available data within $info_split, use the code below.
- *
- * <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
- *
- * @see template_preprocess_search_result()
- */
- ?>
- <?php
- // The code below is a walkaround of the issue that
- // search_excerpt() in search.module fails to create snippet
- //
- // To Do: create alternative of search_excerpt() for multibyte languages
- //
- // get node
- $node = $result['node'];
- // create digest
- $digest = $node->teaser_include ? $node->teaser . $node->body : $node->body;
- // strip HTML tags
- // truncate to 200 characters
- }
- else {
- $digest = $snippet;
- }
- // get search keywords
- $keys = search_get_keys();
- // cleanup seach keywords
- }
- // highlight the search keywords
- foreach($keys_array as $key) {
- }
- ?>
- <dt class="title">
- </dt>
- <dd>
- <?php if ($snippet) : ?>
- <?php endif; ?>
- <?php if ($info) : ?>
- <?php endif; ?>
- </dd>
このファイル「search-result.tpl.php」はDrupal 6.14の標準モジュールであるSearchモジュールのディレクトリの中に含まれているものを自分の使っているテーマのディレクトリにコピーしてきて修正をくわえたものです。このサンプルでは説明文(ダイジェスト)の長さを200文字にしていますが自分の好きな長さにしてください。
以下の添付ファイルはこのsearch-result.tpl.phpをzip圧縮したものです。使われているテーマファイルのディレクトリにコピーし、Searchモジュールを一度無効にしてから有効にしてやる必要があります。
このsearch-result.tpl.phpの最後の部分を自分で手を加えることで、検索結果の表示をカストマイズすることができます。当サイトでもちょっとだけいじっています。
このテンプレートでは検索結果の説明文が「... ...」になってしまう場合に、無条件でノードの先頭から200文字を切り出しています。ですので、その中に検索キーワードが含まれない可能性があります。
日本語の処理に問題があるsearch_excerpt()関数の代換関数を作成すれば良いのですが時間がなくそこまで手がまわりませんので今のところは応急処置ですませてしまっています。
日本語の処理に問題があるsearch_excerpt()関数の代換関数を作成すれば良いのですが時間がなくそこまで手がまわりませんので今のところは応急処置ですませてしまっています。
| 添付 | サイズ |
|---|---|
| search-result.tpl_.zip | 1.5 KB |

