lib/ej/core.rb in ej-0.0.10 vs lib/ej/core.rb in ej-0.0.12

- old
+ new

@@ -6,20 +6,27 @@ require 'hashie' require 'pp' module Ej class Core - DEFAULT_PER = 30000 + DEFAULT_PER = 1000 def initialize(host, index, debug) @logger = debug ? Logger.new($stderr) : nil @index = index @client = Elasticsearch::Client.new hosts: host, logger: @logger, index: @index end - def search(type, query, size, from, source_only, routing = nil, fields = nil) + def search(type, query, size, from, source_only, 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 = Hashie::Mash.new(@client.search(search_option)) @@ -30,12 +37,12 @@ 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 - def copy(source, dest, query) - per = DEFAULT_PER + def copy(source, dest, query, per_size) + per = per_size || DEFAULT_PER num = 0 logger = Logger.new($stdout) source_client = Elasticsearch::Client.new hosts: source, index: @index dest_client = Elasticsearch::Client.new hosts: dest while true @@ -57,15 +64,15 @@ logger.info("copy complete #{from + docs.size}/#{total}") num += 1 end end - def dump(query) - per = DEFAULT_PER + def dump(query, per_size) + per = per_size || DEFAULT_PER num = 0 - bulk_message = [] 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) docs = data.hits.hits @@ -75,12 +82,12 @@ 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 - puts bulk_message.join("\n") end def facet(term, size, query) body = {"facets"=> {"terms"=> @@ -104,10 +111,11 @@ {"query"=>{"query_string"=>{"query"=> query}}, "filter"=>{"bool"=>{"must"=>[], "must_not"=>[]}}}}, "aggs"=> {"agg_" + term => {"terms"=>{"field"=>term, "size"=>size, "order"=>{"_count"=>"desc"}}}}} - @client.search index: @index, body: body + results = @client.search index: @index, body: body + results['aggregations']["agg_" + term]['buckets'] end def min(term) body = { aggs: {