Class: Elastics::Client
- Inherits:
-
Object
- Object
- Elastics::Client
- Includes:
- Bulk
- Defined in:
- lib/elastics/client.rb,
lib/elastics/client/bulk.rb,
lib/elastics/client/cluster.rb
Defined Under Namespace
Constant Summary collapse
- HEADERS =
{'Content-Type' => 'application/json'}
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
- #index(params) ⇒ Object
-
#type ⇒ Object
writeonly
Sets the attribute type.
Instance Method Summary collapse
- #debug!(dev = STDOUT) ⇒ Object
- #debug_off! ⇒ Object
- #delete(params) ⇒ Object
- #delete!(params) ⇒ Object
- #get(params) ⇒ Object
- #get!(params) ⇒ Object
- #index_exists?(index) ⇒ Boolean
-
#initialize(defaults = {}) ⇒ Client
constructor
A new instance of Client.
- #put_mapping(params) ⇒ Object
- #refresh(*args) ⇒ Object
- #refresh!(index = nil) ⇒ Object
- #request(params = {}) ⇒ Object
- #request_path(params) ⇒ Object
- #search(params, *args) ⇒ Object
- #set(id, data) ⇒ Object
- #set_index(index, type = nil) ⇒ Object
Methods included from Bulk
Constructor Details
#initialize(defaults = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/elastics/client.rb', line 16 def initialize(defaults = {}) @index = defaults[:index] @type = defaults[:type] @client = HTTPClient.new if defaults[:host].is_a?(Array) extend Cluster initialize_cluster(defaults) else @host = defaults[:host] || '127.0.0.1:9200' end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
14 15 16 |
# File 'lib/elastics/client.rb', line 14 def client @client end |
#index(params) ⇒ Object
113 114 115 |
# File 'lib/elastics/client.rb', line 113 def index(params) params[:id] ? put(params) : post(params) end |
#type=(value) ⇒ Object (writeonly)
Sets the attribute type
13 14 15 |
# File 'lib/elastics/client.rb', line 13 def type=(value) @type = value end |
Instance Method Details
#debug!(dev = STDOUT) ⇒ Object
28 29 30 |
# File 'lib/elastics/client.rb', line 28 def debug!(dev = STDOUT) client.debug_dev = dev end |
#debug_off! ⇒ Object
32 33 34 |
# File 'lib/elastics/client.rb', line 32 def debug_off! client.debug_dev = nil end |
#delete(params) ⇒ Object
81 82 83 84 |
# File 'lib/elastics/client.rb', line 81 def delete(params) delete!(params) rescue NotFound end |
#delete!(params) ⇒ Object
75 76 77 78 79 |
# File 'lib/elastics/client.rb', line 75 def delete!(params) params = {id: params} unless params.is_a?(Hash) params[:method] = :delete request params end |
#get(params) ⇒ Object
92 93 94 95 |
# File 'lib/elastics/client.rb', line 92 def get(params) get!(params) rescue NotFound end |
#get!(params) ⇒ Object
86 87 88 89 90 |
# File 'lib/elastics/client.rb', line 86 def get!(params) params = {id: params} unless params.is_a?(Hash) params[:method] = :get request(params) end |
#index_exists?(index) ⇒ Boolean
117 118 119 |
# File 'lib/elastics/client.rb', line 117 def index_exists?(index) !!get(index: index, type: nil, id: :_mapping) end |
#put_mapping(params) ⇒ Object
101 102 103 104 105 |
# File 'lib/elastics/client.rb', line 101 def put_mapping(params) params[:id] = :_mapping params[:method] = :put request(params) end |
#refresh(*args) ⇒ Object
125 126 127 128 |
# File 'lib/elastics/client.rb', line 125 def refresh(*args) refresh!(*args) rescue NotFound end |
#refresh!(index = nil) ⇒ Object
121 122 123 |
# File 'lib/elastics/client.rb', line 121 def refresh!(index = nil) request(method: :post, index: index, type: nil, id: :_refresh) end |
#request(params = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/elastics/client.rb', line 53 def request(params = {}) method = params[:method] || :get body = params[:body] body = body.to_json if body && !body.is_a?(String) res = http_request(method, request_path(params), params[:query], body, params) status = res.status return JSON.parse(res.body) if 300 > status result = JSON.parse(res.body) rescue nil err_msg = "#{res.reason}: #{result && result['error'] || '-'}" # NotFound is raised only for valid responses from ElasticSearch raise NotFound, err_msg if 404 == status && result raise Error, err_msg end |
#request_path(params) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/elastics/client.rb', line 41 def request_path(params) str = "" if index = params[:index] || @index str << "/#{index}" type = params[:type] || @type str << "/#{type}" if type end path = params[:id] str << "/#{path}" if path str end |
#search(params, *args) ⇒ Object
107 108 109 110 111 |
# File 'lib/elastics/client.rb', line 107 def search(params, *args) params[:id] = :_search params[:method] = :post Result::Search.new(request(params), *args) end |
#set(id, data) ⇒ Object
97 98 99 |
# File 'lib/elastics/client.rb', line 97 def set(id, data) request(id: id, body: data, method: :put) end |
#set_index(index, type = nil) ⇒ Object
36 37 38 39 |
# File 'lib/elastics/client.rb', line 36 def set_index(index, type = nil) @index = index || nil @type = type || nil end |