lib/ej/core.rb in ej-0.0.4 vs lib/ej/core.rb in ej-0.0.5

- old
+ new

@@ -22,25 +22,34 @@ search_option[:routing] = routing unless routing.nil? results = Hashie::Mash.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}}}} + body[:query] = { query_string: { query: query } } unless query.nil? + @client.search index: @index, type: type, body: body + end + def move(source, dest, query) per = 30000 source_client = Elasticsearch::Client.new hosts: source, index: @index, logger: @logger dest_client = Elasticsearch::Client.new hosts: dest, logger: @logger num = 0 while true from = num * per 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) + p data.hits.hits.size break if data.hits.hits.empty? bulk_message = [] data.hits.hits.each do |doc| - bulk_message << { 'index' => { '_index' => doc._index, '_type' => doc._type, '_id' => doc._id } } - bulk_message << doc._source + source = doc.delete('_source') + doc.delete('_score') + bulk_message << { index: doc.to_h } + bulk_message << source end dest_client.bulk body: bulk_message unless bulk_message.empty? num += 1 end end @@ -112,13 +121,9 @@ @client.cluster.state end def indices @client.cat.indices format: 'json' - end - - def count - @client.cat.count index: @index, format: 'json' end def stats @client.indices.stats index: @index end