module ElasticArSync::Elastic::Syncable::ClassMethods
Public Instance Methods
create_index()
click to toggle source
インデックスを作成 デフォルトは クラス名の小文字_環境名
# File lib/elastic_ar_sync/elastic/syncable.rb, line 53 def create_index ElasticArSync::Elastic::Services::IndexHandler.new(self).create_index("#{index_name}_#{Time.zone.now.strftime('%Y%m%d%H%M')}") end
current_index()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 150 def current_index __elasticsearch__.client.indices.get_alias(index: index_name).keys.first end
current_mapping()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 154 def current_mapping __elasticsearch__.client.indices.get_mapping[current_index]["mappings"]["_doc"]["properties"] end
current_settings()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 158 def current_settings __elasticsearch__.client.indices.get_settings[current_index] end
default_index_mapping()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 126 def default_index_mapping mapping = {} attribute_types.each do |attribute, active_model_type| type = active_model_type.type type = :text if (active_model_type.type.to_sym == :string) type = :keyword if type == :integer && defined_enums.symbolize_keys.keys.include?(attribute.to_sym) type = :date if active_model_type.type == :datetime mapping[attribute.to_sym] = { type: type } end mapping end
default_index_setting(kuromoji_default = false)
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 94 def default_index_setting(kuromoji_default = false) setting_attr = { index: { number_of_shards: 1 } } setting_attr.merge!(ja_analyze_default) if kuromoji_default setting_attr end
delete_index(target_index)
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 57 def delete_index(target_index) ElasticArSync::Elastic::Services::IndexHandler.new(self).delete_index(target_index) end
get_aliases()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 142 def get_aliases begin __elasticsearch__.client.indices.get_alias(index: '') rescue Elasticsearch::Transport::Transport::Errors::NotFound raise Elasticsearch::Transport::Transport::Errors::NotFound, "インデックスがありません alias_name: #{alias_name}" end end
import_all_record(target_index: ,batch_size: 100)
click to toggle source
DBの内容をESのインデックスに同期する
# File lib/elastic_ar_sync/elastic/syncable.rb, line 62 def import_all_record(target_index: ,batch_size: 100) ElasticArSync::Elastic::Worker::IndexImportWorker.perform_async(self, target_index, batch_size) end
index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {}, kuromoji_default: false)
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 72 def index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {}, kuromoji_default: false) attr_mappings.merge!(override_mappings) if override_mappings.present? settings default_index_setting(kuromoji_default).merge!(override_settings) do # ES6からStringが使えないのでtextかkeywordにする。 mappings dynamic: dynamic do attr_mappings.each do |key, value| next unless value.is_a?(Hash) if value[:type].to_s == 'nested' indexes key ,value.reject { |k, _| k.to_sym == :nested_attr } do value[:nested_attr].each { |key, value| indexes key, value } end next end indexes key, value end end end end
index_setup()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 47 def index_setup response = create_index switch_alias(new_index_name: response["index"]) import_all_record(target_index: response["index"]) end
ja_analyze_default()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 100 def ja_analyze_default { "analysis": { "analyzer": { "normal_ja_analyzer": { "type": "custom", "tokenizer": "kuromoji_tokenizer", "mode": "search", "char_filter": [ "icu_normalizer", "kuromoji_iteration_mark" ], "filter": [ "kuromoji_baseform", "kuromoji_part_of_speech", "ja_stop", "lowercase", "kuromoji_number", "kuromoji_stemmer" ] } } } } end
mapping_list_keys()
click to toggle source
# File lib/elastic_ar_sync/elastic/syncable.rb, line 138 def mapping_list_keys mappings.to_hash[:_doc][:properties].keys end
switch_alias(new_index_name:)
click to toggle source
ダウンタイムなしでインデックスを切り替える techlife.cookpad.com/entry/2015/09/25/170000
# File lib/elastic_ar_sync/elastic/syncable.rb, line 68 def switch_alias(new_index_name:) ElasticArSync::Elastic::Services::IndexHandler.new(self).switch_alias(alias_name: index_name, new_index_name: new_index_name) end