lib/ej/core.rb in ej-0.1.3 vs lib/ej/core.rb in ej-0.1.4
- old
+ new
@@ -1,14 +1,17 @@
#!/usr/bin/env ruby
# coding: utf-8
-require 'yaml'
require 'yajl'
require 'elasticsearch'
require 'hashie'
-require 'pp'
require 'parallel'
+require 'logger'
+class HashWrapper < ::Hashie::Mash
+ disable_warnings if respond_to?(:disable_warnings)
+end
+
module Ej
class Core
DEFAULT_PER = 1000
def initialize(host, index, debug)
@logger = Logger.new($stderr)
@@ -30,11 +33,11 @@
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 = Hashie::Mash.new(@client.search(search_option))
+ results = HashWrapper.new(@client.search(search_option))
source_only ? get_sources(results) : results
end
def distinct(term, type, query)
body = { size: 0, "aggs"=>{ term + "_count"=>{"cardinality"=>{"field"=>term}}}}
@@ -47,22 +50,22 @@
logger = Logger.new($stdout)
source_client = Elasticsearch::Client.new hosts: source, index: @index
dest_client = Elasticsearch::Client.new hosts: dest
calculate_body = { size: 0 }
calculate_body[:query] = { query_string: { query: query } } unless query.nil?
- calculate_data = Hashie::Mash.new(source_client.search index: @index, body: calculate_body)
+ calculate_data = HashWrapper.new(source_client.search index: @index, body: calculate_body)
total = calculate_data.hits.total
payloads = ((total/per) + 1).times.to_a
Parallel.map(payloads, in_processes: proc_num) do |num|
from = num * per
if from < define_from
logger.info("skip index (#{num} #{from}-#{from + per})/#{total}")
next
end
body = { size: per, from: from }
body[:query] = { query_string: { query: query } } unless query.nil?
- data = Hashie::Mash.new(source_client.search index: @index, body: body)
+ data = HashWrapper.new(source_client.search index: @index, body: body)
docs = data.hits.hits
bulk_message = []
docs.each do |doc|
source = doc.delete('_source')
doc.delete('_score')
@@ -84,10 +87,10 @@
while true
bulk_message = []
from = num * per
body = { size: per, from: from }
body[:query] = { query_string: { query: query } } unless query.nil?
- data = Hashie::Mash.new(@client.search index: @index, body: body)
+ 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')