No.22
kmyblueにElasticsearchを導入してみた忘備録
気が付いたら3時間ほど格闘していたので、今後の為に忘備録を!忘れる前に!!
それなりに文章が長くなるかもしれないので、畳んでおきます。
#わーさんがやってみた
参考サイト・公式系
https://github.com/kmycode/mastodon/wiki/ElasticSearch%E8%A8%AD%E5%AE%9A%E6%96%B9%E6%B3%95 (kmyblue公式wiki)
https://docs.joinmastodon.org/admin/elasticsearch/ (Mastodonのドキュメントサイト・英語)
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/install-elasticsearch.html (Elasticsearchのドキュメントサイト・英語)
https://github.com/WorksApplications/elasticsearch-sudachi (Sudachiプラグインを置いてある場所・英語)
https://github.com/WorksApplications/elasticsearch-sudachi/releases (実際に使用中のElasticsearchに対応したSudachiプラグインを探す先・英語)
https://github.com/WorksApplications/SudachiDict (Sudachiプラグイン用の辞書ファイル・英語)
参考サイト・忘備録系
https://blog.noellabo.jp/entry/2019/04/19/YNUL9UsohRgNKSya (『ElasticsearchをMastodonとは別のサーバに設置する』Fedibird管理人のえるさんによる2019年の忘備録)
https://github.com/felx/mastodon-documentation/blob/master/Running-Mastodon/Elasticsearch-guide.md (2018年の英語記事)
https://www.geekfeed.co.jp/amazonlinux2-elasticsearch7-sudachi#elasticsearch-sudachi (『AmazonLinux2にelasticsearch7と日本語の形態素解析器「Sudachi」プラグインをインストール』2020年の記事)
実際に行った作業手順
0.下調べした結果、別サーバーへの設置を諦める
主にVPS同士の通信面への知識不足と、VPSの必要スペックがよく分からなかったことなどから、断念。
1.Elasticsearchの推奨スペックを求めて唸る
普通に検索すると、64GBの物理メモリを準備して半分を割り当て云々……みたいなのが引っかかってきたり、ストレージの必要量もよく分からなかったりしたので、取り敢えず自鯖(RAM 4GB)のメモリ使用量が半分くらいなのを確認の上で、メモリ1GBを割り当てることにしました。
どうやら、MastodonでのElasticsearchは1GBで止まることあり、2GBあれば普通に動くけれど4GBが安定?という話があるようです(風の噂)
のちに、のえるさんから、FedibirdのElasticsearchはRAM 4GBのVPSを3台使っているとの情報を得られました。
2.Elasticsearchをインストールする
基本的にkmyblue公式wikiとMastodonやElasticsearchのドキュメントを見ながら進めていく感じです。
2024年5月現在、Mastodonはバージョン7のElasticsearchを使用すること前提なので、バージョン7.17のElasticsearchを入れます。
非DockerのUbuntuの場合、以下のような感じです。
sudo apt install openjdk-17-jre-headless
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update
sudo apt install elasticsearch
systemctl daemon-reload
systemctl enable --now elasticsearch
sudo apt-mark hold elasticsearch
最後の行はSudachiプラグインを使用する際に必要です。
というのも、Sudachiプラグインは厳密にElasticsearchのバージョンに依存するので、勝手にアプデされると使えなくなってしまうからです。
なお、/etc/elasticsearch/jvm.optionsを編集することで、割り当てるメモリを定義することができるようです。
詳細な設定方法は https://github.com/felx/mastodon-documentation/blob/master/Running-Mastodon/Elasticsearch-guide.md (2018年の英語記事)の中ほどにありますが、今回は1GBだけ割り振りたかったので、以下を設定しました。
-Xms256m
-Xmx256m
なお、設定後はElasticsearchの再起動が必要です。
3.Mastodonの設定ファイルを書き換える
これに関してはMastodonのドキュメントサイト・英語の通りですね。
.env.productionに最低限これだけでも書き足します。
ES_ENABLED=true
ES_HOST=localhost
ES_PORT=9200
なお、まだMastodonの再起動は行いません。
4.Sudachiプラグインを導入する
https://github.com/WorksApplications/elasticsearch-sudachi/releases (実際に使用中のElasticsearchに対応したSudachiプラグインを探す先・英語)から、Elasticsearch 7.17.何かしらの数字に対応した最新版のSudachiプラグインのアドレスを取ってきて、/etc/elasticsearch/に導入します。
wget https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/v3.1.1/elasticsearch-7....
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///home/(ユーザー名)/elasticsearch-7.17.21-analysis-sudachi-3.1.1.zip
無事にインストールできたかの確認は、以下のコマンドで。
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list
5.Sudachiプラグイン用の辞書ファイルをダウンロード・展開
https://github.com/WorksApplications/SudachiDict (Sudachiプラグイン用の辞書ファイル・英語)からリンクを辿って最新版の辞書を取ってきます。
wget http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/sudachi-dictionary-latest-core.zi...
sudo apt install unzip
unzip sudachi-dictionary-latest-core.zip
mkdir /etc/elasticsearch/sudachi/
cp sudachi-dictionary-latest-core/system_core.dic /etc/elasticsearch/sudachi/system_core.dic
辞書までインストールできたら、追加で/etc/elasticsearch/sudachi/config.jsonに下記を記述して保存します。
{
"systemDict": "system_core.dic"
}
念のためにElasticsearchを再起動しました。
sudo systemctl restart elasticsearch
6.kmyblueの再起動と設定
この辺りはkmyblue公式wikiの手順通りです。
sudo systemctl restart mastodon-*
sudo su - mastodon
cd live
ln -s config/elasticsearch.default-ja-sudachi.yml .elasticsearch.yml
念のためにもう一度Mastodonの再起動後、いよいよ満を持して
RAILS_ENV=production bin/tootctl search deploy
7.未知の領域・Elasticsearchをアプデするときは
取り敢えず https://blog.noellabo.jp/entry/2019/04/19/YNUL9UsohRgNKSya (『ElasticsearchをMastodonとは別のサーバに設置する』Fedibird管理人のえるさんによる2019年の忘備録)を読むのが安定でしょう。
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin remove analysis-sudachi
sudo apt-mark unhold elasticsearch
sudo apt install elasticsearch
sudo apt-mark hold elasticsearch
wget https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/次のバージョンの数値/analysis-sudachi-elasticsearch7.何かしらの数字-次のバージョンの数値.zip←バージョンにより数字は変わります
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///home/(ユーザー名)/analysis-sudachi-elasticsearch7.何かしらの数字-次のバージョンの数値.zip←同上
sudo systemctl restart elasticsearch
畳む
気が付いたら3時間ほど格闘していたので、今後の為に忘備録を!忘れる前に!!
それなりに文章が長くなるかもしれないので、畳んでおきます。
#わーさんがやってみた
参考サイト・公式系
https://github.com/kmycode/mastodon/wiki/ElasticSearch%E8%A8%AD%E5%AE%9A%E6%96%B9%E6%B3%95 (kmyblue公式wiki)
https://docs.joinmastodon.org/admin/elasticsearch/ (Mastodonのドキュメントサイト・英語)
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/install-elasticsearch.html (Elasticsearchのドキュメントサイト・英語)
https://github.com/WorksApplications/elasticsearch-sudachi (Sudachiプラグインを置いてある場所・英語)
https://github.com/WorksApplications/elasticsearch-sudachi/releases (実際に使用中のElasticsearchに対応したSudachiプラグインを探す先・英語)
https://github.com/WorksApplications/SudachiDict (Sudachiプラグイン用の辞書ファイル・英語)
参考サイト・忘備録系
https://blog.noellabo.jp/entry/2019/04/19/YNUL9UsohRgNKSya (『ElasticsearchをMastodonとは別のサーバに設置する』Fedibird管理人のえるさんによる2019年の忘備録)
https://github.com/felx/mastodon-documentation/blob/master/Running-Mastodon/Elasticsearch-guide.md (2018年の英語記事)
https://www.geekfeed.co.jp/amazonlinux2-elasticsearch7-sudachi#elasticsearch-sudachi (『AmazonLinux2にelasticsearch7と日本語の形態素解析器「Sudachi」プラグインをインストール』2020年の記事)
実際に行った作業手順
0.下調べした結果、別サーバーへの設置を諦める
主にVPS同士の通信面への知識不足と、VPSの必要スペックがよく分からなかったことなどから、断念。
1.Elasticsearchの推奨スペックを求めて唸る
普通に検索すると、64GBの物理メモリを準備して半分を割り当て云々……みたいなのが引っかかってきたり、ストレージの必要量もよく分からなかったりしたので、取り敢えず自鯖(RAM 4GB)のメモリ使用量が半分くらいなのを確認の上で、メモリ1GBを割り当てることにしました。
どうやら、MastodonでのElasticsearchは1GBで止まることあり、2GBあれば普通に動くけれど4GBが安定?という話があるようです(風の噂)
のちに、のえるさんから、FedibirdのElasticsearchはRAM 4GBのVPSを3台使っているとの情報を得られました。
2.Elasticsearchをインストールする
基本的にkmyblue公式wikiとMastodonやElasticsearchのドキュメントを見ながら進めていく感じです。
2024年5月現在、Mastodonはバージョン7のElasticsearchを使用すること前提なので、バージョン7.17のElasticsearchを入れます。
非DockerのUbuntuの場合、以下のような感じです。
sudo apt install openjdk-17-jre-headless
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update
sudo apt install elasticsearch
systemctl daemon-reload
systemctl enable --now elasticsearch
sudo apt-mark hold elasticsearch
最後の行はSudachiプラグインを使用する際に必要です。
というのも、Sudachiプラグインは厳密にElasticsearchのバージョンに依存するので、勝手にアプデされると使えなくなってしまうからです。
なお、/etc/elasticsearch/jvm.optionsを編集することで、割り当てるメモリを定義することができるようです。
詳細な設定方法は https://github.com/felx/mastodon-documentation/blob/master/Running-Mastodon/Elasticsearch-guide.md (2018年の英語記事)の中ほどにありますが、今回は1GBだけ割り振りたかったので、以下を設定しました。
-Xms256m
-Xmx256m
なお、設定後はElasticsearchの再起動が必要です。
3.Mastodonの設定ファイルを書き換える
これに関してはMastodonのドキュメントサイト・英語の通りですね。
.env.productionに最低限これだけでも書き足します。
ES_ENABLED=true
ES_HOST=localhost
ES_PORT=9200
なお、まだMastodonの再起動は行いません。
4.Sudachiプラグインを導入する
https://github.com/WorksApplications/elasticsearch-sudachi/releases (実際に使用中のElasticsearchに対応したSudachiプラグインを探す先・英語)から、Elasticsearch 7.17.何かしらの数字に対応した最新版のSudachiプラグインのアドレスを取ってきて、/etc/elasticsearch/に導入します。
wget https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/v3.1.1/elasticsearch-7....
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///home/(ユーザー名)/elasticsearch-7.17.21-analysis-sudachi-3.1.1.zip
無事にインストールできたかの確認は、以下のコマンドで。
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list
5.Sudachiプラグイン用の辞書ファイルをダウンロード・展開
https://github.com/WorksApplications/SudachiDict (Sudachiプラグイン用の辞書ファイル・英語)からリンクを辿って最新版の辞書を取ってきます。
wget http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/sudachi-dictionary-latest-core.zi...
sudo apt install unzip
unzip sudachi-dictionary-latest-core.zip
mkdir /etc/elasticsearch/sudachi/
cp sudachi-dictionary-latest-core/system_core.dic /etc/elasticsearch/sudachi/system_core.dic
辞書までインストールできたら、追加で/etc/elasticsearch/sudachi/config.jsonに下記を記述して保存します。
{
"systemDict": "system_core.dic"
}
念のためにElasticsearchを再起動しました。
sudo systemctl restart elasticsearch
6.kmyblueの再起動と設定
この辺りはkmyblue公式wikiの手順通りです。
sudo systemctl restart mastodon-*
sudo su - mastodon
cd live
ln -s config/elasticsearch.default-ja-sudachi.yml .elasticsearch.yml
念のためにもう一度Mastodonの再起動後、いよいよ満を持して
RAILS_ENV=production bin/tootctl search deploy
7.未知の領域・Elasticsearchをアプデするときは
取り敢えず https://blog.noellabo.jp/entry/2019/04/19/YNUL9UsohRgNKSya (『ElasticsearchをMastodonとは別のサーバに設置する』Fedibird管理人のえるさんによる2019年の忘備録)を読むのが安定でしょう。
- elasticsearchのプラグインはバージョンチェックが厳密で、本体のバージョンと完全一致したものを使う必要があり、毎回削除して入れ直しが必要
- なので、本体だけうっかりアップデートすると死ぬので、普段はapt-markでholdしておくこと
- https://github.com/WorksApplications/elasticsearch-sudachi/releases からanalysis-sudachiの最新リリースをチェックし、対応版がでてから切り替えるのが吉
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin remove analysis-sudachi
sudo apt-mark unhold elasticsearch
sudo apt install elasticsearch
sudo apt-mark hold elasticsearch
wget https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/次のバージョンの数値/analysis-sudachi-elasticsearch7.何かしらの数字-次のバージョンの数値.zip←バージョンにより数字は変わります
sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///home/(ユーザー名)/analysis-sudachi-elasticsearch7.何かしらの数字-次のバージョンの数値.zip←同上
sudo systemctl restart elasticsearch
畳む
- ユーザ「和条門 尚樹」の投稿だけを見る (※時系列順で見る)
- この投稿と同じカテゴリに属する投稿:
- この投稿日時に関連する投稿:
- この投稿に隣接する前後3件ずつをまとめて見る
- この投稿を再編集または削除する


