DockerのElasticsearchにSudachiを手っ取り早く入れる

概要

Elasticsearchの形態素解析にSudachiを使うといいらしい、という声を聞いた方は多いのではないでしょうか。
kuromojiよりもいいらしい、企業が辞書をメンテしているから良さそう、という評価だと思います。

一方、kuromojiは elasticsearch-plugin install analysis-kuromoji で簡単にインストールできるのに対し、Sudachiはそうなっていません。

今回はもっとも簡単に、Sudachiを入れた環境を起動させることを目指します。

必要になるもの

用意しなければならないものは、以下の3つです。

  1. Elasticsearch (当たり前)
  2. Sudachiの辞書
  3. Elasticsearchに入れるSudachiのプラグイン

これを入れたDockerイメージを作りましょう。 辞書を入れる、プラグインを入れる、ここの手順だけに特化します。

結論

ARG ELASTIC_VER=6.7.0
ARG SUDACHI_PLUGIN_VER=1.3.0


######################################################################
# Sudachiプラグインを入れたElasticsearchのイメージをビルドする
######################################################################
FROM docker.elastic.co/elasticsearch/elasticsearch:${ELASTIC_VER}

ARG ELASTIC_VER
ARG SUDACHI_PLUGIN_VER


RUN curl -OL https://object-storage.tyo2.conoha.io/v1/nc_2520839e1f9641b08211a5c85243124a/sudachi/sudachi-dictionary-20190308-core.zip && \
    curl -OL https://object-storage.tyo2.conoha.io/v1/nc_2520839e1f9641b08211a5c85243124a/sudachi/sudachi-dictionary-20190308-full.zip &&  \
    unzip -o -d config/sudachi -j  'sudachi-dictionary*.zip'  && \
    rm -f sudachi-dictionary*.zip && \
    bin/elasticsearch-plugin install https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/v${ELASTIC_VER}-${SUDACHI_PLUGIN_VER}/analysis-sudachi-elasticsearch${ELASTIC_VER}-${SUDACHI_PLUGIN_VER}.zip

あとは自身の環境でこのDockerイメージをビルドすれば、はい出来上がりです。

ポイント1

ElasticsearchのSudachiプラグインのインストールについて

公式のマニュアルには、こう書いてあります。

GitHub - WorksApplications/elasticsearch-sudachi: The Japanese analysis plugin for elasticsearch

  1. Download analysis-sudachi-elasticsearch zip archive file
  2. Move current dir to $ES_HOME
  3. Execute "bin/elasticsearch-plugin install file:///"
  4. Download sudachi dictionary archive from https://github.com/WorksApplications/SudachiDict
  5. Extract dic file and place it to config/sudachi_tokenizer/system_core.dic
  6. Execute "bin/elasticsearch"

いったんダウンロードし、そのファイルを使いましょうということですが、この手順が面倒なので、Githubのリリースタブから対象のzipを見つけて、そのURLを指定する形がもっとも簡単かと思います。

つまり、こうなります。

bin/elasticsearch-plugin install https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/v6.7.0-1.3.0/analysis-sudachi-elasticsearch6.7.0-1.3.0.zip

ポイント2

辞書は2種類、coreとfullとついたものがあります。

GitHub - WorksApplications/SudachiDict: A lexicon for Sudachi

Latest Versionのところにビルド済みのzipになった辞書があがっているので、これを利用するのが手っ取り早いです。

起動して確認

プラグインの確認

f:id:tsgkdt:20190409180351p:plain
plugins

動作の確認

analyze APIを実行してSudachiが実行されているかを確認します。

f:id:tsgkdt:20190409174337p:plain
analyzer

次回は

今回は既にビルドされたものを使った形でした。
ソースからビルドしようという猛者もいるでしょうから、次回はその内容を書く予定です。