Class: Elastics::Client
- Inherits:
-
Object
- Object
- Elastics::Client
- Defined in:
- lib/elastics/client.rb,
lib/elastics/client/cluster.rb
Defined Under Namespace
Modules: Cluster
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
- #request(params) ⇒ Object
- #request_path(params) ⇒ Object
- #search(params) ⇒ Object
- #set(id, data) ⇒ Object
- #set_index(index, type = nil) ⇒ Object
Constructor Details
#initialize(defaults = {}) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/elastics/client.rb', line 12 def initialize(defaults = {}) if defaults[:host].is_a?(Array) extend Cluster initialize_cluster(defaults) else @host = defaults[:host] || '127.0.0.1:9200' end @index = defaults[:index] @type = defaults[:type] @client = HTTPClient.new end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/elastics/client.rb', line 10 def client @client end |
#index(params) ⇒ Object
107 108 109 |
# File 'lib/elastics/client.rb', line 107 def index(params) params[:id] ? put(params) : post(params) end |
#type=(value) ⇒ Object (writeonly)
Sets the attribute type
9 10 11 |
# File 'lib/elastics/client.rb', line 9 def type=(value) @type = value end |
Instance Method Details
#debug!(dev = STDOUT) ⇒ Object
24 25 26 |
# File 'lib/elastics/client.rb', line 24 def debug!(dev = STDOUT) @client.debug_dev = dev end |
#debug_off! ⇒ Object
28 29 30 |
# File 'lib/elastics/client.rb', line 28 def debug_off! @client.debug_dev = nil end |
#delete(params) ⇒ Object
75 76 77 78 |
# File 'lib/elastics/client.rb', line 75 def delete(params) delete!(params) rescue NotFound end |
#delete!(params) ⇒ Object
70 71 72 73 |
# File 'lib/elastics/client.rb', line 70 def delete!(params) params[:method] = :delete request params end |
#get(params) ⇒ Object
86 87 88 89 |
# File 'lib/elastics/client.rb', line 86 def get(params) get!(params) rescue NotFound end |
#get!(params) ⇒ Object
80 81 82 83 84 |
# File 'lib/elastics/client.rb', line 80 def get!(params) params = {id: params} unless params.is_a?(Hash) params[:method] = :get request(params) end |
#index_exists?(index) ⇒ Boolean
111 112 113 |
# File 'lib/elastics/client.rb', line 111 def index_exists?(index) !!get(index: index, type: nil, id: :_mapping) end |
#put_mapping(params) ⇒ Object
95 96 97 98 99 |
# File 'lib/elastics/client.rb', line 95 def put_mapping(params) params[:id] = :_mapping params[:method] = :put request(params) end |
#request(params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/elastics/client.rb', line 49 def request(params) method = params[:method] || :get body = params[:data].try!(:to_json) 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
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/elastics/client.rb', line 37 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) ⇒ Object
101 102 103 104 105 |
# File 'lib/elastics/client.rb', line 101 def search(params) params[:id] = :_search params[:method] = :post request(params) end |
#set(id, data) ⇒ Object
91 92 93 |
# File 'lib/elastics/client.rb', line 91 def set(id, data) request(id: id, data: data, method: :put) end |
#set_index(index, type = nil) ⇒ Object
32 33 34 35 |
# File 'lib/elastics/client.rb', line 32 def set_index(index, type = nil) @index = index || nil @type = type || nil end |