Class: EsClient::Index
- Inherits:
-
Object
- Object
- EsClient::Index
- Defined in:
- lib/es_client/index.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #build_name(name) ⇒ Object
- #bulk(action, type, documents) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #destroy_document(type, id) ⇒ Object
- #exists? ⇒ Boolean
- #find(type, id) ⇒ Object
- #get_mapping ⇒ Object
- #get_settings ⇒ Object
-
#initialize(name, options = {}) ⇒ Index
constructor
A new instance of Index.
- #put_mapping(type, mapping) ⇒ Object
- #put_settings(settings) ⇒ Object
- #recreate ⇒ Object
- #refresh ⇒ Object
- #save_document(type, id, document) ⇒ Object
- #search(query, options = {}) ⇒ Object
- #update_document(type, id, document) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Index
Returns a new instance of Index.
5 6 7 8 |
# File 'lib/es_client/index.rb', line 5 def initialize(name, ={}) @name = build_name(name) = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/es_client/index.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/es_client/index.rb', line 3 def end |
Instance Method Details
#build_name(name) ⇒ Object
10 11 12 13 |
# File 'lib/es_client/index.rb', line 10 def build_name(name) return name unless EsClient.index_prefix "#{EsClient.index_prefix}_#{name}" end |
#bulk(action, type, documents) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/es_client/index.rb', line 76 def bulk(action, type, documents) payload = [] documents.each do |document| payload << {action => {_index: name, _type: type, _id: document[:id]}} case action when :index payload << document when :update document_for_update = {doc: document} document_for_update.update(document[:bulk_options]) if document[:bulk_options] payload << document_for_update end end serialized_payload = "\n" + payload.map(&:to_json).join("\n") + "\n" EsClient.client.post("/#{name}/#{type}/_bulk", body: serialized_payload) end |
#create ⇒ Object
24 25 26 27 |
# File 'lib/es_client/index.rb', line 24 def create = .present? ? {body: .to_json} : {} EsClient.client.post("/#{name}", ) end |
#delete ⇒ Object
29 30 31 |
# File 'lib/es_client/index.rb', line 29 def delete EsClient.client.delete("/#{name}") end |
#destroy_document(type, id) ⇒ Object
68 69 70 |
# File 'lib/es_client/index.rb', line 68 def destroy_document(type, id) EsClient.client.delete("/#{name}/#{type}/#{id}") end |
#exists? ⇒ Boolean
15 16 17 |
# File 'lib/es_client/index.rb', line 15 def exists? EsClient.client.head("/#{name}").success? end |
#find(type, id) ⇒ Object
72 73 74 |
# File 'lib/es_client/index.rb', line 72 def find(type, id) EsClient.client.get("/#{name}/#{type}/#{id}").decoded['_source'] end |
#get_mapping ⇒ Object
51 52 53 |
# File 'lib/es_client/index.rb', line 51 def get_mapping EsClient.client.get("/#{name}/_mapping").decoded[name]['mappings'] end |
#get_settings ⇒ Object
43 44 45 |
# File 'lib/es_client/index.rb', line 43 def get_settings EsClient.client.get("/#{name}/_settings").decoded[name]['settings'] end |
#put_mapping(type, mapping) ⇒ Object
55 56 57 58 |
# File 'lib/es_client/index.rb', line 55 def put_mapping(type, mapping) json = {type => mapping}.to_json EsClient.client.put("/#{name}/_mapping/#{type}", body: json) end |
#put_settings(settings) ⇒ Object
47 48 49 |
# File 'lib/es_client/index.rb', line 47 def put_settings(settings) EsClient.client.put("/#{name}/_settings", body: settings.to_json) end |
#recreate ⇒ Object
19 20 21 22 |
# File 'lib/es_client/index.rb', line 19 def recreate delete create end |
#refresh ⇒ Object
33 34 35 |
# File 'lib/es_client/index.rb', line 33 def refresh EsClient.client.post("/#{name}/_refresh") end |
#save_document(type, id, document) ⇒ Object
60 61 62 |
# File 'lib/es_client/index.rb', line 60 def save_document(type, id, document) EsClient.client.post("/#{name}/#{type}/#{id}", body: document.to_json) end |
#search(query, options = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/es_client/index.rb', line 37 def search(query, ={}) = .slice(:query, :headers) [:body] = query.to_json EsClient.client.get("/#{name}/#{options[:type]}/_search", ) end |
#update_document(type, id, document) ⇒ Object
64 65 66 |
# File 'lib/es_client/index.rb', line 64 def update_document(type, id, document) EsClient.client.post("/#{name}/#{type}/#{id}/_update", body: {doc: document}.to_json) end |