MySQL には全文検索機能が付いている。だが、ラテン語のようにスペースがないと語句の区切りを認識しないため、そのままでは日本語が検索できない。
MySQLで全文検索 - FULLTEXTインデックスの基礎知識|blog|たたみラボ
を参考に構築してみた。
データベースの文字コードは UTF-8 で、最低検索語の指定は1が本当はよいとは思うが少しでも節約したいので、2とした。
my.cnf [mysqld] ft_min_word_len=2 まず必要なのは、日本語の語句分解なのだが、形態素解析ではなく N-gram により分解する。但し、日本語(ひらがな、カタカナ、漢字)、英語(アルファベット、数字)への分解をまずすることで無駄なインデックス作成を抑制するようにしてみた。日本語に関しては N-gram を用いて、英語に関しては、キャメル記法に対しては、分解するようにした。
myindex.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 <?php mb\_internal\_encoding("UTF-8"); mb\_regex\_encoding("UTF-8"); class FullTextSearcher { var $markwords; var $scorequery; public function FullTextSearcher() { } public function search_sql($statement) { return "'". $this->to_ngram($statement, 2) ."' IN BOOLEAN MODE"; } public function getScore() { return "'".$this->scorequery."'"; } public function getMarkupWords() { return $this->markwords; } private function to_ngram($string, $n){ $string = mb\_ereg\_replace("^(\\s| )+","", $string); $string = mb\_ereg\_replace("(\\s| )+$","", $string); $str_array = preg_split("/(\\s| )+/", $string); $result = array(); $markwords = array(); $scores = array(); foreach ($str_array as $str){ if ('-' != substr($str, 0, 1)) { $markwords\[\] = $str; } $r = $this->to\_ngram\_query($str, $n); $result\[\] = $r\[0\]; $scores\[\] = $r\[1\]; } $this->markwords = $markwords; $this->scorequery = join(' ', $scores); return join(' ', $result); } private function to\_ngram\_query($string, $n){ $string = trim($string); if ($string == ''){ return ''; } if ('-' != substr($string, 0, 1)) { $plus = true; } else { $plus = false; $string = substr($string, 1); } $length = mb_strlen($string); if ($length < $n){ if ($plus) { return array("+".$string."*", $string); } else { return array("-".$string."*", ""); } } $ngrams = array(); $scores = array(); $wa = new WordsAnalyzer(); $wa->loadStr($string); $wds = $wa->get_indexes(); foreach ($wds as $ngram){ if ($plus) { $ngrams\[\] = "+" . $ngram; $scores\[\] = $ngram; } else { $ngrams\[\] = "-" . $ngram; } } return array(join(' ', $ngrams), join(' ', $scores)); } } class WordsAnalyzer { private $words; public function WordsAnalyzer() { } public function get_indexes() { return array_keys($this->words); } public function get_fulltext() { return join(' ', array_keys($this->words)); } public function loadStr($str) { $this->words = array(); ereg_replace(13, "", $str); ereg_replace(10, " ", $str); $this->analyze_token($this->split_token($str)); } private function split_token($str) { $token = array(); while (1) { $bytes = mb_ereg("\[一-龠\]+|\[ぁ-ん\]+|\[ァ-ヴー\]+|\[a-zA-Z0-9\]+|\[a-zA-Z0-9\]+", $str, $match); if ($bytes == FALSE) break; $match = $match\[0\]; array_push($token, $match); $pos = strpos($str, $match); $str = substr($str, $pos+$bytes); } return $token; } private function analyze_token($token) { for ($i = 0, $len = count($token); $i < $len; $i++) { $word = $token\[$i\]; if (mb_ereg("\[一-龠\]", $word, $match) != FALSE) { if (mb_strlen($word) > 1) { $this->japanese($word); } else { $this->addword($word); } } else if (mb_ereg("\[ぁ-ん\]", $word, $match) != FALSE) { if (mb_strlen($word) > 1) { $this->hiragana($word); } } else if (mb_ereg("\[ァ-ヴー\]", $word, $match) != FALSE) { if (mb_strlen($word) > 1) { $this->japanese($word); } } else if (mb_ereg("\[a-zA-Z0-9\]", $word, $match) != FALSE) { if (strlen($word) > 0) { // camel-gram $this->english($word); } } else if (mb_ereg("\[a-zA-Z0-9\]", $word, $match) != FALSE) { if (mb_strlen($word) > 0) { // camel-gram $word = mb\_convert\_kana($word, 'rn'); $this->english($word); } } else { $this->addword($word); } } } private function addword($word) { if (strlen($word) == 0) return; $this->words{$word} = true; } private function japanese($str) { // bi-gram $ret = $this->split_ngram($str, 2); foreach ($ret as $key => $value) { if (mb_strlen($value) > 1) { $this->addword($value); } } } private function hiragana($str) { // bi-gram $ret = $this->split_ngram($str, 2); foreach ($ret as $key => $value) { if (mb_strlen($value) > 1) { if (mb_ereg("っ.|を|です|ます|する|ある|いる|から", $value, $match) != FALSE) { } else { $this->addword($value); } } } } private function english($str) { $this->addword($str); // そのままも登録 // キャメル記法を分解して登録 while(1){ $r = ereg("(\[a-z\]|\[A-z\]|\[0-9\])\[a-z\]+", $str, $match); if ($r == FALSE) { break; } $match = $match\[0\]; $this->addword($match); $pos = strpos($str, $match); $str = substr($str, $pos + $r); } } private function split_ngram($string, $n){ $ngrams = array(); $string = trim($string); if ($string == '') return ''; for ($i = 0, $len = mb_strlen($string); $i < $len; $i++) { $ngrams\[\] = mb_substr($string, $i, $n); } return $ngrams; } } ?>