前言
假設你已經參考 Mastodon 官方網站的說明內容,成功的配置了全文搜尋。
Configuring full-text search - Mastodon documentation
Setting up Elasticsearch to search for statuses (authored, favourited, or mentioned), public indexable status, and accounts
安裝插件
在這裡你需要先安裝兩個不同的 Elasticsearch 插件:
前者用於中文分詞的搜尋,後者用於繁簡轉換。
請參照 GitHub 上的說明完成插件的安裝,安裝後進行檢查:
檢查 Elasticsearch 版本
在終端機中輸入指令:
curl -XGET 'http://localhost:9200'得到的 JSON 回應內容可能會是這樣:
{
"name" : "taeyeon",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "_6cBkhEuTdufrOwnYghiGQ",
"version" : {
"number" : "7.17.18",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "8682172c2130b9a411b1bd5ff37c9792367de6b0",
"build_date" : "2024-02-02T12:04:59.691750271Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}其中 version 底下的 number 就是目前的 Elasticsearch 版本。
檢查插件版本
可以使用 Elasticsearch 提供的 _cat/plugins API 輸入指令:
curl -XGET 'http://localhost:9200/_cat/plugins?v'得到的回應內容可能會是這樣:
name component version
taeyeon analysis-ik 7.17.18
taeyeon analysis-stconvert 7.17.18這樣就可以明確的知道目前安裝的插件版本。
注意:插件版本要與 Elasticsearch 的版本相同
修改索引定義
這邊要修改 Mastodon 的索引配置,你可以在 ../app/chewy/ 目錄下找到相關文件:
accounts_index.rb
statuses_index.rb
tags_index.rb
接下來讓我們逐一修改各個文件:
accounts_index.rb
# frozen_string_literal: true
class AccountsIndex < Chewy::Index
include DatetimeClampingConcern
settings index: index_preset(refresh_interval: '30s'), analysis: {
filter: {
english_stop: {
type: 'stop',
stopwords: '_english_',
},
english_stemmer: {
type: 'stemmer',
language: 'english',
},
english_possessive_stemmer: {
type: 'stemmer',
language: 'possessive_english',
},
},
analyzer: {
natural: {
tokenizer: 'ik_max_word',
# 將原本的 "standard" 改為 "ik_max_word"
filter: %w(
lowercase
asciifolding
cjk_width
elision
english_possessive_stemmer
english_stop
english_stemmer
),
},
verbatim: {
# 將原本的 "standard" 改為 "ik_max_word"
tokenizer: 'ik_max_word',
filter: %w(lowercase asciifolding cjk_width),
},
edge_ngram: {
tokenizer: 'edge_ngram',
filter: %w(lowercase asciifolding cjk_width),
},
},
tokenizer: {
edge_ngram: {
type: 'edge_ngram',
min_gram: 1,
max_gram: 15,
},
},
}
index_scope ::Account.searchable.includes(:account_stat)
root date_detection: false do
field(:id, type: 'long')
field(:following_count, type: 'long')
field(:followers_count, type: 'long')
field(:properties, type: 'keyword', value: ->(account) { account.searchable_properties })
field(:last_status_at, type: 'date', value: ->(account) { clamp_date(account.last_status_at || account.created_at) })
field(:display_name, type: 'text', analyzer: 'verbatim') { field :edge_ngram, type: 'text', analyzer: 'edge_ngram', search_analyzer: 'verbatim' }
field(:username, type: 'text', analyzer: 'verbatim', value: ->(account) { [account.username, account.domain].compact.join('@') }) { field :edge_ngram, type: 'text', analyzer: 'edge_ngram', search_analyzer: 'verbatim' }
field(:text, type: 'text', analyzer: 'verbatim', value: ->(account) { account.searchable_text }) { field :stemmed, type: 'text', analyzer: 'natural' }
end
endstatuses_index.rb
# frozen_string_literal: true
class StatusesIndex < Chewy::Index
include DatetimeClampingConcern
settings index: index_preset(refresh_interval: '30s', number_of_shards: 5), analysis: {
# 新增全域 char_filter 定義
char_filter: {
tsconvert: {
type: 'stconvert',
keep_both: false,
delimiter: '#',
convert_type: 't2s',
},
},
filter: {
english_stop: {
type: 'stop',
stopwords: '_english_',
},
english_stemmer: {
type: 'stemmer',
language: 'english',
},
english_possessive_stemmer: {
type: 'stemmer',
language: 'possessive_english',
},
},
analyzer: {
verbatim: {
# 改為使用 ik_max_word
tokenizer: 'ik_max_word',
filter: %w(lowercase),
},
content: {
# 改為使用 ik_max_word
tokenizer: 'ik_max_word',
filter: %w(
lowercase
asciifolding
cjk_width
elision
english_possessive_stemmer
english_stop
english_stemmer
),
# 加入 tsconvert 過濾器
char_filter: %w(tsconvert),
},
hashtag: {
tokenizer: 'keyword',
filter: %w(
word_delimiter_graph
lowercase
asciifolding
cjk_width
),
},
},
}
index_scope ::Status.unscoped.kept.without_reblogs.includes(:media_attachments, :local_mentioned, :local_favorited, :local_reblogged, :local_bookmarked, :tags, preview_cards_status: :preview_card, preloadable_poll: :local_voters), delete_if: ->(status) { status.searchable_by.empty? }
root date_detection: false do
field(:id, type: 'long')
field(:account_id, type: 'long')
field(:text, type: 'text', analyzer: 'verbatim', value: ->(status) { status.searchable_text }) { field(:stemmed, type: 'text', analyzer: 'content') }
field(:tags, type: 'text', analyzer: 'hashtag', value: ->(status) { status.tags.map(&:display_name) })
field(:searchable_by, type: 'long', value: ->(status) { status.searchable_by })
field(:language, type: 'keyword')
field(:properties, type: 'keyword', value: ->(status) { status.searchable_properties })
field(:created_at, type: 'date', value: ->(status) { clamp_date(status.created_at) })
end
endtags_index.rb
# frozen_string_literal: true
class TagsIndex < Chewy::Index
include DatetimeClampingConcern
settings index: index_preset(refresh_interval: '30s'), analysis: {
char_filter: {
tsconvert: {
type: 'stconvert',
keep_both: false,
delimiter: '#',
convert_type: 'both',
},
},
filter: {
# 用於搜尋時產生多位置的 token 並扁平化,以保留複雜的斷詞能力
word_delimiter_graph: {
type: 'word_delimiter_graph',
preserve_original: false,
generate_word_parts: false,
generate_number_parts: false,
catenate_all: false,
adjust_offsets: true,
},
# 將 token 圖扁平化為單一序列,避免建立索引時出現 offsets 錯誤
flatten_graph: {
type: 'flatten_graph',
},
lowercase: { type: 'lowercase' },
asciifolding: { type: 'asciifolding' },
cjk_width: { type: 'cjk_width' },
},
analyzer: {
content: {
tokenizer: 'ik_max_word', # 使用 IK 斷詞器
char_filter: %w(tsconvert), # 繁簡轉換
filter: %w(lowercase asciifolding cjk_width), # 簡單索引分析
},
search_content: {
tokenizer: 'ik_max_word',
char_filter: %w(tsconvert),
filter: %w(word_delimiter_graph flatten_graph lowercase asciifolding cjk_width),
},
edge_ngram: {
tokenizer: 'edge_ngram',
filter: %w(lowercase asciifolding cjk_width),
},
},
tokenizer: {
edge_ngram: {
type: 'edge_ngram',
min_gram: 2,
max_gram: 15,
},
},
}
index_scope ::Tag.listable
crutch :time_period do
7.days.ago.to_date..0.days.ago.to_date
end
root date_detection: false do
field :name,
type: 'text',
analyzer: 'content',
search_analyzer: 'search_content',
value: :display_name do
field :edge_ngram,
type: 'text',
analyzer: 'edge_ngram',
search_analyzer: 'content'
end
field :reviewed,
type: 'boolean',
value: ->(tag) { tag.reviewed? }
field :usage,
type: 'long',
value: ->(tag, crutches) { tag.history.aggregate(crutches.time_period).accounts }
field :last_status_at,
type: 'date',
value: ->(tag) { clamp_date(tag.last_status_at || tag.created_at) }
end
end
測試驗證
都改完了,你超棒!接下來是後續的測試與驗證:
重建索引
請使用以下指令強制重建索引,以使新設定生效:
RAILS_ENV=production bin/tootctl search deploy --reset-chewy可以使用 _analyze API 針對索引測試,輸入指令:
curl -XGET 'http://localhost:9200/statuses/_analyze' \
-H 'Content-Type: application/json' \
-d '{
"analyzer": "content",
"text": "達美樂"
}'curl -XGET 'http://localhost:9200/tags/_analyze' \
-H 'Content-Type: application/json' \
-d '{
"analyzer": "content",
"text": "周杰倫"
}'看看回應的內容是否正常。
重新啟動服務
最後使用更新 Mastodon 時最常使用的指令來重啟 Mastodon 服務:
systemctl restart mastodon-sidekiq
systemctl reload mastodon-web就大功告成啦!
最後別忘記去網站試試看搜尋功能,應該是可以用啦。
參考資料
Instruction for ElasticSearch setup for Chinese support no longer current · Issue #1428 · mastodon/documentation
Steps to reproduce the problem Try to follow the setup here: https://docs.joinmastodon.org/admin/elasticsearch/#search-optimization-for-other-languages The current code does not match the diff. Exp...