Class: Supple::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/supple/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Config

Returns a new instance of Config.



67
68
69
70
71
72
# File 'lib/supple/model.rb', line 67

def initialize(model)
  @model = model

  @index_name_method = Supple.config.index.default_index_name
  @document_type_method = Supple.config.index.default_document_type
end

Instance Method Details

#clientObject



64
65
66
# File 'lib/supple/model.rb', line 64

def client
  Supple.client
end

#document_type(&block) ⇒ Object



79
80
81
82
# File 'lib/supple/model.rb', line 79

def document_type(&block)
  @document_type_method = block if block_given?
  @document_type ||= @document_type_method.call(@model)
end

#index_name(&block) ⇒ Object



74
75
76
77
# File 'lib/supple/model.rb', line 74

def index_name(&block)
  @index_name_method = block if block_given?
  @index_name ||= @index_name_method.call(@model)
end

#index_scopeObject



90
91
92
# File 'lib/supple/model.rb', line 90

def index_scope
  @model.all
end

#mappings(&block) ⇒ Object



84
85
86
87
88
# File 'lib/supple/model.rb', line 84

def mappings(&block)
  @mappings ||= {}
  @mappings = DSL::Mapping.new(&block).to_hash if block_given?
  @mappings
end

#reindexObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/supple/model.rb', line 101

def reindex
  alias_name = index_name
  new_index_name = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S%L")

  document_mappings = {}
  document_mappings[document_type] = mappings
  client.indices.create index: new_index_name,
    body: {
      settings: settings,
      mappings: document_mappings
    }

  if client.indices.exists_alias(name: alias_name)
    existing_alias = client.indices.get_alias(name: alias_name)
    Importer.new(index_scope, new_index_name, document_type).execute!
    client.indices.delete index: existing_alias.keys
    client.indices.put_alias index: new_index_name, name: alias_name
  else
    client.indices.put_alias index: new_index_name, name: alias_name
    Importer.new(index_scope, new_index_name, document_type).execute!
  end

end

#settings(data = {}) ⇒ Object



94
95
96
97
98
# File 'lib/supple/model.rb', line 94

def settings(data = {})
  @settings ||= {}
  @settings.merge! data
  @settings
end