--- tags: public-tech breaks: false --- # Mastodon v3.5.0とElasticsearch v7.17.2とanalysis-sudachi v2.1.0で日本語全文検索をする MastodonにはElasticsearchと連携して全文検索をする機能があり、これを有効化する方法はドキュメントとして公開されている([Full-text search](https://docs.joinmastodon.org/admin/optional/elasticsearch/))。しかしこの仕組みは欧文用のため、これをそのまま使うと日本語ではうまく全文検索できない。そこで、以前から有志によってMastodon側に変更を施して日本語に対しても検索できるようにする手法が開発されていた。この記事では、そのうち[kaias1jpさんのもの](https://www.kaias1jp.com/entry/2021/01/02/145307)をベースにして、自分のサーバに導入した際の作業手順をまとめる。この作業を行うことでanalysis-sudachi(とanalysis-icu)を用いて日本語の文節を意識した全文検索を行うことが可能になる。 ## 1. Mastodon v3.5.0にパッチを当てる 実はMastodon v3.5.0にはバグがあり、後述する`tootctl search deploy`が失敗する。これを解決するために、v3.5.0のリリース直後の[コミット](https://github.com/mastodon/mastodon/commit/ef196c913c77338be5ebb1e02af2f6225f857080)を取り込んだソースコードをデプロイする。このコミット自体は独立しているので、単にGit上でマージしてMastodonを再起動すれば反映される(はず)。 ## 2. Mastodonを改造して日本語全文検索を可能にする Mastodonのソースコードを編集し、analysis-sudachiを利用して全文検索を行うようにする。詳細は[当該コミット](https://github.com/ushitora-anqou/mastodon/commit/c9e62af0c9f37edf4488b04489dd25b5e98fd627)(と[kaias1jpさんの記事](https://www.kaias1jp.com/entry/2021/01/02/145307))を参考のこと。ただし元記事と異なり、公開投稿全てを検索対象とする修正は入れていないため、検索できるのは検索ユーザが関わった投稿のみになる。 ついでに表示件数を増加させる修正も行った([当該コミット](https://github.com/ushitora-anqou/mastodon/commit/47eeb6cbd46bff68575c2210d10bb1c6d63502cb))。 ## 3. Elasticsearch v7.17.2のインストール [Mastodonの公式ドキュメント](https://docs.joinmastodon.org/admin/optional/elasticsearch/)の通りにインストールする。 ``` apt install openjdk-17-jre-headless wget -O /usr/share/keyrings/elasticsearch.asc https://artifacts.elastic.co/GPG-KEY-elasticsearch echo "deb [signed-by=/usr/share/keyrings/elasticsearch.asc] https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list apt update apt install elasticsearch ``` v7.17.2が入る。後述するanalysis-sudachiはElasticsearchのバージョンに応じて違うバイナリをインストールする必要があるため、勝手にElasticsearchのバージョンが上がらないようにholdしておく。 ``` sudo apt-mark hold elasticsearch ``` ## 4. analysis-sudachi v2.1.0のインストール [analysis-sudachi](https://github.com/WorksApplications/elasticsearch-sudachi)は形態素解析器であるSudachiをElasticsearchで使用できるようにするプラグインである。これの最新版であるv2.1.0をインストールするのだが、GitHubで配布されているバイナリはElasticsearch v7.10.1向けであるため、そのままではインストールできない。そこで、READMEに従って自分でビルドする必要がある。 ``` # build ./gradlew -PelasticsearchVersion=7.17.2 build ``` 成果物は `build/distributions/analysis-sudachi-7.17.2-2.1.1-SNAPSHOT.zip` にある。これをElasticsearchに食わせてインストールする。 ``` # install /usr/share/elasticsearch/bin/elasticsearch-plugin install file:///path/to/elasticsearch-sudachi/build/distributions/analysis-sudachi-7.17.2-2.1.1-SNAPSHOT.zip ``` ## 5. analysis-icuの導入 日本語文字列の正規化を行うICUを導入する。 ``` /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-icu ``` ## 6. Sudachi用辞書ファイルの設置 Sudachiが正常に動作するためには辞書ファイルが必要である。辞書ファイルは https://github.com/WorksApplications/SudachiDict にて配布されている。 `/etc/elasticsearch/sudachi/`に以下のファイルを置く。なおこのパスは先程編集したMastodonのソース中に[ハードコードされている](https://github.com/ushitora-anqou/mastodon/commit/c9e62af0c9f37edf4488b04489dd25b5e98fd627#diff-96f454fcd855b89e4a636b7043e1f50f26a6555b851734250a9e6ea30935cb66R11-R12)。 - `system_*.dic` http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/ - `sudachi.json` https://github.com/WorksApplications/Sudachi/blob/develop/src/main/resources/sudachi.json - `char.def` https://github.com/WorksApplications/Sudachi/blob/develop/src/main/resources/char.def なお`sudachi.json`の中の`system_core.dic`を`system_full.dic`に書き換えると、より豊富な語彙が使えるようになりそうである。ただしその場合でも`system_core.dic`が`/etc/elasticsearch/sudachi/`配下に無いと動かない。 ## 7. Elasticsearchのメモリ使用量を設定 Elasticsearchが思いがけず大量にメモリを消費することを防ぐために、メモリ使用量を予め設定しておく。この時`/etc/elasticsearch/jvm.options`を書き換える**のではなく**、`/etc/elasticsearch/jvm.options.d/`配下に`.options`で終わるファイル名のファイルを置いて指定するのが[当世風らしい](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/advanced-configuration.html)ので、そのようにする。今回は`memory.options`というファイルを作成して、以下のように記述し256MBに制限する。 ``` -Xms256m -Xmx256m ``` ## 8. Elasticsearchを起動 ``` systemctl start elasticsearch ``` ## 9. `.env.production`の編集 以下を追加: ``` ES_ENABLED=true ES_HOST=localhost ES_PORT=9200 ``` ## 10. Mastodonをrestart ``` systemctl restart mastodon-sidekiq systemctl reload mastodon-web ``` ## 11. Elasticsearchのindexの作成 以下を実行して、現在DBに存在する投稿をElasticsearchに取り込む。DBのサイズにもよるが、大抵長時間かかるので、寝る前とかに実行するのがよさそう。 ``` su - mastodon cd live RAILS_ENV=production bin/tootctl search deploy ``` この処理が終われば、無事検索窓から全文検索ができるようになっている(はず)。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up