Searchモジュールの日本語認識について

Events happening in the community are now at Drupal community events on www.drupal.org.
kurupira@groups.drupal.org's picture

お世話になります。
某掲示板でこちらを紹介していただき質問させていただきます。

現在Drupal6.12を使用しています。
Searchモジュールの使用に関して、デフォルトでは検索の際、最低3文字以上の単語を入力するようにと
設定されていますが、日本語では3文字以上の単語と認識されない場合があります。
例:メール、ユーザ、サービス
Drupalを紹介している日本のサイトの検索でも同様の事象が起こるようです。

その他いろいろ試してみましたが、文字数の判定時「ー」を認識せず、検索が走り始めると「ー」が認識される
ような動きをしています。
例:メール ⇒ 「メ」と「ル」と認識 ⇒ 「3文字以上のキーワードを、最低でも1つは入力してください。」
  パフォーマンス ⇒ 「パフォ」と「マンス」と認識 ⇒「パフォーマンス」という単語を検索

解決方法をご存知でしたらお教えいただければ幸いです。
よろしくお願いします。

Comments

phpで

xbro's picture

解決方法になるかわかりませんが、率直に思うのは検索ボタンを押してphpで”ー”を空白などに置き換えるコードが明らかにありますよね。文字数もそれを数える一行がどこかにあるでしょう。phpの検索コードで基本的な作業です。

解決できなかったら教えて下さい、一度コード内で探して見ます(私はDrupal初心者ですが)。

スコット

"ー"の問題は知りませんでした。

qchan's picture

http://あなたのドメイン.com/admin/settings/search

へ行くと検索モジュールの設定ができます。
「索引付けをする単語の最小文字数: 」はデフォルトでは「3」になっていますので、
「2」くらいにしておくと、漢字の熟語はなんとか拾えます。
僕は普段それでしのいでましたが、おっしゃるような「ー」をカットしてしまう現象には気が付きませんでした。
試してみると、ほんとに消えてしまいますね。

最小文字数を「1」にすれば『コード』『メール』などの言葉がもれることはなくなりそうですが、
「コ」「ド」「メ」「ル」が入っているだけの関係ないNodeも一緒に引っ張ってしまいます。
また、searchモジュールのテーブルもレコード数が多くなってしまうので問題ですね。
なにか良い解決さくがあれば、Drupalの日本語環境にとって有益ですね。

以前、MySQLだとIndexが増えすぎて大変なので、PostgreSQLを使えば解決すると
いう話がでていたのですが、詳しくわからずじまいでした。
どなたか解説していただけたらありがたいです。

コード

xbro's picture

私の見てるsearch.moduleのコードと同じであれば下記のコードがありますね。ここでなぜかPREG_CLASS_SEARCH_EXCLUDE のDefine に無いのに「30FC KATAKANA-HIRAGANA PROLONGED SOUND MARK / 仮名の長音 / 長音符号」が引っかかって空白に置き換えられてる可能性が高いと思います。

あまりカッコよくないですが、このコードの前に”ー”を何かありえないPREG_CLASS_SEARCH_EXCLUDEにないString(例えば「#+」などに )に置き換え、下記のコードが実行されたのち、もう一回今度はそれを元通り「ー」に置き換えて解決できるかもしれません。

// $Id: search.module,v 1.250.2.4 2008/09/17 06:42:20 goba Exp $

// With the exception of the rules above, we consider all punctuation,
// marks, spacers, etc, to be a word boundary.
$text = preg_replace('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', ' ', $text);

原因がわからないのは不気味で、私もregex苦手なので、得意な方は理由がわかるかもしれません。とりあえずPREG_CLASS_SEARCH_EXCLUDE には 30FCはありませんね。

Scott

Thread trans :

xbro's picture

Apparently at the search module the unicode character "ー" seems to be filtered out and replaced by keyword separation character " " (space) by the code below before it is queried, when it is not defined at PREG_CLASS_SEARCH_EXCLUDE. Does anyone know why?

$text = preg_replace('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', ' ', $text);

As pointed out by Qchan, this is in fact a major BUG with regards to Drupal in Japanese. Got like any Japanese regex experts around you?

Scott

kuwamura's picture

searchモジュールのインデクス機能の国際化対応オプションは、不十分のようです。おそらく、マルチバイト文字列を3文字ずつ切って、インデックス化しているようですので、それをもとに検索をしようとしてもマッチしないことが、多々あるのではないでしょうか。

これには私も大変悩みましたが、PostgreSQLはTSearch2という全文検索エンジンが内臓されたので期待をしていたところ、昨年くらいにTSearch2の日本語化モジュールのtextsearch_ja が提供されました。

Mediawikiでは、このtsearch対応がされていて日本語の検索も問題なくできるのですが、drupalでは未対応だったのでどうするか思案していたところ、drupal-tsearchモジュールが開発されていることを見つけました。これをsearchモジュールのかわりに使うことで、PostgreSQLのTSearch2エンジンが利用できるようになりました。

http://a98.jugem.jp/?eid=366
http://a98.jugem.jp/?eid=360
http://github.com/mikl/drupal-tsearch/tree/master

MySQLの場合に、これが直接解決となるかどうかは不明ですが、Senaなどの全文検索エンジンとの組み合わせでなんとかなるのかもしれません。

とりあえず、参考までに。

--
J.kuwamura

J.kuwamura

単語分割

kuwamura's picture

書き忘れましたが、日本語のインデックス化の前に単語に分割する必要があります。欧米の文章では、スペースで単語が区切られていますが、日本語では区切られていません。これを行うために、分かち書きという「てにをは」によって単純に分割する方法(kakasi等)、文法的に形態素解析をする方法(chasen, mecab等)、そして、決められた語数で分割するngram方式(sena等)があるようです。textsearch_ja はmecabとsenaに対応しているようです。

http://textsearch-ja.projects.postgresql.org/index-ja.html

Tritonnプロジェクトでは、MySQLとSenaまたはMecabで全文検索を行う試みを行っているようです。
http://qwik.jp/tritonn/

--
J.kuwamura

J.kuwamura

ー is included in the

Garrett Albright's picture

ー is included in the regular expression.
Regular expression が「ー」を持っていますよ。

ー is Unicode character 30FC.

The definition of PREG_CLASS_SEARCH_EXCLUDE has… (search.module, line 45)

'\x{3099}-\x{309e}\x{30a0}\x{30fb}-\x{30fe}\x{3190}-\x{319f}\x{31c0}-\x{31cf}'

The bold part means "Unicode characters 30FB through 30FE."

I'll try making and submitting a patch for search.module to fix this.

Here's the issue with the

Garrett Albright's picture

Here's the issue with the patch. Patchはココです。.

Note that you'll have to rebuild the search index after applying the patch in order for it to work correctly on nodes that have already been created. Go to Administer > Site configuration > Search settings and click the "Re-index site" button, then run cron until the "Indexing status" reads "100% of the site has been indexed" again. (If someone could translate this, it'd be appreciated - it's kind of important. I'd make another comic thing, but the last one took a long time to make, actually…)

awesome!

xbro's picture

we should contact Drupal.jp some day..

すごい。

qchan's picture

解決策がもう見つかって、patchファイルもdrupal.orgにアップされるんですね。

scott
英語で伝えてくれてありがとう。PREG_CLASS_SEARCH_EXCLUDEが怪しいというあなたの推測当たっていましたね。

9j8
解説ありがとうございます。
MySQLの今後がいまいち不明なので、PostgreSQLを使ってみたいと思ってます。
またいろいろ教えてください。

Garrett
Cool!
When this problem is recovered, it is easy to come to use Drupal in Japanese very much.
Thank you.

PREG_CLASS_SEARCH_EXCLUDE

kurupira@groups.drupal.org's picture

皆様ありがとうございます。

search.module内PREG_CLASS_SEARCH_EXCLUDE内の\x{30fb}-\x{30fe}を
\x{30fc}-\x{30fe}に書き換え。

また、qchanさんのおっしゃる通り「索引付けをする単語の最小文字数: 」を2に設定することで
漢字の熟語対策を施しました。

ここまでを暫定処置として、patchをアップしていただけるということなので、patch適用をもって処置完了と
したいと考えています。

ご協力ありがとうございました。

re-index

xbro's picture

Garrett君からpatchを適用した後、サイトのインデックスを作り直す必要があるとの事です。

Search Index作成時のメモ

qchan's picture

Garrettが作ってくれたPatchファイルを当てたらキレイにカタカナワードが検索できるようになりました。
素晴らしい!
「メール」がちゃんとヒットします。

僕の環境だけかもしれないですが、search.moduleを新しくする際に
ちょっととまどった点があるのでメモしておきます。

admin/settings/search

[Indexing status] >[Re-Index site]ボタンを押します。
すると、「0% of the site has been indexed. 」と表示されます。
この後、Cronが回る度にサイトの文字列がIndexされていきますが
手動でCronを回してもIndex数が増えないことがあります。
そのときは、
admin/settings/performance
の一番下にある「Clear cached data」でキャッシュをクリアしてみてください。
(Admin Menuモジュールの左上にある「Flush all caches」でもよいです)

patchを当てられない環境の方用にsearch.moduleファイルを添付しました。
ファイル名から.txt拡張子を外してから、
drupalインストールフォルダ/modules/search/search.module 
と入れ替えてください。

Update時に注意が必要なので、次回リリースから本体に始めから入ってくれるとよいですね。
(これは日本語版にという意味ではなく、通常のdrupal.orgが配布しているものにという意味です。
UTFなのでこのファイルは共通ですから)

どこかで掲示? 発表 Should we announce?

xbro's picture

この情報ってJapanグループのどこかでちゃんと掲示するべきでしょうか?そうでしたら場所は解りませんが任せます(stickies section?)。should we put this information as some sort of permanent announcement at the Japan Group site? How could we do this, at the stickies section?

Drupal.jp と j-box, drupal.orgにも掲示したいですよね。Drupal.jpもしくはDrupal.orgに言えば本体に入る可能性高くなりますかね? 言わなかったら、誰も知らない事にはなりませんかね?Would it be necessary for us to notify drupal.jp, j-box group and drupal.org with regards to this? Is there a chance that if we don't make some noise not many will be informed?

私としてはPatchはこういうやり方での事とか、SearchモジュールがIndex(こないだ手作りしてました)してるなんて知らなかったのでいい経験になりました。For me, since I didn't know much about patches, of which I'd like to find out how to make them and install them, and didn't know about the indexing features of Drupal, this was a fantastic experience.

Best regards,

Scott

p.s. この事を挙げてくれたSasakiさん、解決、Patchを作ってくれたGarrett君とみんなに乾杯!
We all deserve a beer for this, Cheers!

The patch should first be

Antoine Lafontaine's picture

The patch should first be sent back the the module's bug queue. (I think Garret mentioned he would do it... sorry if I assume wrong)

If we need to have a sticky about this, let's make it a wiki page with only the needed explanation + patch (and link to how to patch... or just a file like qchan posted) until the patch is committed to the core module. And lets point to this thread from the wiki page if people want to read/add comments.

上記の p.s. 訂正 & Dries

xbro's picture

この問題を挙げてくれたのは kurupiraさんですね、上記コメント訂正できないようなのでこのコメントで訂正します。

patchのページ行ってきたら、Garrett君ってひょっとしたら僕より日本語の細かい事知ってそうで、日本語のWordがほとんど母音で終わるのは知らず、よく説明してるなと思いました。Garrett, you really explained it good at the patch page, and I was a little surprised at the Japanese language facts you noted as I had no idea.

ちなみに Dries という人からコメントがPatchに対してありました。
I think we may have created enough noise at Drupal.org if someone by the name Dries commented on the patch, fyi.

D5で使わせていただきました

bkenro's picture

Drupal 5.18 を使用しています。
D6 用のパッチをそのまま適用して問題解決しました。すばらしい!!
とても助かりました。本当にありがとうございます!

someone named Dries

Shin-gdo_J's picture

Hmmmm..... :-)

僕は天皇陛下から直々にお言葉を賜り、いたく感激したことがあります。
#嘘。
そ、そんな感じ?
とにかく、/user/1 って見て「おおおっ!」っと(笑)

しかし、お忙しいだろうによくこまごまと見てるもんだなぁ、と本当に感心というか感動。
でも、そういうのが本当に能力のある人なんでしょうねぇ。

Shin@ひとりごともーど
//なんとか iPhone 3GS 正式予約。
//ケースに入れようかどうしようか考えちう。入れて“Drupalマーク”貼ろうかなw 赤バージョンのヤツ作って。
//でも、街中で見られても反応はよくて「???」、最悪「(なんか気持ち悪ぅ)」だけだったりして(^^;;

Thank you!

aiwata55's picture

Thank you kurupita for raising this issue and Garrett for making a patch for this very quickly.


Aki Iwata
FOREST & trees

The patch wasn't accepted

Garrett Albright's picture

The patch wasn't accepted into the Drupal 6 codebase (yet?), so after upgrading to Drupal 6.13, you'll need to reapply the patch.

Drupal 6.13のアップデートするの後で、もう一度パッチを使って下さい。

Good news! Dries has accepted

Garrett Albright's picture

Good news! Dries has accepted the patch into the Drupal 7 codebase. If you download the current HEAD with CVS, the patch is in there. Yay!

Still lobbying to get it into Drupal 6 too… Please head over to the issue and try out the Drupal 6 patch, and if it works for you, please voice your support in the issue for getting it included.

パッチはDrupal 7に入りました!

素晴らしい!

qchan's picture

やりましたね!
早くD7使いたいです。
まだまだD6が現役ですので、こちらにも入って欲しいですね。

すごい!!Fantastic news. Thanks

setvik's picture

すごい!!Fantastic news. Thanks so much for the patch.

+1 for getting this into Drupal 6

お尋ねします。

alitoshi's picture

パッチを宛てるとは具体的にどのような手順を踏めばよいのでしょうか?
色々調べましたが、方法が分からなくて。。

qchanさまの添付されたファイルをダウンロードできれば一番よいのですが。。

よろしくお願いいたします。

qchan's picture

あれ? 添付が消えてますね。
g.d.oの仕様が変わったのかもしれません。

MacOSXやLinuxなどの環境でははじめからコマンドが使えるのですが、
Winodows環境だとちょっと手順が必要のようです。
下のリンク先が参考になるとよいのですが。

diff/patch Tips
http://www.sip.eee.yamaguchi-u.ac.jp/kou/diff-patch.html

patch(Windowsで動くアプリにしたもの)
http://www.vector.co.jp/soft/win95/prog/se015025.html

小粋空間:パッチをあてる
http://www.koikikukan.com/archives/2006/01/22-235056.php

*スレッドを立ち上げるときにはファイル添付が出来るようですので、
新スレッドを立てることも考えてみます。

alitoshi's picture

qchanさま

早速コメントありがとうございます。
教えていただいたサイトは非常に参考になりました。

ただ、私はレンタルサーバにインストールしてるので、これらの方法では難しそうですね。
引き続き探してみます。

alitoshi's picture

qchanさま

早速コメントありがとうございます。
教えていただいたサイトは非常に参考になりました。

ただ、私はレンタルサーバにインストールしてるので、これらの方法では難しそうですね。
引き続き探してみます。

qchan's picture

Localのご自分のPCでパッチをあててから、レンタルサーバーにアップして置き換えればよいですよ。
/modules/search/search.module
を置き換えます。

あ!なるほど!!

alitoshi's picture

確かにそうですね。
そのような発想にいたりませんでした。
早速やってみます。
ありがとうございました!

Just when I had forgotten

Garrett Albright's picture

Just when I had forgotten about this… It has finally been committed to Drupal 6!

次のDrupal 6リリースはこのパッチを入りますよ!

日本 コミュニティ: Drupal Japan User Group

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: