ElasticsearchとWordPress
(とついでに「Mediawiki」)関連情報があちこちにとっちらかっちゃったので、一旦整理する意味でここでまとめて書いてみます。まずはインストールから。現在、「Elasticsearch」のバージョンは「5」「6」「7」に分かれています。当然最新のバージョンを入れて最新の機能を使いたいところですが連携する側の対応状況も考慮する必要があります。Wordpressで使っているプラグイン「ElasticPress」の推奨バージョンは「5.2」でテストしてある一番高いバージョンは「6.3」です。次にMediawikiの「CirrusSearch」「Elastica」が対応しているのはMediawikiの1.32は「5.6.x」まで1.33からは「6.5」以上がサポートされています。ということでこのサイトで使っている「Elasticsearch」のバージョンは「5.6.5」です。マイナーバージョンの「.5」は「Sudachi」プラグインのサポート状況から決めました。(5.6.5はSNAPSHOTが提供されている)
Ubuntuのインストールはこの「リンク」先の説明通りでOKです。バージョンを指定してインストールするには以下のコマンドを使います。
apt install elasticsearch=5.6.15
バージョンを固定・非固定するには以下のコマンドです。
apt-mark hold elasticsearch apt-mark unhold elasticsearch
インストール後にまずやるのはメモリの調整です。商用で使っているサーバーならいざ知らず、個人が趣味で使っているサーバーのメモリは多くても1GB程度だと思います。デフォルトでは2GBとなっている以下のオプションをメモリの空き容量を見て調整します。ここで指定したメモリサイズでJavaが起動します。
# Xms represents the initial size of total heap space # Xmx represents the maximum size of total heap space -Xms512m -Xmx512m
日本語の形態素解析に対応したプラグインを導入しないと全文検索ならぬ一文字ずつの全語検索となってしまいます。使うプラグインは「analysis-icu」「analysis-kuromoji」「analysis-sudachi」の3つです。
-rwxr-xr-x 1 root root 8075 2月 14 01:23 elasticsearch* -rwxr-xr-x 1 root root 2605 2月 14 01:23 elasticsearch-keystore* -rwxr-xr-x 1 root root 2595 2月 14 01:23 elasticsearch-plugin* -rwxr-xr-x 1 root root 223 2月 14 01:23 elasticsearch-systemd-pre-exec* -rwxr-xr-x 1 root root 2569 2月 14 01:23 elasticsearch-translog* -rwxr-xr-x 1 root root 367 2月 14 01:23 elasticsearch.in.sh*
プラグインのインストールには「elasticsearch-plugin」を使います。「Sudachi」はGitHubから「5.6.5」に対応したSNAPSHOTをファイルでダウンロードしておきます。以下のコマンドでインストールします。
./elasticsearch-plugin install analysis-icu ./elasticsearch-plugin install analysis-kuromoji ./elasticsearch-plugin install file:///file/location/here/analysis-sudachi-elasticsearch5.6.15-1.2.0-SNAPSHOT.zip
個人で使う場合にプロキシを経由することを考慮することは無いと思いますが、万が一プロキシを超えてプラグインをインストールしなければならないケースでは以下のようにプロキシを指定します。
ES_JAVA_OPTS="-Dhttp.proxyHost=host_name -Dhttp.proxyPort=port_number -Dhttps.proxyHost=host_name -Dhttps.proxyPort=https_port_number" bin/elasticsearch-plugin install analysis-kuromoji 使用例) ES_JAVA_OPTS="-Dhttp.proxyHost=localhost -Dhttp.proxyPort=5865 -Dhttps.proxyHost=localhost -Dhttps.proxyPort=5865" bin/elasticsearch-plugin install analysis-kuromoji
以上で「Elasticsearch」側の準備は完了です。次は「Wordpress」と「Mediawiki」での対応です。「Wordpress」は「ElasticPress」と「Sudachi」を使います。詳細な設定は「こちら」を参照下さい。「Mediawiki」は「Elastica + CirrusSearch」と「Kuromoji」を使います。詳細は「こちら」を参照下さい。インデックスの作成は「ElasticPress」はプラグインの設定画面から、「Mediawiki」は「こちら」の手順で行います。
最終更新日: 2019年6月8日