Class: Kishu::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kishu/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kishu/client.rb', line 11

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

Instance Method Details

#aggregations(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kishu/client.rb', line 71

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

#clear_indexObject



52
53
54
55
# File 'lib/kishu/client.rb', line 52

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

#get(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kishu/client.rb', line 31

def get options={}

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

#get_logdate(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kishu/client.rb', line 58

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

#is_empty?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/kishu/client.rb', line 47

def is_empty?
  return true unless get
  nil
end