Class: Ej::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/ej/core.rb

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Core

Returns a new instance of Core.



13
14
15
16
17
# File 'lib/ej/core.rb', line 13

def initialize(values)
  @logger =  values.logger
  @index = values.index
  @client = values.client
end

Instance Method Details

#aggs(terms, size, query) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ej/core.rb', line 81

def aggs(terms, size, query)
  body = {
    "size"=>0,
    "query"=>{
      "query_string"=>{
        "query"=>query
      }
    }
  }

  agg_terms = []
  code = %Q{['aggs']}
  terms.each_with_index do |term, i|
    term_name = "agg_#{term}"
    aggs_body = {
      term_name=>{
        "terms"=>{
          "field"=>term,
          "size"=>size,
          "order"=>{
            "_count"=>"desc"
          }
        }
      }
    }

    eval(%Q{body#{code} = aggs_body})
    code += %Q{['#{term_name}']['aggs']}
  end

  @client.search index: @index, body: body
end

#bulk(timestamp_key, type, add_timestamp, id_keys, index, data) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ej/core.rb', line 132

def bulk(timestamp_key, type, add_timestamp, id_keys, index, data)
  template = id_keys.map { |key| '%s' }.join('_') unless id_keys.nil?
  bulk_message = []
  data.each do |record|
    if timestamp_key.nil?
      timestamp = Time.now.to_datetime.to_s
    else
      timestamp = record[timestamp_key].to_time.to_datetime.to_s
    end
    record.merge!('@timestamp' => timestamp) if add_timestamp
    meta = { index: { _index: index, _type: type } }
    meta[:index][:_id] = Util.generate_id(template, record, id_keys) unless id_keys.nil?
    bulk_message << meta
    bulk_message << record
  end
  connect_with_retry { @client.bulk body: bulk_message unless bulk_message.empty? }
end

#copy(source, dest, query, per_size, scroll) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ej/core.rb', line 150

def copy(source, dest, query, per_size, scroll)
  source_client = Elasticsearch::Client.new hosts: source
  dest_client = Elasticsearch::Client.new hosts: dest

  scroll_option = get_scroll_option(@index, query, per_size, scroll)
  r = connect_with_retry { source_client.search(scroll_option) }
  total = r['hits']['total']
  i = 0
  i += bulk_results(r, dest_client, i, total)

  while r = connect_with_retry { source_client.scroll(scroll_id: r['_scroll_id'], scroll: scroll) } and
    (not r['hits']['hits'].empty?) do
    i += bulk_results(r, dest_client, i, total)
  end
end

#distinct(term, type, query) ⇒ Object



37
38
39
40
41
# File 'lib/ej/core.rb', line 37

def distinct(term, type, query)
  body = { size: 0, "aggs"=>{ term + "_count"=>{"cardinality"=>{"field"=>term}}}}
  body[:query] = { query_string: { query: query } } unless query.nil?
  @client.search index: @index, type: type, body: body
end

#dump(query, per_size) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ej/core.rb', line 43

def dump(query, per_size)
  per = per_size || DEFAULT_PER
  num = 0
  while true
    bulk_message = []
    from = num * per
    body = { size: per, from: from }
    body[:query] = { query_string: { query: query } } unless query.nil?
    data = HashWrapper.new(@client.search index: @index, body: body)
    docs = data.hits.hits
    break if docs.empty?
    docs.each do |doc|
      source = doc.delete('_source')
      doc.delete('_score')
      bulk_message << Yajl::Encoder.encode({ 'index' => doc.to_h })
      bulk_message << Yajl::Encoder.encode(source)
    end
    num += 1
    puts bulk_message.join("\n")
  end
end

#facet(term, size, query) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ej/core.rb', line 65

def facet(term, size, query)
  body = {"facets"=>
    {"terms"=>
      {"terms"=>{"field"=>term, "size"=>size, "order"=>"count", "exclude"=>[]},
       "facet_filter"=>
        {"fquery"=>
          {"query"=>
            {"filtered"=>
              {"query"=>
                {"bool"=>
                  {"should"=>[{"query_string"=>{"query"=>query}}]}},
               "filter"=>{"bool"=>{"must"=>[{"match_all"=>{}}]}}}}}}}},
   "size"=>0}
  @client.search index: @index, body: body
end

#max(term) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/ej/core.rb', line 123

def max(term)
  body = {
    aggs: {
      "max_#{term}" => { max: { field: term } }
    }
  }
  @client.search index: @index, body: body, size: 0
end

#min(term) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/ej/core.rb', line 114

def min(term)
  body = {
    aggs: {
      "min_#{term}" => { min: { field: term } }
    }
  }
  @client.search index: @index, body: body, size: 0
end

#search(type, query, size, from, meta, routing = nil, fields = nil, sort = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ej/core.rb', line 19

def search(type, query, size, from, meta, routing = nil, fields = nil, sort = nil)
  body = { from: from }
  body[:size] = size unless size.nil?
  if sort
    sorts = []
    sort.each do |k, v|
      sorts << { k => v }
    end
    body[:sort] = sorts
  end
  body[:query] = { query_string: { query: query } } unless query.nil?
  search_option = { index: @index, type: type, body: body }
  search_option[:routing] = routing unless routing.nil?
  search_option[:_source] = fields.nil? ? nil : fields.join(',')
  results = HashWrapper.new(@client.search(search_option))
  meta ? results : Util.get_sources(results)
end