class AgnosticBackend::Elasticsearch::Client

Attributes

endpoint[R]

Public Class Methods

new(endpoint:) click to toggle source
# File lib/agnostic_backend/elasticsearch/client.rb, line 9
def initialize(endpoint:)
  @endpoint = endpoint
  @connection = Faraday::Connection.new(url: endpoint)
end

Public Instance Methods

describe_index_fields(index_name, type) click to toggle source

returns an array of RemoteIndexFields (or nil)

# 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) click to toggle source

sends an HTTP request to the ES server @body is taken to be either (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

# 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

Private Instance Methods

default_headers() click to toggle source
# File lib/agnostic_backend/elasticsearch/client.rb, line 45
def default_headers
  {'Content-Type' => 'application/json'}
end