diff --git a/app/chewy/accounts_index.rb b/app/chewy/accounts_index.rb index 61f5277d2bee7ee33e8cbf3981b27d18f046fb76..1f8571c09d3b7b70f66e56c1b33dcaac8f731d9f 100644 --- a/app/chewy/accounts_index.rb +++ b/app/chewy/accounts_index.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class AccountsIndex < Chewy::Index - settings index: { refresh_interval: '30s' }, analysis: { + settings index: index_preset(refresh_interval: '30s'), analysis: { filter: { english_stop: { type: 'stop', diff --git a/app/chewy/instances_index.rb b/app/chewy/instances_index.rb index 2bce4043c9c5d5dd343f11d9f20a5a7b7a62a621..0d58167dc807b5737cb184a33de3ece173c3d825 100644 --- a/app/chewy/instances_index.rb +++ b/app/chewy/instances_index.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class InstancesIndex < Chewy::Index - settings index: { refresh_interval: '30s' } + settings index: index_preset(refresh_interval: '30s') index_scope ::Instance.searchable diff --git a/app/chewy/statuses_index.rb b/app/chewy/statuses_index.rb index 6dd4fb18b024dc204df2b4e2f722e2d6e6166fd7..9f680efa52d5e3ff4cf7a53e5c30b45392fdf897 100644 --- a/app/chewy/statuses_index.rb +++ b/app/chewy/statuses_index.rb @@ -3,7 +3,7 @@ class StatusesIndex < Chewy::Index include FormattingHelper - settings index: { refresh_interval: '30s' }, analysis: { + settings index: index_preset(refresh_interval: '30s', number_of_shards: 5), analysis: { filter: { english_stop: { type: 'stop', diff --git a/app/chewy/tags_index.rb b/app/chewy/tags_index.rb index df3d9e4cce2920e90a88f4b7e7acf186ed9f2731..b2d50a000c38e5f1e32bb44810b078da9c4c79b9 100644 --- a/app/chewy/tags_index.rb +++ b/app/chewy/tags_index.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class TagsIndex < Chewy::Index - settings index: { refresh_interval: '30s' }, analysis: { + settings index: index_preset(refresh_interval: '30s'), analysis: { analyzer: { content: { tokenizer: 'keyword', diff --git a/config/application.rb b/config/application.rb index a3bd3dfa4e5672707b16279a936f3f75fd1952e5..372adc16801b699798e2967881cc49b5dc4e966e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -41,6 +41,8 @@ require_relative '../lib/mastodon/rack_middleware' require_relative '../lib/public_file_server_middleware' require_relative '../lib/devise/two_factor_ldap_authenticatable' require_relative '../lib/devise/two_factor_pam_authenticatable' +require_relative '../lib/chewy/settings_extensions' +require_relative '../lib/chewy/index_extensions' require_relative '../lib/chewy/strategy/mastodon' require_relative '../lib/chewy/strategy/bypass_with_warning' require_relative '../lib/webpacker/manifest_extensions' diff --git a/config/initializers/chewy.rb b/config/initializers/chewy.rb index dc90176213e5ef276f626736c383627c31c6794d..7d51a08457b879e976e1d4f9b6544f55908bb60e 100644 --- a/config/initializers/chewy.rb +++ b/config/initializers/chewy.rb @@ -25,14 +25,6 @@ Chewy.root_strategy = :bypass_with_warning if Rails.env.production? Chewy.request_strategy = :mastodon Chewy.use_after_commit_callbacks = false -module Chewy - class << self - def enabled? - settings[:enabled] - end - end -end - # Elasticsearch uses Faraday internally. Faraday interprets the # http_proxy env variable by default which leads to issues when # Mastodon is run with hidden services enabled, because diff --git a/lib/chewy/index_extensions.rb b/lib/chewy/index_extensions.rb new file mode 100644 index 0000000000000000000000000000000000000000..064fd56b3e6648b17b589a23db50ddb7863cd215 --- /dev/null +++ b/lib/chewy/index_extensions.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Chewy + module IndexExtensions + def index_preset(base_options = {}) + case ENV['ES_PRESET'].presence + when 'single_node_cluster', nil + base_options.merge(number_of_replicas: 0) + when 'small_cluster' + base_options.merge(number_of_replicas: 1) + when 'large_cluster' + base_options.merge(number_of_replicas: 1, number_of_shards: (base_options[:number_of_shards] || 1) * 2) + end + end + end +end + +Chewy::Index.extend(Chewy::IndexExtensions) diff --git a/lib/chewy/settings_extensions.rb b/lib/chewy/settings_extensions.rb new file mode 100644 index 0000000000000000000000000000000000000000..0eb8b336f3afc96fb1fb2d466d94988999a7b491 --- /dev/null +++ b/lib/chewy/settings_extensions.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Chewy + module SettingsExtensions + def enabled? + settings[:enabled] + end + end +end + +Chewy.extend(Chewy::SettingsExtensions)