eson-core 0.7.0 → 0.8.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.
Files changed (54) hide show
  1. data/Rakefile +2 -2
  2. data/eson-core.gemspec +2 -1
  3. data/lib/eson-core.rb +5 -0
  4. data/lib/eson/api.rb +61 -12
  5. data/lib/eson/chainable.rb +3 -3
  6. data/lib/eson/client.rb +699 -180
  7. data/lib/eson/error.rb +10 -2
  8. data/lib/eson/request.rb +57 -16
  9. data/lib/eson/shared/cluster/health.rb +9 -2
  10. data/lib/eson/shared/cluster/nodes.rb +9 -1
  11. data/lib/eson/shared/cluster/shutdown.rb +9 -1
  12. data/lib/eson/shared/cluster/state.rb +9 -1
  13. data/lib/eson/shared/cluster/stats.rb +9 -1
  14. data/lib/eson/shared/core/bulk.rb +9 -1
  15. data/lib/eson/shared/core/count.rb +10 -1
  16. data/lib/eson/shared/core/delete.rb +10 -1
  17. data/lib/eson/shared/core/delete_by_query.rb +10 -1
  18. data/lib/eson/shared/core/explain.rb +44 -0
  19. data/lib/eson/shared/core/get.rb +9 -1
  20. data/lib/eson/shared/core/index.rb +13 -2
  21. data/lib/eson/shared/core/mget.rb +12 -3
  22. data/lib/eson/shared/core/more_like_this.rb +10 -1
  23. data/lib/eson/shared/core/msearch.rb +10 -1
  24. data/lib/eson/shared/core/percolate.rb +10 -1
  25. data/lib/eson/shared/core/scroll.rb +17 -0
  26. data/lib/eson/shared/core/search.rb +15 -3
  27. data/lib/eson/shared/core/simple_search.rb +9 -1
  28. data/lib/eson/shared/core/update.rb +33 -0
  29. data/lib/eson/shared/core/validate.rb +50 -0
  30. data/lib/eson/shared/indices/aliases.rb +37 -9
  31. data/lib/eson/shared/indices/analyze.rb +9 -2
  32. data/lib/eson/shared/indices/clear_cache.rb +9 -1
  33. data/lib/eson/shared/indices/close_index.rb +8 -1
  34. data/lib/eson/shared/indices/create_index.rb +10 -1
  35. data/lib/eson/shared/indices/delete_index.rb +8 -1
  36. data/lib/eson/shared/indices/delete_mapping.rb +9 -1
  37. data/lib/eson/shared/indices/delete_template.rb +8 -1
  38. data/lib/eson/shared/indices/exists.rb +8 -1
  39. data/lib/eson/shared/indices/flush.rb +9 -1
  40. data/lib/eson/shared/indices/get_aliases.rb +16 -0
  41. data/lib/eson/shared/indices/get_mapping.rb +9 -1
  42. data/lib/eson/shared/indices/get_settings.rb +8 -1
  43. data/lib/eson/shared/indices/get_template.rb +8 -1
  44. data/lib/eson/shared/indices/open_index.rb +8 -1
  45. data/lib/eson/shared/indices/optimize.rb +9 -1
  46. data/lib/eson/shared/indices/put_mapping.rb +10 -1
  47. data/lib/eson/shared/indices/put_template.rb +9 -1
  48. data/lib/eson/shared/indices/refresh.rb +9 -1
  49. data/lib/eson/shared/indices/segments.rb +8 -1
  50. data/lib/eson/shared/indices/snapshot.rb +8 -1
  51. data/lib/eson/shared/indices/stats.rb +9 -1
  52. data/lib/eson/shared/indices/status.rb +8 -1
  53. data/lib/eson/shared/indices/update_settings.rb +10 -1
  54. metadata +7 -8
@@ -1,14 +1,22 @@
1
1
  module Eson
2
+ # Special error class for errors happening in the Eson context. In contrast
3
+ # to StandardErrors, this error also holds the response associated with this
4
+ # Error.
2
5
  class Error < StandardError
3
6
  attr_accessor :response
4
7
 
8
+ #
9
+ # @param [String] msg The error message
10
+ # @param [Object] response The raw response object.
5
11
  def initialize(msg, response)
6
- super(msg)
12
+ super(response.body)
7
13
 
8
14
  self.response = response
9
15
  end
10
16
  end
11
-
17
+
18
+ # Error class that indicates a missing document.
12
19
  NotFoundError = Class.new(Error)
20
+ # Error class that indicates a missing index.
13
21
  IndexNotFoundError = Class.new(NotFoundError)
14
22
  end
@@ -1,9 +1,18 @@
1
1
  module Eson
2
+ # A request is a generic object that implements parameter manipulation
3
+ # methods and Esons plugin API. It is always bound to a client.
4
+ #
5
+ # This class cannot be used directly but must be subclassed by protocol
6
+ # implementations. Proper implementations must respond to `#call`.
7
+ #
8
+ # @example constructing a request by hand
9
+ # r = protocol::Request.new(protocol::IndexExists, [ResponseParser], client)
2
10
  class Request
3
11
  attr_accessor :api, :client
4
12
 
5
13
  attr_accessor :pretty, :source, :index, :indices, :case
6
14
 
15
+
7
16
  def initialize(api, plugins, client)
8
17
  self.api = api
9
18
  self.client = client
@@ -16,11 +25,35 @@ module Eson
16
25
  end
17
26
  end
18
27
 
28
+ # This is a default implementation of `handle_block` that can be overriden
29
+ # by apis or plugins.
30
+ #
31
+ # @yield self
19
32
  def handle_block
20
33
  yield self
21
34
  end
22
35
 
23
- def params=(params)
36
+ # This is an internal method that allows the client to set default
37
+ # parameters to requests without having to check for the presence of the
38
+ # method beforehand.
39
+ #
40
+ # @api internal
41
+ def set_parameters_without_exceptions(params)
42
+ params.each do |k,v|
43
+ begin
44
+ self.send("#{k}=", v)
45
+ rescue NoMethodError => e
46
+ #drop
47
+ end
48
+ end
49
+ end
50
+
51
+ # Mass assignment of parameters to a request. Invalid parameters will
52
+ # raise an exception.
53
+ #
54
+ # @param [Hash] params The parameters, keyed by name
55
+ # @raise [NoMethodError]
56
+ def parameters=(params)
24
57
  params.each do |k,v|
25
58
  begin
26
59
  self.send("#{k}=", v)
@@ -30,6 +63,17 @@ module Eson
30
63
  end
31
64
  end
32
65
 
66
+ # Checks whether a Plugin works with a certain type of request. For example,
67
+ # Search plugins usually only work with Search requests. The decision is up
68
+ # to the plugin. If the plugin does not list a set of APIs that it works
69
+ # with, it will be considered compatible to all.
70
+ #
71
+ # @param [Eson::Api] api the api in question
72
+ # @param [Module] plugin the plugin module
73
+ # @param [Eson::Client] client the client to which the plugin should be added
74
+ #
75
+ # @return [true,false] Whether the plugin is compatible or not.
76
+ # @api internal
33
77
  def pluggable?(api, plugin, client)
34
78
  if plugin.respond_to? :plugin_for
35
79
  plugin.plugin_for(client.protocol).include?(api)
@@ -64,8 +108,9 @@ module Eson
64
108
  @source || source_from_params
65
109
  end
66
110
 
67
- # TODO: woah, this needs refactoring
68
-
111
+ # Extracts the source parameters from the parameter set.
112
+ #
113
+ # @return [String] the request source, as String
69
114
  def source_from_params
70
115
  return nil unless self.respond_to? :source_param
71
116
 
@@ -77,27 +122,23 @@ module Eson
77
122
  return encode(obj)
78
123
  end
79
124
  else
80
- pairs = source_param.map do |p|
125
+ obj = {}
126
+ source_param.each_with_object(obj) do |p, o|
81
127
  if v = self.send(p)
82
- [p, v]
83
- else
84
- nil
128
+ o[p] = v
85
129
  end
86
130
  end
87
- pairs.compact!
88
-
89
- return nil if pairs.empty?
90
-
91
- obj = {}
92
-
93
- pairs.each do |p, v|
94
- obj[p] = v
95
- end
96
131
 
132
+ return nil if obj.empty?
97
133
  return encode(obj)
98
134
  end
99
135
  end
100
136
 
137
+ # Encode any object. If it is an {Eson}-Object and responds to
138
+ # `to_query_hash`, this will be preferred. Otherwise, MultiJson is used.
139
+ #
140
+ # @param [Object] obj the object to encode
141
+ # @return [String] the object, encoded as JSON
101
142
  def encode(obj)
102
143
  if obj.respond_to? :to_query_hash
103
144
  obj = obj.to_query_hash
@@ -1,16 +1,23 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Health#parameters}
6
+ # {include:Health#source_param}
7
+ # {include:Health#multi_index}
8
+ # {include:Health#multi_types}
3
9
  module Health
4
10
  extend API
5
11
 
12
+ # @!macro multi_index
6
13
  multi_index true
7
-
14
+
15
+ # @!macro parameters
8
16
  parameters :wait_for_status,
9
17
  :level,
10
18
  :wait_for_relocating_shards,
11
19
  :wait_for_nodes,
12
20
  :timeout
13
-
14
21
  end
15
22
  end
16
23
  end
@@ -1,10 +1,18 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Nodes#parameters}
6
+ # {include:Nodes#source_param}
7
+ # {include:Nodes#multi_index}
8
+ # {include:Nodes#multi_types}
3
9
  module Nodes
4
10
  extend API
5
11
 
12
+ # @!macro no_multi_index
6
13
  multi_index false
7
14
 
15
+ # @!macro parameters
8
16
  parameters :nodes
9
17
 
10
18
  def nodes
@@ -12,4 +20,4 @@ module Eson
12
20
  end
13
21
  end
14
22
  end
15
- end
23
+ end
@@ -1,11 +1,19 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Shutdown#parameters}
6
+ # {include:Shutdown#source_param}
7
+ # {include:Shutdown#multi_index}
8
+ # {include:Shutdown#multi_types}
3
9
  module Shutdown
4
10
  #TODO fully implement and test this
5
11
  extend API
6
12
 
13
+ # @!macro no_multi_index
7
14
  multi_index false
8
15
 
16
+ # @!macro parameters
9
17
  parameters :nodes,
10
18
  :delay,
11
19
  :master,
@@ -17,4 +25,4 @@ module Eson
17
25
  end
18
26
  end
19
27
  end
20
- end
28
+ end
@@ -1,11 +1,19 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:State#parameters}
6
+ # {include:State#source_param}
7
+ # {include:State#multi_index}
8
+ # {include:State#multi_types}
3
9
  module State
4
10
  extend API
5
11
  #TODO implement filter_indices correctly
6
12
 
13
+ # @!macro multi_index
7
14
  multi_index true
8
15
 
16
+ # @!macro parameters
9
17
  parameters :filter_nodes,
10
18
  :filter_routing_table,
11
19
  :filter_metadata,
@@ -14,4 +22,4 @@ module Eson
14
22
 
15
23
  end
16
24
  end
17
- end
25
+ end
@@ -1,10 +1,18 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Stats#parameters}
6
+ # {include:Stats#source_param}
7
+ # {include:Stats#multi_index}
8
+ # {include:Stats#multi_types}
3
9
  module Stats
4
10
  extend API
5
11
 
12
+ # @!macro no_multi_index
6
13
  multi_index false
7
14
 
15
+ # @!macro parameters
8
16
  parameters :nodes
9
17
 
10
18
  def nodes
@@ -12,4 +20,4 @@ module Eson
12
20
  end
13
21
  end
14
22
  end
15
- end
23
+ end
@@ -1,9 +1,17 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Bulk#parameters}
6
+ # {include:Bulk#source_param}
7
+ # {include:Bulk#multi_index}
8
+ # {include:Bulk#multi_types}
3
9
  module Bulk
4
10
  extend API
5
11
 
12
+ # @!macro source_param
6
13
  source_param :bulk
14
+ # @!macro parameters
7
15
  parameters :bulk
8
16
 
9
17
  def bulk
@@ -23,4 +31,4 @@ module Eson
23
31
  end
24
32
  end
25
33
  end
26
- end
34
+ end
@@ -1,12 +1,20 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # @note {include:Count#parameters}
6
+ # @note {include:Count#source_param}
7
+ # @note {include:Count#multi_index}
8
+ # @note {include:Count#multi_types}
3
9
  module Count
4
10
  extend API
5
11
 
6
12
  attr_accessor :type
7
13
 
14
+ # @!macro multi_index
8
15
  multi_index true
9
16
 
17
+ # @!macro source_param
10
18
  source_param :query,
11
19
  :timeout,
12
20
  :from,
@@ -23,6 +31,7 @@ module Eson
23
31
  :version,
24
32
  :min_score
25
33
 
34
+ # @!macro parameters
26
35
  parameters :timeout,
27
36
  :types,
28
37
  :routing,
@@ -60,4 +69,4 @@ module Eson
60
69
 
61
70
  end
62
71
  end
63
- end
72
+ end
@@ -1,12 +1,21 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Delete#parameters}
6
+ # {include:Delete#source_param}
7
+ # {include:Delete#multi_index}
8
+ # {include:Delete#multi_types}
3
9
  module Delete
4
10
  extend API
5
11
 
12
+ # @!macro no_multi_index
6
13
  multi_index false
7
14
 
15
+ # @!macro source_param
8
16
  source_param :item
9
17
 
18
+ # @!macro parameters
10
19
  parameters(
11
20
  :type,
12
21
  :id,
@@ -23,4 +32,4 @@ module Eson
23
32
  )
24
33
  end
25
34
  end
26
- end
35
+ end
@@ -1,15 +1,24 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # @note {include:DeleteByQuery#parameters}
6
+ # @note {include:DeleteByQuery#source_param}
7
+ # @note {include:DeleteByQuery#multi_index}
8
+ # @note {include:DeleteByQuery#multi_types}
3
9
  module DeleteByQuery
4
10
  extend API
5
11
 
6
12
  attr_accessor :type
7
13
 
14
+ # @!macro multi_index
8
15
  multi_index true
9
16
 
17
+ # @!macro source_param
10
18
  source_param :query
11
19
 
12
20
  # TODO: check the parameter list
21
+ # @!macro parameters
13
22
  parameters :timeout,
14
23
  :types,
15
24
  :routing,
@@ -47,4 +56,4 @@ module Eson
47
56
 
48
57
  end
49
58
  end
50
- end
59
+ end
@@ -0,0 +1,44 @@
1
+ module Eson
2
+ module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Explain#parameters}
6
+ # {include:Explain#source_param}
7
+ # {include:Explain#multi_index}
8
+ # {include:Explain#multi_types}
9
+ module Explain
10
+ extend API
11
+
12
+ # @!macro multi_index
13
+ multi_index false
14
+
15
+ # @!macro multi_types
16
+ multi_types false
17
+
18
+
19
+ # @!macro source_param
20
+ source_param :query,
21
+ :fields,
22
+ :facets,
23
+ :filter
24
+
25
+ # @!macro parameters
26
+ parameters :id,
27
+ :type,
28
+ :query,
29
+ :routing,
30
+ :parent,
31
+ :preference,
32
+ :q,
33
+ :df,
34
+ :analyzer,
35
+ :analyze_wildcard,
36
+ :lowercase_expanded_terms,
37
+ :lenient,
38
+ :default_operator,
39
+ :fields,
40
+ :facets,
41
+ :filter
42
+ end
43
+ end
44
+ end
@@ -1,10 +1,18 @@
1
1
  module Eson
2
2
  module Shared
3
+ # Requests using this API have the following properties:
4
+ #
5
+ # {include:Get#parameters}
6
+ # {include:Get#source_param}
7
+ # {include:Get#multi_index}
8
+ # {include:Get#multi_types}
3
9
  module Get
4
10
  extend API
5
11
 
12
+ # @!macro no_multi_index
6
13
  multi_index false
7
14
 
15
+ # @!macro parameters
8
16
  parameters(
9
17
  :id,
10
18
  :type,
@@ -16,4 +24,4 @@ module Eson
16
24
 
17
25
  end
18
26
  end
19
- end
27
+ end