Module: Syphon::Index::ClassMethods

Defined in:
lib/syphon/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index_base_nameObject



39
40
41
# File 'lib/syphon/index.rb', line 39

def index_base_name
  @index_base_name ||= name.sub(/Index\z/, '').underscore.pluralize
end

#index_settingsObject

Returns the value of attribute index_settings.



11
12
13
# File 'lib/syphon/index.rb', line 11

def index_settings
  @index_settings
end

#pre_sqlObject

Returns the value of attribute pre_sql.



11
12
13
# File 'lib/syphon/index.rb', line 11

def pre_sql
  @pre_sql
end

Instance Method Details

#build(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/syphon/index.rb', line 47

def build(options = {})
  old_internal_name = internal_index_name
  new_internal_name = new_internal_index_name(index_name)

  made_it = false
  begin
    client.indices.create(index: new_internal_name, body: {settings: index_settings})
    sources.each do |name, source|
      body = source.mapping
      client.indices.put_mapping(index: new_internal_name, type: source.type, body: body)
      source.import(index: new_internal_name) unless options[:schema_only]
    end

    warmups.each { |w| w.call(new_internal_name) }

    remove = {remove: {index: old_internal_name, alias: index_name}} if old_internal_name
    add = {add: {index: new_internal_name, alias: index_name}}
    client.indices.update_aliases body: {actions: [remove, add].compact}
    made_it = true
  ensure
    if made_it
      client.indices.delete(index: old_internal_name) if old_internal_name
    else
      client.indices.delete(index: new_internal_name) if new_internal_name
    end
  end
end

#clientObject



23
24
25
# File 'lib/syphon/index.rb', line 23

def client
  Syphon.client
end

#database_connectionObject



19
20
21
# File 'lib/syphon/index.rb', line 19

def database_connection
  Syphon.database_connection
end

#define_source(name = nil, options = {}, &block) ⇒ Object



86
87
88
89
90
# File 'lib/syphon/index.rb', line 86

def define_source(name = nil, options = {}, &block)
  source = sources[name] ||= Source.new(self, name, options)
  source.schema.configure(&block) if block
  source
end

#define_warmup(&block) ⇒ Object



92
93
94
# File 'lib/syphon/index.rb', line 92

def define_warmup(&block)
  warmups << block
end

#destroyObject



75
76
77
78
# File 'lib/syphon/index.rb', line 75

def destroy
  internal_name = internal_index_name and
    client.indices.delete index: internal_name
end

#index_nameObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/syphon/index.rb', line 27

def index_name
  @index_name ||=
    begin
      namespace = Syphon.index_namespace
      if namespace.to_s.empty?
        index_base_name
      else
        "#{namespace}_#{index_base_name}"
      end
    end
end

#inherited(subclass) ⇒ Object



13
14
15
16
17
# File 'lib/syphon/index.rb', line 13

def inherited(subclass)
  subclass.pre_sql = pre_sql.dup
  subclass.index_settings = index_settings.dup
  super
end

#search(options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/syphon/index.rb', line 80

def search(options = {})
  options[:index] ||= index_name
  options[:type] ||= source.type
  client.search(options)
end

#source(name = nil) ⇒ Object



96
97
98
# File 'lib/syphon/index.rb', line 96

def source(name = nil)
  sources[name]
end

#sourcesObject



43
44
45
# File 'lib/syphon/index.rb', line 43

def sources
  @sources ||= {}
end

#warmupsObject



100
101
102
# File 'lib/syphon/index.rb', line 100

def warmups
  @warmups ||= []
end