Class: Elasticsearch::Resources::Index

Inherits:
Object
  • Object
show all
Includes:
Clusterable, Configurable, Nameable, Queryable, Resource
Defined in:
lib/elasticsearch/resources/index.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Nameable

included

Methods included from Clusterable

included

Methods included from Configurable

included

Methods included from Queryable

included

Methods included from Resource

included

Constructor Details

#initialize(cluster:, &block) ⇒ Index

Returns a new instance of Index.



12
13
14
15
# File 'lib/elasticsearch/resources/index.rb', line 12

def initialize(cluster:, &block)
  self.cluster = cluster
  configure(&block)
end

Class Method Details

.typesObject



119
120
121
122
123
# File 'lib/elasticsearch/resources/index.rb', line 119

def self.types
  (@type_names ||= {}).collect do |key, type_name|
    [key, Object.const_get(type_name)]
  end.to_h
end

Instance Method Details

#build_type(key:, type_class: nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/elasticsearch/resources/index.rb', line 48

def build_type(key:, type_class: nil)
  type_class = self.class.types[key] if type_class.nil?
  type_class&.new(index: self).tap do |type|
    settings.type(key).tap do |settings|
      type.settings = settings.dup unless settings.nil?
    end
  end
end

#clientObject



31
32
33
# File 'lib/elasticsearch/resources/index.rb', line 31

def client
  cluster.client
end

#count(body, options = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/elasticsearch/resources/index.rb', line 97

def count(body, options = {})
  params = {
    body: body
  }.merge(options)

  query(:count, params)
end

#createObject



77
78
79
# File 'lib/elasticsearch/resources/index.rb', line 77

def create
  query_index(:create)
end

#deleteObject



85
86
87
# File 'lib/elasticsearch/resources/index.rb', line 85

def delete
  query_index(:delete)
end

#exists?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/elasticsearch/resources/index.rb', line 73

def exists?
  query_index(:exists?)
end

#find_clusterObject



105
106
107
# File 'lib/elasticsearch/resources/index.rb', line 105

def find_cluster
  self.cluster
end

#find_index(index: nil) ⇒ Object



109
110
111
# File 'lib/elasticsearch/resources/index.rb', line 109

def find_index(index: nil)
  matches_name?(index) ? self : nil
end

#find_type(index: nil, type:) ⇒ Object



113
114
115
116
117
# File 'lib/elasticsearch/resources/index.rb', line 113

def find_type(index: nil, type:)
  types.find do |t|
    t.find_type(type: type)
  end
end

#nameObject



35
36
37
# File 'lib/elasticsearch/resources/index.rb', line 35

def name
  settings.name
end

#put_mapping(options = {}) ⇒ Object



81
82
83
# File 'lib/elasticsearch/resources/index.rb', line 81

def put_mapping(options = {})
  query_index(:put_mapping, options)
end

#query(action, options = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/elasticsearch/resources/index.rb', line 65

def query(action, options = {})
  params = {
    index: name
  }.merge(options)

  super(action, **params)
end

#query_index(action, options = {}) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/elasticsearch/resources/index.rb', line 57

def query_index(action, options = {})
  params = {
    index: name
  }.merge(options)

  client.indices.send(action, **params)
end

#search(body, options = {}) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/elasticsearch/resources/index.rb', line 89

def search(body, options = {})
  params = {
    body: body
  }.merge(options)

  query(:search, params)
end

#setup!Object



17
18
19
20
21
22
# File 'lib/elasticsearch/resources/index.rb', line 17

def setup!
  unless exists?
    create
    super
  end
end

#teardown!Object



24
25
26
27
28
29
# File 'lib/elasticsearch/resources/index.rb', line 24

def teardown!
  if exists?
    delete
    super
  end
end

#typesObject



39
40
41
42
43
44
45
46
# File 'lib/elasticsearch/resources/index.rb', line 39

def types
  @types ||= self.class.types.collect do |key, type_class|
    [
      key,
      build_type(key: key)
    ]
  end.to_h
end