Sha256: dbaf22028e1bbbbea44e9c7d5abad0a3ebcfe9e6b134ab61c540b95a410a02d0

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

require 'faraday'

module AgnosticBackend
  module Elasticsearch

    class Client
      attr_reader :endpoint

      def initialize(endpoint:)
        @endpoint = endpoint
        @connection = Faraday::Connection.new(url: endpoint)
      end

      # returns an array of RemoteIndexFields (or nil)
      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

      # 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
      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

      def default_headers
        {'Content-Type' => 'application/json'}
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
agnostic_backend-1.0.4 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-1.0.3 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-1.0.2 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-1.0.1 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-1.0.0 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-0.9.9 lib/agnostic_backend/elasticsearch/client.rb
agnostic_backend-0.9.8 lib/agnostic_backend/elasticsearch/client.rb