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) @options = 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 @options 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/es_client/index.rb', line 77 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 = @options.present? ? {body: @options.to_json} : {} EsClient.client.post!("/#{name}", ) end |
#delete ⇒ Object
29 30 31 32 |
# File 'lib/es_client/index.rb', line 29 def delete return unless exists? EsClient.client.delete!("/#{name}") end |
#destroy_document(type, id) ⇒ Object
69 70 71 |
# File 'lib/es_client/index.rb', line 69 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
73 74 75 |
# File 'lib/es_client/index.rb', line 73 def find(type, id) EsClient.client.get("/#{name}/#{type}/#{id}").decoded['_source'] end |
#get_mapping ⇒ Object
52 53 54 |
# File 'lib/es_client/index.rb', line 52 def get_mapping EsClient.client.get("/#{name}/_mapping").decoded[name]['mappings'] end |
#get_settings ⇒ Object
44 45 46 |
# File 'lib/es_client/index.rb', line 44 def get_settings EsClient.client.get("/#{name}/_settings").decoded[name]['settings'] end |
#put_mapping(type, mapping) ⇒ Object
56 57 58 59 |
# File 'lib/es_client/index.rb', line 56 def put_mapping(type, mapping) json = {type => mapping}.to_json EsClient.client.put("/#{name}/_mapping/#{type}", body: json) end |
#put_settings(settings) ⇒ Object
48 49 50 |
# File 'lib/es_client/index.rb', line 48 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
34 35 36 |
# File 'lib/es_client/index.rb', line 34 def refresh EsClient.client.post("/#{name}/_refresh") end |
#save_document(type, id, document) ⇒ Object
61 62 63 |
# File 'lib/es_client/index.rb', line 61 def save_document(type, id, document) EsClient.client.post("/#{name}/#{type}/#{id}", body: document.to_json) end |