elastics-client 1.1.11 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ # These methods are available as Elastics.<method>(*vars)
2
+ # you can get the updated full reference and usage example of these methods
3
+ # by just doing in the console:
4
+ # Elastics.doc
5
+
6
+
7
+
8
+ #-----------------------------------------------------------------------------#
9
+ ### Search ###
10
+ # The search api is covered by the templating system
11
+
12
+
13
+
14
+ #-----------------------------------------------------------------------------#
15
+ ### Search Shards ###
16
+
17
+ search_shards:
18
+ - GET
19
+ - /<<index>>/<<type>>/_search_shards
20
+ -
21
+ - REFERENCES:
22
+ group: search_api
23
+ api_name: Search Shards
24
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-shards.html
25
+ notice: "You can pass the parameters as the :params variable."
26
+
27
+
28
+
29
+ #-----------------------------------------------------------------------------#
30
+ ### Multi Search ###
31
+ # the Multi Search api is covered by the <YourClass>.multi_search method
32
+ # added by the Elastics::Templates module
33
+
34
+
35
+
36
+ #-----------------------------------------------------------------------------#
37
+ ### Count ###
38
+
39
+ count:
40
+ - GET
41
+ - /<<index>>/<<type>>/_count
42
+ -
43
+ - REFERENCES:
44
+ group: search_api
45
+ api_name: Search Shards
46
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html
47
+ notice: "You must pass the query to validate as the :data variable. You can pass the parameters as the :params variable."
48
+
49
+
50
+
51
+ #-----------------------------------------------------------------------------#
52
+ ### Search Exists ###
53
+
54
+ search_exists:
55
+ - GET
56
+ - /<<index>>/<<type>>/<<id= ~ >>/_search/exists
57
+ -
58
+ - REFERENCES:
59
+ group: search_api
60
+ api_name: Search Exists
61
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-exists.html
62
+ notice: "You must pass the query to validate as the :data variable. You can pass the parameters as the :params variable."
63
+
64
+
65
+
66
+ #-----------------------------------------------------------------------------#
67
+ ### Validate ###
68
+
69
+ validate:
70
+ - GET
71
+ - /<<index>>/<<type>>/<<id= ~ >>/_validate/query
72
+ -
73
+ - REFERENCES:
74
+ group: search_api
75
+ api_name: Validate
76
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html
77
+ notice: "You must pass the query to validate as the :data variable. You can pass the parameters as the :params variable."
78
+
79
+
80
+
81
+ #-----------------------------------------------------------------------------#
82
+ ### Explain ###
83
+
84
+ explain:
85
+ - GET
86
+ - /<<index>>/<<type>>/<<id= ~ >>/_explain
87
+ -
88
+ - REFERENCES:
89
+ group: search_api
90
+ api_name: Validate
91
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html
92
+ notice: "You must pass the query to explain as the :data variable. You can pass the parameters as the :params variable."
93
+
94
+
95
+
96
+ #-----------------------------------------------------------------------------#
97
+ ### Percolator ###
98
+
99
+ delete_percolator:
100
+ - DELETE
101
+ - /<<index>>/.percolator/<<id>>
102
+ -
103
+ - REFERENCES: &percolator
104
+ group: search_api
105
+ api_name: Percolator
106
+ api_url: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-percolate.html
107
+
108
+ percolate:
109
+ - GET
110
+ - /<<index>>/<<type>>/_percolate
111
+ -
112
+ - &percolator_ref
113
+ REFERENCES:
114
+ <<: *percolator
115
+ notice: "You must pass the the document (and additional queries) as :data variable."
116
+
117
+ percolate_count:
118
+ - GET
119
+ - /<<index>>/<<type>>/_percolate/count
120
+ -
121
+ - *percolator_ref
122
+
123
+ put_percolator:
124
+ - PUT
125
+ - /<<index>>/.percolator/<<id>>
126
+ -
127
+ - *percolator_ref
128
+
129
+
130
+
131
+ #-----------------------------------------------------------------------------#
132
+ ### More Like This ###
133
+
134
+ more_like_this:
135
+ - GET
136
+ - /<<index>>/<<type>>/<<id>>/_mlt
137
+ -
138
+ - REFERENCES:
139
+ group: search_api
140
+ api_name: More Like This
141
+ api_url: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-more-like-this.html
142
+ notice: "You can pass the search API as :data variable."
143
+ aliases:
144
+ - :mlt
@@ -5,8 +5,10 @@ module Elastics
5
5
  attr_accessor :variables
6
6
 
7
7
  def initialize(context, vars={})
8
- @variables = Vars.new({:context => context,
9
- :index => Conf.variables[:index]}.merge(vars))
8
+ v = {:context => context}
9
+ # support for elastics-rails index default
10
+ v[:index] = Conf.variables[:index] if Conf.variables.has_key?(:index)
11
+ @variables = Vars.new(v, vars)
10
12
  end
11
13
 
12
14
  def init; end
@@ -15,19 +15,24 @@ module Elastics
15
15
  # accepts a path to a file or YAML string
16
16
  def load_source_for(klass, source, source_vars)
17
17
  if source.nil? || source !~ /\n/m
18
- paths = [ "#{Conf.elastics_dir}/#{source}.yml",
19
- "#{Conf.elastics_dir}/#{Utils.class_name_to_type(context.name)}.yml",
20
- source.to_s ]
21
- source = paths.find {|p| File.exist?(p)}
18
+ base_names = [source, Utils.class_name_to_path(context.name), Utils.class_name_to_type(context.name)]
19
+ paths = base_names.map{|bn| ["#{Conf.elastics_dir}/#{bn}.yml", "#{Conf.elastics_dir}/#{bn}.yml.erb"]}.flatten
20
+ paths << source.to_s
21
+ source = paths.find {|p| File.exist?(p)}
22
22
  end
23
- raise ArgumentError, "expected a string, got #{source.inspect}" \
24
- unless source.is_a?(String)
23
+ raise ArgumentError, "Unable to load the source: expected a string, got #{source.inspect}" \
24
+ unless source.is_a?(String)
25
25
  @sources << [klass, source, source_vars]
26
26
  do_load_source(klass, source, source_vars)
27
27
  # fixes the legacy empty stubs, which should call super instead
28
- @templates.keys.each do |name|
29
- meta_context.send(:define_method, name){|*vars| super *vars }
30
- end
28
+ # @templates.keys.each do |name|
29
+ # meta_context.send(:define_method, name){|*vars| super *vars }
30
+ # end
31
+ end
32
+
33
+ # loads a API Template source
34
+ def load_api_source(source=nil, source_vars=nil)
35
+ load_source_for(Elastics::Template::Api, source, source_vars)
31
36
  end
32
37
 
33
38
  # loads a Generic Template source
@@ -3,86 +3,155 @@ module Elastics
3
3
  module Templates
4
4
  module Doc
5
5
 
6
+ class Output
7
+
8
+
9
+ attr_reader :template, :method_call
10
+
11
+ def initialize(name, proxy)
12
+ @name, @template = proxy.templates.find do |n, t|
13
+ t.is_a?(Elastics::Template::Api) && t.aliases.include?(name)
14
+ end || [ name, proxy.templates[name]]
15
+ @method_call = [proxy.context, @name].join('.')
16
+
17
+ sources = []
18
+ sources << { :class => @template.class,
19
+ :source => @template.to_source }
20
+ @template.partials.each do |name|
21
+ partial = proxy.partials[name]
22
+ sources << { :class => partial.class,
23
+ :source => partial.to_source }
24
+ end
25
+ @sources = sources
26
+ end
27
+
28
+
29
+ def render_usage(indent='')
30
+ variables = @template.instance_eval do
31
+ interpolate
32
+ @base_variables.deep_merge @host_elastics && @host_elastics.variables, @temp_variables
33
+ end
34
+ all_tags = @template.tags + @template.partials
35
+ return @method_call if all_tags.size == 0
36
+ lines = all_tags.map do |t|
37
+ comments = 'partial' if t.to_s[0] == '_'
38
+ line = ['', t.inspect]
39
+ line + if variables.has_key?(t)
40
+ ["#{variables[t].inspect},", comments_to_s(comments)]
41
+ else
42
+ ["#{to_code(t)},", comments_to_s(comments, 'required')]
43
+ end
44
+ end
45
+ lines.sort! { |a,b| b[3] <=> a[3] }
46
+ lines.first[0] = @method_call
47
+ lines.last[2].chop!
48
+ max = lines.transpose.map{ |c| c.map(&:length).max }
49
+ formatted = lines.map{ |line| "%-#{max[0]}s %-#{max[1]}s => %-#{max[2]}s %s" % line }
50
+ indented = formatted.shift
51
+ indented = [indented] + formatted.map{ |line| indent + line }
52
+ indented.join("\n") + "\n "
53
+ end
54
+
55
+
56
+ def comments_to_s(*comments)
57
+ comments = comments.compact
58
+ return '' if comments == []
59
+ "# #{comments.join(' ')}"
60
+ end
61
+
62
+
63
+ def to_code(name)
64
+ keys = name.to_s.split('.').map{ |s| s =~ /^[0..9]+$/ ? s.to_i : s.to_sym }
65
+ code = keys.shift.to_s
66
+ return code if keys.empty?
67
+ keys.each { |k| code << "[#{k.inspect}]" }
68
+ code
69
+ end
70
+
71
+
72
+ def render_sources
73
+ @sources.map do |source|
74
+ <<-output.gsub(/^ {14}/m,'')
75
+ #{'-' * source[:class].name.length}
76
+ #{source[:class]}
77
+ #{source[:source]}
78
+ output
79
+ end.join("\n")
80
+ end
81
+
82
+
83
+ def render_api
84
+ notice = if @template.is_a?(Elastics::Template::Api) && @template.references['notice']
85
+ "\nNotice: #{@template.references['notice']}\n "
86
+ end
87
+ <<-output.gsub(/^ {12}/m,'')
88
+ ########## #{@method_call} ##########
89
+
90
+ API Name: #{@template.references['api_name']}
91
+ API URL: #{@template.references['api_url']}
92
+
93
+ #{render_sources}
94
+ Usage:
95
+ #{render_usage(' ' * 12)}#{ notice }
96
+ output
97
+ end
98
+
99
+
100
+ def render_custom
101
+ <<-output.gsub(/^ {12}/m,'')
102
+ ########## #{@method_call} ##########
103
+
104
+ #{render_sources}
105
+
106
+ Usage:
107
+ #{render_usage(' ' * 12)}
108
+ output
109
+ end
110
+
111
+
112
+ def render_stub
113
+ aliased = if @template.is_a?(Elastics::Template::Api) && @template.references['aliases']
114
+ "\n# also aliased by: #{@template.aliases.map(&:inspect).join(', ')}"
115
+ end
116
+ <<-output.gsub(/^ {12}/m,'')
117
+ def self.#{@name}(*vars)
118
+ ## this is a stub, used for reference
119
+ super
120
+ end#{ aliased }
121
+ output
122
+ end
123
+
124
+
125
+ def render
126
+ output = (@template.is_a?(Elastics::Template::Api) ? render_api : render_custom)
127
+ output = output.split("\n").map{ |l| '# ' + l }.join("\n")
128
+ output << "\n#{render_stub}\n\n"
129
+ output
130
+ end
131
+
132
+ end
133
+
134
+
6
135
  def doc(*names)
7
136
  names = templates.keys if names.empty?
8
- doc = "\n"
137
+ doc = "\n"
9
138
  names.each do |name|
10
- next unless templates.include?(name)
11
- block = ''
12
- temp = templates[name]
13
- meth_call = [context, name].join('.')
14
- block << <<-meth_call
15
- ########## #{meth_call} ##########
16
- #{'-' * temp.class.to_s.length}
17
- #{temp.class}
18
- #{temp.to_source}
19
- meth_call
20
- temp.partials.each do |par_name|
21
- par = partials[par_name]
22
- block << <<-partial
23
- #{'-' * par.class.to_s.length}
24
- #{par.class}
25
- #{par.to_source}
26
- partial
27
- end
28
- block << "\nUsage:\n"
29
- block << build_usage(meth_call, temp)
30
- block << "\n "
31
- doc << block.split("\n").map{|l| '# ' + l}.join("\n")
32
- doc << <<-meth.gsub(/^ {14}/m,'')
33
-
34
- def #{meth_call}(*vars)
35
- ## this is a stub, used for reference
36
- super
37
- end
38
-
39
- meth
139
+ next unless context.respond_to?(name)
140
+ doc << Output.new(name, self).render
40
141
  end
41
142
  Prompter.say_log doc
42
143
  end
43
144
 
44
- def usage(name)
45
- meth_call = [context, name].join('.')
46
- Prompter.say_log build_usage(meth_call, templates[name])
47
- end
48
145
 
49
- private
50
-
51
- def build_usage(meth_call, temp)
52
- variables = temp.instance_eval do
53
- interpolate
54
- @base_variables.deep_merge @host_elastics && @host_elastics.variables, @temp_variables
55
- end
56
- all_tags = temp.tags + temp.partials
57
- return meth_call if all_tags.size == 0
58
- lines = all_tags.map do |t|
59
- comments = 'partial' if t.to_s[0] == '_'
60
- line = ['', t.inspect]
61
- line + if variables.has_key?(t)
62
- ["#{variables[t].inspect},", comments_to_s(comments)]
63
- else
64
- ["#{to_code(t)},", comments_to_s(comments, 'required')]
65
- end
66
- end
67
- lines.sort! { |a,b| b[3] <=> a[3] }
68
- lines.first[0] = meth_call
69
- lines.last[2].chop!
70
- max = lines.transpose.map { |c| c.map(&:length).max }
71
- lines.map { |line| "%-#{max[0]}s %-#{max[1]}s => %-#{max[2]}s %s" % line }.join("\n")
146
+ def usage(name)
147
+ Prompter.say_log "\n#{Output.new(name, self).render_usage}\n"
72
148
  end
73
149
 
74
- def comments_to_s(*comments)
75
- comments = comments.compact
76
- return '' if comments == []
77
- "# #{comments.join(' ')}"
78
- end
79
150
 
80
- def to_code(name)
81
- keys = name.to_s.split('.').map{|s| s =~ /^[0..9]+$/ ? s.to_i : s.to_sym}
82
- code = keys.shift.to_s
83
- return code if keys.empty?
84
- keys.each{|k| code << "[#{k.inspect}]"}
85
- code
151
+ def find(pattern)
152
+ pattern = /#{pattern}/ unless pattern.is_a?(Regexp)
153
+ methods = templates.keys.select{ |key| key =~ pattern }
154
+ Prompter.say_log methods.to_yaml
86
155
  end
87
156
 
88
157
  end
@@ -8,7 +8,7 @@ module Elastics
8
8
  send :define_template, Template::Search, name, args, source_vars
9
9
  end
10
10
 
11
- # http://www.elasticsearch.org/guide/reference/api/multi-search.html
11
+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html
12
12
  # requests can be an array of arrays: [[:template1, variable_hash1], [template2, variable_hash2]]
13
13
  # or a hash {:template1 => variable_hash1, template2 => variable_hash2}
14
14
  # The variables are an hash of variables that will be used to render the msearch template
@@ -34,24 +34,21 @@ module Elastics
34
34
  end
35
35
  end
36
36
 
37
- # implements search_type=scan (http://www.elasticsearch.org/guide/reference/api/search/search-type.html)
37
+ # implements search_type=scan (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#scan)
38
38
  def scan_search(template, *vars, &block)
39
39
  user_raw_result = vars.any?{|v| v[:raw_result]}
40
- scroll = '5m'
41
- search_vars = Vars.new({:params => { :search_type => 'scan',
42
- :scroll => scroll,
43
- :size => 50 },
44
- :raw_result => true}, *vars)
45
- search_temp = template.is_a?(Elastics::Template) ? template : templates[template]
40
+ scroll = '5m'
41
+ search_vars = Vars.new({:params => { :search_type => 'scan',
42
+ :scroll => scroll,
43
+ :size => 50 },
44
+ :raw_result => true}, *vars)
45
+ search_temp = template.is_a?(Elastics::Template) ? template : templates[template]
46
46
 
47
- scroll_vars = Vars.new({:params => { :scroll => scroll },
48
- :raw_result => true}, variables, *vars)
49
- scroll_temp = Elastics::Template.new( :get,
50
- '/_search/scroll',
51
- nil,
52
- scroll_vars )
53
- search_res = search_temp.render search_vars
54
- scroll_id = search_res['_scroll_id']
47
+ scroll_vars = Vars.new({:params => { :scroll => scroll },
48
+ :raw_result => true}, variables, *vars)
49
+ scroll_temp = Elastics::Template.new( :get, '/_search/scroll', nil, scroll_vars )
50
+ search_res = search_temp.render search_vars
51
+ scroll_id = search_res['_scroll_id']
55
52
  while (result = scroll_temp.render(:data => scroll_id)) do
56
53
  break if result['hits']['hits'].empty?
57
54
  scroll_id = result['_scroll_id']
@@ -60,7 +57,7 @@ module Elastics
60
57
  end
61
58
  end
62
59
 
63
- # implements search_type=count (http://www.elasticsearch.org/guide/reference/api/search/search-type.html)
60
+ # implements search_type=count (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#count)
64
61
  def count_search(template, *vars)
65
62
  template = template.is_a?(Elastics::Template) ? template : templates[template]
66
63
  template.render Vars.new({:params => {:search_type => 'count'}, :raw_result => true}, *vars)