Class: Waistband::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/waistband/index.rb

Instance Method Summary collapse

Constructor Details

#initialize(index_name, options = {}) ⇒ Index

Returns a new instance of Index.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/waistband/index.rb', line 11

def initialize(index_name, options = {})
  options = options.stringify_keys

  @index_name = index_name
  @stringify  = config['stringify']
  @log_level  = config['log_level']

  # subindexes checks
  if options['version'].present?
    # version
    @version  = options['version']
    @subs     = ['version', @version]
  elsif options['subs'].present?
    # subs
    @subs = [options['subs']] if options['subs'].present?
    @subs = @subs.flatten     if @subs.is_a?(Array)
  end

end

Instance Method Details

#alias(alias_name) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/waistband/index.rb', line 177

def alias(alias_name)
  alias_name = full_alias_name alias_name
  client.indices.put_alias(
    index: config_name,
    name: alias_name
  )
end

#alias_exists?(alias_name) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
# File 'lib/waistband/index.rb', line 185

def alias_exists?(alias_name)
  alias_name = full_alias_name alias_name
  client.indices.exists_alias(
    index: config_name,
    name: alias_name
  )
end

#clientObject



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/waistband/index.rb', line 197

def client
  @client ||= begin
    _client = ::Waistband.config.client

    if @log_level && Waistband.config.logger
      _client.transport.logger       = Waistband.config.logger
      _client.transport.logger.level = @log_level
    end

    _client
  end
end

#configObject



193
194
195
# File 'lib/waistband/index.rb', line 193

def config
  ::Waistband.config.index @index_name
end

#createObject



64
65
66
67
68
# File 'lib/waistband/index.rb', line 64

def create
  create!
rescue ::Waistband::Errors::IndexExists => ex
  true
end

#create!Object



70
71
72
73
74
75
# File 'lib/waistband/index.rb', line 70

def create!
  client.indices.create index: config_name, body: config.except('name', 'stringify', 'log_level')
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => ex
  raise ex unless ex.message.to_s =~ /IndexAlreadyExistsException/
  raise ::Waistband::Errors::IndexExists.new("Index already exists")
end

#deleteObject



77
78
79
80
81
# File 'lib/waistband/index.rb', line 77

def delete
  delete!
rescue ::Waistband::Errors::IndexNotFound => ex
  true
end

#delete!Object



83
84
85
86
87
88
# File 'lib/waistband/index.rb', line 83

def delete!
  client.indices.delete index: config_name
rescue Elasticsearch::Transport::Transport::Errors::NotFound => ex
  raise ex unless ex.message.to_s =~ /IndexMissingException/
  raise ::Waistband::Errors::IndexNotFound.new("Index not found")
end

#destroy(id, options = {}) ⇒ Object



147
148
149
150
151
# File 'lib/waistband/index.rb', line 147

def destroy(id, options = {})
  destroy!(id, options)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#destroy!(id, options = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/waistband/index.rb', line 153

def destroy!(id, options = {})
  options = options.with_indifferent_access
  type = options[:_type] || default_type_name

  client.delete(
    index: config_name,
    id: id,
    type: type
  )
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  client.indices.exists index: config_name
end

#find(id, options = {}) ⇒ Object



108
109
110
111
112
# File 'lib/waistband/index.rb', line 108

def find(id, options = {})
  find!(id, options)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#find!(id, options = {}) ⇒ Object



114
115
116
117
# File 'lib/waistband/index.rb', line 114

def find!(id, options = {})
  doc = read!(id, options)
  doc['_source']
end

#read(id, options = {}) ⇒ Object



130
131
132
133
134
# File 'lib/waistband/index.rb', line 130

def read(id, options = {})
  read!(id, options)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#read!(id, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/waistband/index.rb', line 136

def read!(id, options = {})
  options = options.with_indifferent_access
  type = options[:_type] || default_type_name

  client.get(
    index: config_name,
    type: type,
    id: id
  ).with_indifferent_access
end

#read_result(id, options = {}) ⇒ Object



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

def read_result(id, options = {})
  read_result!(id, options)
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#read_result!(id, options = {}) ⇒ Object



125
126
127
128
# File 'lib/waistband/index.rb', line 125

def read_result!(id, options = {})
  hit = read!(id, options)
  ::Waistband::Result.new(hit)
end

#refreshObject



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

def refresh
  client.indices.refresh index: config_name
end

#save(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/waistband/index.rb', line 90

def save(*args)
  body_hash = args.extract_options!
  id = args.first
  _type = body_hash.delete(:_type) || body_hash.delete('_type') || default_type_name

  # map everything to strings if need be
  body_hash = stringify_all(body_hash) if @stringify

  saved = client.index(
    index: config_name,
    type: _type,
    id: id,
    body: body_hash
  )

  saved['_id'].present?
end

#search(body_hash) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/waistband/index.rb', line 164

def search(body_hash)
  page, page_size = get_page_info body_hash
  body_hash       = parse_search_body(body_hash)
  search_hash     = {index: config_name, body: body_hash}

  search_hash[:from] = body_hash[:from] if body_hash[:from]
  search_hash[:size] = body_hash[:size] if body_hash[:size]

  search_hash = client.search(search_hash)

  ::Waistband::SearchResults.new(search_hash, page: page, page_size: page_size)
end

#update_all_mappingsObject



39
40
41
42
43
# File 'lib/waistband/index.rb', line 39

def update_all_mappings
  responses = types.map do |type|
    update_mapping(type).merge('_type' => type)
  end
end

#update_mapping(type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/waistband/index.rb', line 45

def update_mapping(type)
  properties = config['mappings'][type]['properties'] || {}

  mapping_hash = {type => {properties: properties}}

  client.indices.put_mapping(
    index: config_name,
    type: type,
    body: mapping_hash
  )
end

#update_settingsObject



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

def update_settings
  client.indices.put_settings(
    index: config_name,
    body: settings
  )
end