Sha256: 66dcc5e4df324c1fbda7d5d10522f2f300b1fa9236967cfc6051f55fe6219d2b

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'faraday_middleware'
require 'faraday_middleware/aws_sigv4'
require 'logger'

require_relative 'utils'
require_relative 'base'

module Kishu
  class Client 

    def initialize

      if ES_HOST == "localhost:9200" || ES_HOST == "elasticsearch:9200"
        @client = Elasticsearch::Client.new(host: ES_HOST, user: "elastic", password: ELASTIC_PASSWORD, transport_options: { request: { timeout: 3600}}) do |f|
          f.adapter Faraday.default_adapter
        end
      else
          @client = Elasticsearch::Client.new(host: ES_HOST, port: '80', scheme: 'http') do |f|
            f.request :aws_sigv4,
              service: 'es',
              region: AWS_REGION,
              access_key_id: AWS_ACCESS_KEY_ID,
              secret_access_key: AWS_SECRET_ACCESS_KEY
            f.adapter Faraday.default_adapter
          end
      end
      @client
    end


    def get options={}

      x =@client.search(body:{
          size: options[:size] ||= 0,
          query: {
            query_string: {
              query: "*"
            }
          },
          aggregations: aggregations(options)
        },
        index: ES_INDEX
        )
      x
    end

    def is_empty?
      return true unless get
      nil
    end

    def clear_index
      @client.indices.delete index: ES_INDEX
      puts "Resolutions index has been deleted"
    end


    def get_logdate options={}
      @client.search(body:{
          size: 1,
          query: {
            query_string: {
              query: "*"
            }
          }
        },
        index: "resolutions"
        ).dig("hits","hits",0,"_source","logdate")
    end

    def aggregations options={}
      {
        doi: {composite: {
          sources: [{doi: {terms: {field: :doi	}}}],
          after: { doi: options.fetch(:after_key,"")},
          size: options[:aggs_size]
          },
          aggs: {
            unique: {terms: {field: "unique_usage"}},
            totale: {terms: {field: "total_usage"	}}
          }
        }
      }
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kishu-1.0.2 lib/kishu/client.rb
kishu-1.0.1 lib/kishu/client.rb
kishu-1.0.0 lib/kishu/client.rb