lib/ej/commands.rb in ej-0.1.4 vs lib/ej/commands.rb in ej-0.1.5

- old
+ new

@@ -1,7 +1,5 @@ -#!/usr/bin/env ruby -# coding: utf-8 require 'ej/core' require 'thor' require 'yajl' module Ej @@ -15,33 +13,36 @@ map '-c' => :count map '-b' => :bulk map '-l' => :indices map '-a' => :aliases map '-m' => :mapping - map '-e' => :debug_eval map '--health' => :health def initialize(args = [], options = {}, config = {}) super(args, options, config) @global_options = config[:shell].base.options - @core = Ej::Core.new(@global_options[:host], @global_options[:index], @global_options[:debug]) + values = Values.new(@global_options) + @core = Ej::Core.new(values) + @indices = Ej::Indices.new(values) + @cluster = Ej::Cluster.new(values) + @nodes = Ej::Nodes.new(values) end desc '-s [lucene query]', 'search' option :type, type: :string, aliases: '-t', default: nil, desc: 'type' option :size, type: :numeric, aliases: '-n', default: nil, desc: 'size' option :from, type: :numeric, aliases: '--from', default: 0, desc: 'from' option :fields, type: :array, aliases: '--fields', default: nil, desc: 'fields' - option :source_only, type: :boolean, aliases: '--so', default: true, desc: 'from' + option :meta, type: :boolean, default: false, desc: 'meta' option :query, type: :string, aliases: '-q', default: nil, desc: 'query' option :sort, type: :hash, aliases: '--sort', default: nil, desc: 'ex. --sort @timestamp:desc' def search(query = options[:query]) puts_with_format(@core.search(options[:type], query, options[:size], options[:from], - options[:source_only], + options[:meta], nil, options[:fields], options[:sort] )) end @@ -63,20 +64,18 @@ desc 'copy', 'copy index' option :source, type: :string, aliases: '--source', required: true, desc: 'source host' option :dest, type: :string, aliases: '--dest', required: true, desc: 'dest host' option :query, type: :string, aliases: '-q', default: nil, desc: 'query' option :per, type: :numeric, default: nil, desc: 'per' - option :proc_num, type: :numeric, default: 1, desc: 'proc num for multi thread' - option :from, type: :numeric, default: 0, desc: 'from' + option :scroll, type: :string, default: "1m", desc: 'scroll' def copy @core.copy( options[:source], options[:dest], options[:query], options[:per], - options[:proc_num], - options[:from] + options[:scroll] ) end desc 'dump', 'dump index' option :query, type: :string, aliases: '-q', default: nil, desc: 'query' @@ -110,125 +109,137 @@ option :term, type: :string, aliases: '-k', desc: 'terms' def max puts_with_format(@core.max(options[:term])) end - desc '-b', 'bulk import STDIN JSON' + desc '-b', 'bulk import JSON' option :index, aliases: '-i', type: :string, default: "logstash-#{Time.now.strftime('%Y.%m.%d')}", required: true, desc: 'index' option :type, type: :string, aliases: '-t', default: nil, required: true, desc: 'type' option :timestamp_key, aliases: '--timestamp_key', type: :string, desc: 'timestamp key', default: nil option :add_timestamp, type: :boolean, default: true, desc: 'add_timestamp' option :id_keys, type: :array, aliases: '--id', default: nil, desc: 'id' + option :inputs, type: :array, aliases: '--inputs', default: [], desc: 'input files path.' def bulk - @core.bulk(options[:timestamp_key], options[:type], options[:add_timestamp], options[:id_keys], options[:index]) + inputs = options[:inputs].empty? ? [STDIN] : options[:inputs] + inputs.each do |key| + buffer = (key.class == STDIN.class) ? STDIN.read : File.read(key) + @core.bulk( + options[:timestamp_key], + options[:type], + options[:add_timestamp], + options[:id_keys], + options[:index], + Util.parse_json(buffer) + ) + end end desc 'health', 'health' def health - puts_with_format(@core.health) + puts_with_format(@cluster.health) end desc '-a', 'list aliases' def aliases - puts_with_format(@core.aliases) + puts_with_format(@indices.aliases) end desc 'state', 'state' def state - puts_with_format(@core.state) + puts_with_format(@cluster.state) end desc 'indices', 'show indices summary' def indices - puts_with_format(@core.indices) + puts_with_format(@indices.indices) end desc 'stats', 'index stats' def stats - puts_with_format(@core.stats) + puts_with_format(@indices.stats) end desc 'mapping', 'show mapping' def mapping - puts_with_format(@core.mapping) + puts_with_format(@indices.mapping) end desc 'not_analyzed', 'not analyzed' def not_analyzed json = File.read(File.expand_path('../../../template/not_analyze_template.json', __FILE__)) hash = Yajl::Parser.parse(json) - puts_with_format(@core.put_template('ej_init', hash)) + puts_with_format(@indices.put_template('ej_init', hash)) end desc 'put_routing', "put routing.\nexsample. ej put_routing -i someindex -t sometype --path somecolumn" option :index, aliases: '-i', type: :string, default: nil, required: true, desc: 'index' option :type, aliases: '-t', type: :string, default: nil, required: true, desc: 'type' option :path, type: :string, default: nil, required: true, desc: 'path' def put_routing body = { options[:type] => {"_routing"=>{"required"=>true, "path"=>options[:path]}}} - puts_with_format(@core.put_mapping(options[:index], options[:type], body)) + puts_with_format(@indices.put_mapping(options[:index], options[:type], body)) end desc 'put_template', 'put template' def put_template(name) hash = Yajl::Parser.parse(STDIN.read) - puts_with_format(@core.put_template(name, hash)) + puts_with_format(@indices.put_template(name, hash)) end desc 'create_aliases', 'create aliases' option :alias, type: :string, aliases: '-a', default: nil, required: true, desc: 'alias name' option :indices, type: :array, aliases: '-x', default: nil, required: true, desc: 'index array' def create_aliases - @core.create_aliases(options[:alias], options[:indices]) + @indices.create_aliases(options[:alias], options[:indices]) end desc 'recovery', 'recovery' def recovery - @core.recovery + @indices.recovery end desc 'delete', 'delete' option :index, aliases: '-i', type: :string, default: nil, required: true, desc: 'index' option :type, type: :string, aliases: '-t', default: nil, desc: 'type' - option :query, type: :string, aliases: '-q', default: nil, desc: 'query' + option :query, type: :hash, aliases: '-q', default: nil, desc: 'query' def delete - @core.delete(options[:index], options[:type], options[:query]) + @indices.delete(options[:index], options[:type], options[:query]) end desc 'delete_template --name [name]', 'delete_template' option :name, type: :string, default: nil, required: true, desc: 'template name' def delete_template - @core.delete_template(options[:name]) + @indices.delete_template(options[:name]) end desc 'template', 'show template' def template - puts_with_format(@core.template) + puts_with_format(@indices.template) end desc 'settings', 'show template' def settings - puts_with_format(@core.settings) + puts_with_format(@indices.settings) end desc 'warmer', 'warmer' def warmer - puts_with_format(@core.warmer) + puts_with_format(@indices.warmer) end desc 'refresh', 'refresh' def refresh - puts_with_format(@core.refresh) + puts_with_format(@indices.refresh) end desc 'nodes_info', 'view nodes info' def nodes_info - puts_with_format @core.nodes_info + puts_with_format @nodes.nodes_info end desc 'nodes_stats', 'view nodes stats' def nodes_stats - puts_with_format @core.nodes_stats + puts_with_format @nodes.nodes_stats end private def puts_with_format(object)