elasticsearchのセットアップ
elasticsearch自体のセットアップをするためにscript/setupを呼ぶとそのディレクトリのelasticsearch/以下にセットアップしてくれるようにしてみた。内部的にはelasticsearchのtarを取ってきて展開しているのと、pluginを入れてみているだけ。
carton exec -- perl script/setup
今回はそれぞれのページのidとタイトルと本文をインデックスするようにしてみる。
$es->indices->create(
index => 'wikipedia',
body => {
settings => {
index => {
analysis => {
tokenizer => {
text_ja_tokenizer => {
type => 'kuromoji_tokenizer',
mode => 'search',
},
},
analyzer => {
text_ja_analyzer => {
tokenizer => 'text_ja_tokenizer',
type => 'custom',
filter => [
'kuromoji_part_of_speech',
'icu_normalizer',
],
char_filter => [
'html_strip'
],
},
},
},
},
},
mappings => {
page => {
properties => {
id => +{ type => 'long', index => 'not_analyzed' },
title => +{ type => 'string', index => 'analyzed', analyzer => 'text_ja_analyzer' },
text => +{ type => 'string', index => 'analyzed', analyzer => 'text_ja_analyzer' },
},
},
},
},
);
上はjsonのAPI叩く部分をperlのインターフェースでやっているだけ。elasticsearchを起動した上で、このスクリプトを叩く。
$ carton exec -- perl script/create-elasticsearch-index.pl
あとはダウンロードして展開したデータをインポートしてみる。
carton exec -- perl script/import-wikipedia.pl jawiki-latest-pages-articles1.xml
ファイル全部読み込んでツリー構造を構築し、全部メモリに持っているので、あんまり良くない。実際にはもうちょっと工夫とかしたほうが良さそう。