Class: AgnosticBackend::Elasticsearch::Client
- Inherits:
-
Object
- Object
- AgnosticBackend::Elasticsearch::Client
- Defined in:
- lib/agnostic_backend/elasticsearch/client.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#describe_index_fields(index_name, type) ⇒ Object
returns an array of RemoteIndexFields (or nil).
-
#initialize(endpoint:) ⇒ Client
constructor
A new instance of Client.
-
#send_request(http_method, path: "", body: nil) ⇒ Object
sends an HTTP request to the ES server (a) a Hash (in which case it will be encoded to JSON), or (b) a string (in which case it will be assumed to contain JSON data) returns a Faraday::Response instance.
Constructor Details
#initialize(endpoint:) ⇒ Client
Returns a new instance of Client.
9 10 11 12 |
# File 'lib/agnostic_backend/elasticsearch/client.rb', line 9 def initialize(endpoint:) @endpoint = endpoint @connection = Faraday::Connection.new(url: endpoint) end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/agnostic_backend/elasticsearch/client.rb', line 7 def endpoint @endpoint end |
Instance Method Details
#describe_index_fields(index_name, type) ⇒ Object
returns an array of RemoteIndexFields (or nil)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/agnostic_backend/elasticsearch/client.rb', line 15 def describe_index_fields(index_name, type) response = send_request(:get, path: "#{index_name}/_mapping/#{type}") if response.success? body = ActiveSupport::JSON.decode(response.body) if response.body.present? return if body.blank? fields = body[index_name.to_s]["mappings"][type.to_s]["properties"] fields.map do |field_name, properties| properties = Hash[ properties.map{|k,v| [k.to_sym, v]} ] type = properties.delete(:type) AgnosticBackend::Elasticsearch::RemoteIndexField.new field_name, type, **properties end end end |
#send_request(http_method, path: "", body: nil) ⇒ Object
sends an HTTP request to the ES server (a) a Hash (in which case it will be encoded to JSON), or (b) a string (in which case it will be assumed to contain JSON data) returns a Faraday::Response instance
35 36 37 38 39 40 41 |
# File 'lib/agnostic_backend/elasticsearch/client.rb', line 35 def send_request(http_method, path: "", body: nil) body = ActiveSupport::JSON.encode(body) if body.is_a? Hash @connection.run_request(http_method.downcase.to_sym, path.to_s, body, default_headers) end |