Class: Waistband::Index
- Inherits:
-
Object
- Object
- Waistband::Index
- Defined in:
- lib/waistband/index.rb
Instance Method Summary collapse
- #alias(alias_name) ⇒ Object
- #alias_exists?(alias_name) ⇒ Boolean
- #client ⇒ Object
- #config ⇒ Object
- #create ⇒ Object
- #create! ⇒ Object
- #delete ⇒ Object
- #delete! ⇒ Object
- #destroy(id, options = {}) ⇒ Object
- #destroy!(id, options = {}) ⇒ Object
- #exists? ⇒ Boolean
- #find(id, options = {}) ⇒ Object
- #find!(id, options = {}) ⇒ Object
-
#initialize(index_name, options = {}) ⇒ Index
constructor
A new instance of Index.
- #read(id, options = {}) ⇒ Object
- #read!(id, options = {}) ⇒ Object
- #refresh ⇒ Object
- #save(*args) ⇒ Object
- #search(body_hash) ⇒ Object
- #update_mapping(type) ⇒ Object
- #update_settings ⇒ Object
Constructor Details
#initialize(index_name, options = {}) ⇒ Index
Returns a new instance of Index.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/waistband/index.rb', line 9 def initialize(index_name, = {}) = .stringify_keys @index_name = index_name @stringify = config['stringify'] # subindexes checks if ['version'].present? # version @version = ['version'] @subs = ['version', @version] elsif ['subs'].present? # subs @subs = [['subs']] if ['subs'].present? @subs = @subs.flatten if @subs.is_a?(Array) end end |
Instance Method Details
#alias(alias_name) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/waistband/index.rb', line 149 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
157 158 159 160 161 162 163 |
# File 'lib/waistband/index.rb', line 157 def alias_exists?(alias_name) alias_name = full_alias_name alias_name client.indices.exists_alias( index: config_name, name: alias_name ) end |
#client ⇒ Object
169 170 171 |
# File 'lib/waistband/index.rb', line 169 def client @client ||= ::Waistband.config.client end |
#config ⇒ Object
165 166 167 |
# File 'lib/waistband/index.rb', line 165 def config ::Waistband.config.index @index_name end |
#create ⇒ Object
51 52 53 54 55 |
# File 'lib/waistband/index.rb', line 51 def create create! rescue ::Waistband::Errors::IndexExists => ex true end |
#create! ⇒ Object
57 58 59 60 |
# File 'lib/waistband/index.rb', line 57 def create! raise ::Waistband::Errors::IndexExists.new("Index already exists") if exists? client.indices.create index: config_name, body: config.except('name', 'stringify') end |
#delete ⇒ Object
62 63 64 65 66 |
# File 'lib/waistband/index.rb', line 62 def delete delete! rescue ::Waistband::Errors::IndexNotFound => ex true end |
#delete! ⇒ Object
68 69 70 71 |
# File 'lib/waistband/index.rb', line 68 def delete! raise ::Waistband::Errors::IndexNotFound.new("Index not found") unless exists? client.indices.delete index: config_name end |
#destroy(id, options = {}) ⇒ Object
119 120 121 122 123 |
# File 'lib/waistband/index.rb', line 119 def destroy(id, = {}) destroy!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#destroy!(id, options = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/waistband/index.rb', line 125 def destroy!(id, = {}) = .with_indifferent_access type = [:_type] || default_type_name client.delete( index: config_name, id: id, type: type ) end |
#exists? ⇒ Boolean
28 29 30 |
# File 'lib/waistband/index.rb', line 28 def exists? client.indices.exists index: config_name end |
#find(id, options = {}) ⇒ Object
91 92 93 94 95 |
# File 'lib/waistband/index.rb', line 91 def find(id, = {}) find!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#find!(id, options = {}) ⇒ Object
97 98 99 100 |
# File 'lib/waistband/index.rb', line 97 def find!(id, = {}) doc = read!(id, ) doc['_source'] end |
#read(id, options = {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/waistband/index.rb', line 102 def read(id, = {}) read!(id, ) rescue Elasticsearch::Transport::Transport::Errors::NotFound nil end |
#read!(id, options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/waistband/index.rb', line 108 def read!(id, = {}) = .with_indifferent_access type = [:_type] || default_type_name client.get( index: config_name, type: type, id: id ).with_indifferent_access end |
#refresh ⇒ Object
32 33 34 |
# File 'lib/waistband/index.rb', line 32 def refresh client.indices.refresh index: config_name end |
#save(*args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/waistband/index.rb', line 73 def save(*args) body_hash = args. 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
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/waistband/index.rb', line 136 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_mapping(type) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/waistband/index.rb', line 36 def update_mapping(type) client.indices.put_mapping( index: config_name, type: type, body: config['mappings'][type] ) end |
#update_settings ⇒ Object
44 45 46 47 48 49 |
# File 'lib/waistband/index.rb', line 44 def update_settings client.indices.put_settings( index: config_name, body: settings ) end |