Module: ElasticArSync::Elastic::Syncable::ClassMethods

Defined in:
lib/elastic_ar_sync/elastic/syncable.rb

Instance Method Summary collapse

Instance Method Details

#create_indexObject

インデックスを作成 デフォルトは クラス名の小文字_環境名



53
54
55
# 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_indexObject



150
151
152
# 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_mappingObject



154
155
156
# 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_settingsObject



158
159
160
# File 'lib/elastic_ar_sync/elastic/syncable.rb', line 158

def current_settings
  __elasticsearch__.client.indices.get_settings[current_index]
end

#default_index_mappingObject



126
127
128
129
130
131
132
133
134
135
136
# 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) ⇒ Object



94
95
96
97
98
# 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) ⇒ Object



57
58
59
# 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_aliasesObject



142
143
144
145
146
147
148
# 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) ⇒ Object

DBの内容をESのインデックスに同期する



62
63
64
# 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.to_s, target_index, batch_size)
end

#index_config(dynamic: 'false', override_settings: {}, attr_mappings: default_index_mapping, override_mappings: {}, kuromoji_default: false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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_setupObject



47
48
49
50
51
# 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_defaultObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_keysObject



138
139
140
# 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:) ⇒ Object

ダウンタイムなしでインデックスを切り替える techlife.cookpad.com/entry/2015/09/25/170000



68
69
70
# 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