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.



9
10
11
12
13
14
15
16
17
18
# File 'lib/waistband/index.rb', line 9

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

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

  # alias subs
  @subs = [options['subs']] if options['subs'].present?
  @subs = @subs.flatten     if @subs.is_a?(Array)
end

Instance Method Details

#alias(alias_name) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/waistband/index.rb', line 140

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)


148
149
150
151
152
153
154
# File 'lib/waistband/index.rb', line 148

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

#clientObject



160
161
162
# File 'lib/waistband/index.rb', line 160

def client
  @client ||= ::Waistband.config.client
end

#configObject



156
157
158
# File 'lib/waistband/index.rb', line 156

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

#createObject



43
44
45
46
47
# File 'lib/waistband/index.rb', line 43

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

#create!Object



49
50
51
52
# File 'lib/waistband/index.rb', line 49

def create!
  raise ::Waistband::Errors::IndexExists.new("Index already exists") if exists?
  client.indices.create index: config_name, body: config.except('name', 'stringify')
end

#deleteObject



54
55
56
57
58
# File 'lib/waistband/index.rb', line 54

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

#delete!Object



60
61
62
63
# File 'lib/waistband/index.rb', line 60

def delete!
  raise ::Waistband::Errors::IndexNotFound.new("Index not found") unless exists?
  client.indices.delete index: config_name
end

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



111
112
113
114
115
# File 'lib/waistband/index.rb', line 111

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

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



117
118
119
120
121
122
123
124
125
126
# File 'lib/waistband/index.rb', line 117

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)


20
21
22
# File 'lib/waistband/index.rb', line 20

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

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



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

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

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



89
90
91
92
# File 'lib/waistband/index.rb', line 89

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

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



94
95
96
97
98
# File 'lib/waistband/index.rb', line 94

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

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/waistband/index.rb', line 100

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

#refreshObject



24
25
26
# File 'lib/waistband/index.rb', line 24

def refresh
  client.indices.refresh index: config_name
end

#save(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/waistband/index.rb', line 65

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



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/waistband/index.rb', line 128

def search(body_hash)
  page, page_size = get_page_info body_hash
  body_hash       = parse_search_body(body_hash)

  search_hash = client.search(
    index: config_name,
    body: body_hash
  )

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

#update_mapping(type) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/waistband/index.rb', line 28

def update_mapping(type)
  client.indices.put_mapping(
    index: config_name,
    type: type,
    body: config['mappings'][type]
  )
end

#update_settingsObject



36
37
38
39
40
41
# File 'lib/waistband/index.rb', line 36

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