kmyblueにElasticsearchを導入したときのメモ(2024年5月・2025年1月)
初出: わーさんのFediverse雑記 チラシ裏 Post No.22(2024-05-21) と Post No.35(2025-01-08)。kmyblue の全文検索(Elasticsearch+Sudachi)で四苦八苦した 2 本を一つにまとめた記事。2026 年 9 月の式年遷宮では、この経験をもとに Elasticsearch 8 系+Sudachi 3.6 を「版指定+hold」で入れ直している → 式年遷宮の手順メモ
kmyblueにElasticsearchを導入してみた忘備録(2024-05-21)
気が付いたら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を編集することで、割り当てるメモリを定義することができるようです。
詳細な設定方法は 2018年の英語記事 の中ほどにありますが、今回は1GBだけ割り振りたかったので、以下を設定しました。
-Xms256m
-Xmx256m
なお、設定後はElasticsearchの再起動が必要です。
3.Mastodonの設定ファイルを書き換える
これに関してはMastodonのドキュメントサイトの通りですね。
.env.productionに最低限これだけでも書き足します。
ES_ENABLED=true
ES_HOST=localhost
ES_PORT=9200
なお、まだMastodonの再起動は行いません。
4.Sudachiプラグインを導入する
Sudachiプラグインの releases から、Elasticsearch 7.17.何かしらの数字に対応した最新版のSudachiプラグインのアドレスを取ってきて、/etc/elasticsearch/に導入します。
wget https://github.com/WorksApplications/elasticsearch-sudachi/releases/download/v3.1.1/elasticsearch-7.17.21-analysis-sudachi-3.1.1.zip
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プラグイン用の辞書ファイルをダウンロード・展開
SudachiDict からリンクを辿って最新版の辞書を取ってきます。
wget http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/sudachi-dictionary-latest-core.zip
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をアプデするときは
取り敢えず のえるさんの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
(wget と install のファイル名はバージョンにより数字が変わります)
ElasticSearchをダウングレード?した時のメモ(2025-01-08)
正直ほぼ忘れかけてるけど今後も同じことやらかさないとは限らないので……(遠い目 #わーさんがやってみた
Ubuntuで Elasticsearch の過去バージョン をインストールしたい場合は、公式リポジトリから直接 DEBパッケージ をダウンロードしてインストールする方法が便利です。
①Elasticsearchアーカイブページにて必要なバージョンを選択し、対応する amd64のDEBパッケージ のダウンロードURLを確認
wget ダウンロードURL
②パッケージのインストールを行います。
sudo dpkg -i elasticsearch-(バージョン)-amd64.deb
このあとエラー祭りとなり、調べたところどうやら以前にインストールされていた Elasticsearch 7.17.26 のデータディレクトリが存在し、今回インストールした 7.17.24 ではそのデータ形式と互換性がないため、ダウングレードできずに起動が失敗していた模様。
③データを初期化の上で、起動します。
sudo mv /var/lib/elasticsearch /var/lib/elasticsearch_backup
sudo mkdir /var/lib/elasticsearch
sudo chown -R elasticsearch:elasticsearch /var/lib/elasticsearch
sudo chmod 700 /var/lib/elasticsearch
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
sudo systemctl status elasticsearch
④以下、新規インストールの手順を踏襲します。
【備考】
設定ファイルも吹っ飛んでいるので、メモリを限定している場合は確認が必要
sudo nano /etc/elasticsearch/jvm.options
その後(2026-09 追記): 7 系向けの Sudachi プラグインは v3.3.0(ES 7.17.24 対応)が最終で、それ以降は 7 系が打ち切られた。2026 年 9 月の式年遷宮では Elasticsearch 8.19 系+Sudachi 3.6 を新鯖に「版を指定して入れて hold」し、索引は旧鯖から運ばずに tootctl search deploy で作り直した(No.35 のとおり、データディレクトリは版が違うと読めないので持ち込まないのが正解)。