Class: Elasticsearch::Resources::Index
- Inherits:
-
Object
- Object
- Elasticsearch::Resources::Index
- Includes:
- Clusterable, Configurable, Nameable, Queryable, Resource
- Defined in:
- lib/elasticsearch/resources/index.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_type(key:, type_class: nil) ⇒ Object
- #client ⇒ Object
- #count(body, options = {}) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #find_cluster ⇒ Object
- #find_index(index: nil) ⇒ Object
- #find_type(index: nil, type:) ⇒ Object
-
#initialize(cluster:, &block) ⇒ Index
constructor
A new instance of Index.
- #name ⇒ Object
- #put_mapping(options = {}) ⇒ Object
- #query(action, options = {}) ⇒ Object
- #query_index(action, options = {}) ⇒ Object
- #search(body, options = {}) ⇒ Object
- #setup! ⇒ Object
- #teardown! ⇒ Object
- #types ⇒ Object
Methods included from Nameable
Methods included from Clusterable
Methods included from Configurable
Methods included from Queryable
Methods included from Resource
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
.types ⇒ Object
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 |
#client ⇒ Object
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, = {}) params = { body: body }.merge() query(:count, params) end |
#create ⇒ Object
77 78 79 |
# File 'lib/elasticsearch/resources/index.rb', line 77 def create query_index(:create) end |
#delete ⇒ Object
85 86 87 |
# File 'lib/elasticsearch/resources/index.rb', line 85 def delete query_index(:delete) end |
#exists? ⇒ Boolean
73 74 75 |
# File 'lib/elasticsearch/resources/index.rb', line 73 def exists? query_index(:exists?) end |
#find_cluster ⇒ Object
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 |
#name ⇒ Object
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( = {}) query_index(:put_mapping, ) end |
#query(action, options = {}) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/elasticsearch/resources/index.rb', line 65 def query(action, = {}) params = { index: name }.merge() 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, = {}) params = { index: name }.merge() 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, = {}) params = { body: body }.merge() 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 |
#types ⇒ Object
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 |