eson-http 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/LICENSE.md +20 -0
  2. data/README.md +111 -0
  3. data/Rakefile +31 -0
  4. data/bin/elshell +20 -0
  5. data/eson-http.gemspec +28 -0
  6. data/lib/eson-http.rb +55 -0
  7. data/lib/eson/http.rb +16 -0
  8. data/lib/eson/http/api.rb +13 -0
  9. data/lib/eson/http/client.rb +24 -0
  10. data/lib/eson/http/cluster/health.rb +15 -0
  11. data/lib/eson/http/cluster/nodes.rb +14 -0
  12. data/lib/eson/http/cluster/shutdown.rb +15 -0
  13. data/lib/eson/http/cluster/state.rb +16 -0
  14. data/lib/eson/http/cluster/stats.rb +18 -0
  15. data/lib/eson/http/core/bulk.rb +33 -0
  16. data/lib/eson/http/core/count.rb +26 -0
  17. data/lib/eson/http/core/delete.rb +14 -0
  18. data/lib/eson/http/core/delete_by_query.rb +22 -0
  19. data/lib/eson/http/core/get.rb +15 -0
  20. data/lib/eson/http/core/index.rb +26 -0
  21. data/lib/eson/http/core/mget.rb +23 -0
  22. data/lib/eson/http/core/more_like_this.rb +14 -0
  23. data/lib/eson/http/core/msearch.rb +47 -0
  24. data/lib/eson/http/core/percolate.rb +16 -0
  25. data/lib/eson/http/core/search.rb +29 -0
  26. data/lib/eson/http/core/simple_search.rb +18 -0
  27. data/lib/eson/http/indices/aliases.rb +14 -0
  28. data/lib/eson/http/indices/analyze.rb +14 -0
  29. data/lib/eson/http/indices/clear_cache.rb +18 -0
  30. data/lib/eson/http/indices/close_index.rb +14 -0
  31. data/lib/eson/http/indices/create_index.rb +14 -0
  32. data/lib/eson/http/indices/delete_index.rb +14 -0
  33. data/lib/eson/http/indices/delete_mapping.rb +17 -0
  34. data/lib/eson/http/indices/delete_template.rb +14 -0
  35. data/lib/eson/http/indices/exists.rb +14 -0
  36. data/lib/eson/http/indices/flush.rb +18 -0
  37. data/lib/eson/http/indices/get_mapping.rb +20 -0
  38. data/lib/eson/http/indices/get_settings.rb +18 -0
  39. data/lib/eson/http/indices/get_template.rb +14 -0
  40. data/lib/eson/http/indices/open_index.rb +14 -0
  41. data/lib/eson/http/indices/optimize.rb +18 -0
  42. data/lib/eson/http/indices/put_mapping.rb +16 -0
  43. data/lib/eson/http/indices/put_template.rb +14 -0
  44. data/lib/eson/http/indices/refresh.rb +18 -0
  45. data/lib/eson/http/indices/segments.rb +18 -0
  46. data/lib/eson/http/indices/snapshot.rb +18 -0
  47. data/lib/eson/http/indices/stats.rb +18 -0
  48. data/lib/eson/http/indices/status.rb +18 -0
  49. data/lib/eson/http/indices/update_settings.rb +18 -0
  50. data/lib/eson/http/request.rb +95 -0
  51. data/lib/eson/modules/response_parser.rb +22 -0
  52. data/lib/eson/modules/status_handler.rb +24 -0
  53. data/log4j.properties +18 -0
  54. data/test/http/client_test.rb +14 -0
  55. data/test/http/cluster_test.rb +58 -0
  56. data/test/http/index_test.rb +180 -0
  57. data/test/http/indices/basics_test.rb +257 -0
  58. data/test/http/query_test.rb +70 -0
  59. data/test/modules/query_plugin_test.rb +40 -0
  60. data/test/seeds/seeds.rb +30 -0
  61. data/test/test_config.rb +33 -0
  62. metadata +202 -0
@@ -0,0 +1,180 @@
1
+ require './test/test_config'
2
+
3
+ context 'HTTP client quick api' do
4
+ helper(:node) { Node::External.instance }
5
+
6
+ helper(:client) do
7
+ Eson::Client.new(:server => "http://#{node.ip}:#{node.port}",
8
+ :protocol => Eson::HTTP,
9
+ :plugins => [Eson::StatusHandler, Eson::ResponseParser],
10
+ :logger => 'test/test.log')
11
+ end
12
+
13
+ context "after put request" do
14
+ setup do
15
+ client.index :doc => {"test" => "bar"},
16
+ :type => "bar",
17
+ :id => 600
18
+ end
19
+
20
+ asserts("is stored in correct index") {topic["_index"]}.equals('default')
21
+
22
+ context "get request" do
23
+ setup do
24
+ client.get :type => "bar",
25
+ :id => 600
26
+ end
27
+
28
+ asserts("id") { topic["_id"] }.equals("600")
29
+ end
30
+
31
+ context "delete" do
32
+ setup do
33
+ client.delete :type => "bar",
34
+ :id => 600
35
+ end
36
+
37
+ asserts("ok") { topic["ok"] }
38
+ end
39
+
40
+ end
41
+
42
+ context "percolate" do
43
+ setup do
44
+ client.create_index :index => 'test'
45
+ client.index :index => '_percolator',
46
+ :type => 'test',
47
+ :id => 'kuku',
48
+ :doc => {
49
+ :query => {
50
+ :match_all => { }
51
+ }
52
+ }
53
+ client.refresh
54
+ end
55
+
56
+ asserts("ok") { topic["ok"] }
57
+ asserts("responds to percolation requests") do
58
+ (
59
+ client.percolate :index => 'test',
60
+ :type => "type1",
61
+ :doc => {
62
+ :test => "foo"
63
+ }
64
+ )["matches"]
65
+ end.equals(["kuku"])
66
+ end
67
+
68
+ context "mget" do
69
+ setup do
70
+ client.create_index :index => 'mget'
71
+ client.index :index => "mget_test",
72
+ :type => "foo",
73
+ :doc => {:foo => :bar},
74
+ :id => 1
75
+ client.index :index => "mget_test",
76
+ :type => "foo",
77
+ :doc => {:foo => :bar},
78
+ :id => 2
79
+ client.refresh
80
+ client.mget(:index => "mget_test", :ids => [1,2])
81
+ end
82
+
83
+ asserts("number of docs") { topic["docs"].length }.equals(2)
84
+ end
85
+
86
+ unless ENV["ES_VERSION"] && ENV["ES_VERSION"] < "0.19.0"
87
+ context "delete_by_query" do
88
+ setup do
89
+ client.index :index => "delete_by_query",
90
+ :type => "foo",
91
+ :doc => {:foo => :bar}
92
+ client.index :index => "delete_by_query",
93
+ :type => "foo",
94
+ :doc => {:foo => :bar}
95
+ client.refresh :index => "delete_by_query"
96
+
97
+ client.delete_by_query :index => "delete_by_query",
98
+ :query => { :match_all => {} }
99
+ end
100
+
101
+ asserts("no doc left") { client.search(:index => "delete_by_query")["hits"]["total"] }.equals(0)
102
+ end
103
+ end
104
+ end
105
+
106
+ context 'HTTP client verbose API' do
107
+ helper(:client) do
108
+ Eson::HTTP::Client.new(:auto_call => false)
109
+ end
110
+
111
+ context "returns request objects" do
112
+ setup do
113
+ client.index
114
+ end
115
+
116
+ asserts_topic("is an Index request").kind_of?(Eson::HTTP::Index)
117
+ end
118
+
119
+ context "#msearch without block" do
120
+ setup do
121
+ msearch = client.msearch
122
+ msearch.search :index => 'test',
123
+ :type => 'kuku',
124
+ :query => {
125
+ :match_all => { }
126
+ }
127
+ msearch.search :index => 'test',
128
+ :query => {
129
+ :match_all => { }
130
+ }
131
+ msearch.search :index => 'test',
132
+ :search_type => 'count',
133
+ :query => {
134
+ :match_all => { }
135
+ }
136
+ msearch
137
+ end
138
+
139
+ asserts(:source).equals(<<-JSON)
140
+ {"indices":["test"],"types":["kuku"]}
141
+ {"query":{"match_all":{}}}
142
+ {"indices":["test"]}
143
+ {"query":{"match_all":{}}}
144
+ {"indices":["test"],"search_type":"count"}
145
+ {"query":{"match_all":{}}}
146
+ JSON
147
+ end
148
+
149
+ context "#msearch with block" do
150
+ setup do
151
+ client.msearch :index => "foo", :type => "bar" do |s|
152
+ s.search :index => 'test',
153
+ :type => 'kuku',
154
+ :query => {
155
+ :match_all => { }
156
+ }
157
+ s.search :index => 'test',
158
+ :query => {
159
+ :match_all => { }
160
+ }
161
+ s.search :index => 'test',
162
+ :search_type => 'count',
163
+ :query => {
164
+ :match_all => { }
165
+ }
166
+ end
167
+ end
168
+
169
+ asserts(:source).equals(<<-JSON)
170
+ {"indices":["test"],"types":["kuku"]}
171
+ {"query":{"match_all":{}}}
172
+ {"indices":["test"]}
173
+ {"query":{"match_all":{}}}
174
+ {"indices":["test"],"search_type":"count"}
175
+ {"query":{"match_all":{}}}
176
+ JSON
177
+
178
+ asserts(:fill).equals(Addressable::URI.parse("foo/bar/_msearch"))
179
+ end
180
+ end
@@ -0,0 +1,257 @@
1
+ require './test/test_config'
2
+
3
+ context 'HTTP client' do
4
+ helper(:node) { Node::External.instance }
5
+
6
+ helper(:client) do
7
+ Eson::Client.new(:server => "http://#{node.ip}:#{node.port}",
8
+ :protocol => Eson::HTTP,
9
+ :plugins => [Eson::StatusHandler, Eson::ResponseParser],
10
+ :logger => 'test/test.log',
11
+ :auth => ['Aladdin', 'open sesame'])
12
+ end
13
+
14
+ context "http auth" do
15
+ asserts {
16
+ request = client.search({}, false)
17
+ request.base_resource.headers['Authorization']
18
+ }.equals('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==')
19
+ end
20
+
21
+ context "missing index" do
22
+ asserts {
23
+ client.refresh
24
+ client.get :index => "missing", :type => "foo", :id => 1
25
+ }.raises(Eson::IndexNotFoundError)
26
+ end
27
+
28
+ context "missing handler" do
29
+ asserts {
30
+ client.get :index => "missing"
31
+ }.raises(Eson::Error)
32
+ end
33
+
34
+ # deactivated, because it is behaving flakey...
35
+ #context "with an existing index" do
36
+ # setup do
37
+ # client.create_index :index => "existing"
38
+ # client.refresh
39
+ # end
40
+ #
41
+ # asserts {
42
+ # client.get :index => "existing", :type => "foo", :id => 81923123
43
+ # }.raises(Eson::NotFoundError)
44
+ #end
45
+
46
+ context "delete index" do
47
+ setup do
48
+ client.delete_index :index => "for_deletion"
49
+ end
50
+
51
+ asserts("ok") { topic["ok"] }
52
+ end
53
+
54
+ context "create index" do
55
+ setup do
56
+ client.create_index :index => "for_creation"
57
+ end
58
+
59
+ asserts("ok") { topic["ok"] }
60
+ end
61
+
62
+ context "create with settings and mappings" do
63
+ setup do
64
+ client.create_index :index => "for_creation_with_settings_and_mappings",
65
+ :settings => {
66
+ :index => {
67
+ :number_of_shards => 1
68
+ }
69
+ },
70
+ :mappings => {
71
+ :type1 => {
72
+ :_source => { :enabled => false },
73
+ :properties => {
74
+ :field1 => { :type => "string", :index => "not_analyzed" }
75
+ }
76
+ }
77
+ }
78
+ end
79
+
80
+ asserts("ok") { topic["ok"] }
81
+ asserts("settings can be retrieved") do
82
+ (
83
+ client.get_settings :index => "for_creation_with_settings_and_mappings"
84
+ )["for_creation_with_settings_and_mappings"]["settings"]["index.number_of_shards"]
85
+ end
86
+ end
87
+
88
+ context "template creation" do
89
+ setup do
90
+ result = client.put_template :name => 'foo',
91
+ :template => 'foo*',
92
+ :settings => {
93
+ :index => {
94
+ :number_of_shards => 1
95
+ }
96
+ },
97
+ :mappings => {
98
+ :type1 => {
99
+ :_source => { :enabled => false },
100
+ :properties => {
101
+ :field1 => { :type => "string", :index => "not_analyzed" }
102
+ }
103
+ }
104
+ }
105
+ sleep 0.5
106
+ result
107
+ end
108
+
109
+ asserts("ok") { topic["ok"] }
110
+
111
+ context "get template" do
112
+ setup do
113
+ client.get_template :name => 'foo'
114
+ end
115
+
116
+ asserts("returns correct template") { topic["foo"] }
117
+ end
118
+
119
+ context "delete template" do
120
+ setup do
121
+ client.delete_template :name => 'foo'
122
+ end
123
+
124
+ asserts("ok") { topic["ok"] }
125
+ end
126
+ end
127
+
128
+ context "aliases" do
129
+ setup do
130
+ client.create_index :index => "abc"
131
+ client.create_index :index => "def"
132
+ client.aliases do |r|
133
+ r.add "abc", "alias"
134
+ r.add "def", "alias"
135
+ end
136
+ end
137
+
138
+ asserts("ok") { topic["ok"] }
139
+ asserts("can be queried") do
140
+ client.status :index => "alias"
141
+ end
142
+ end
143
+
144
+ context "refresh" do
145
+ setup do
146
+ client.refresh :index => "default"
147
+ end
148
+
149
+ asserts("ok") { topic["ok"] }
150
+ end
151
+
152
+ context "snapshot" do
153
+ setup do
154
+ client.snapshot :index => "default"
155
+ end
156
+
157
+ asserts("ok") { topic["ok"] }
158
+ end
159
+
160
+ context "clear_cache" do
161
+ setup do
162
+ client.clear_cache :index => "default",
163
+ :filter => true
164
+ end
165
+
166
+ asserts("ok") { topic["ok"] }
167
+ end
168
+
169
+ context "flush" do
170
+ setup do
171
+ client.flush :index => "default"
172
+ end
173
+
174
+ asserts("ok") { topic["ok"] }
175
+ end
176
+
177
+ context "optimize" do
178
+ setup do
179
+ client.optimize :index => "default"
180
+ end
181
+
182
+ asserts("ok") { topic["ok"] }
183
+ end
184
+
185
+ context "close index" do
186
+ setup do
187
+ client.create_index :index => 'for_closing'
188
+ client.close_index :index => 'for_closing'
189
+ client.refresh
190
+ end
191
+
192
+ asserts("ok") { topic["ok"] }
193
+ asserts("cannot be requested") { (client.status :index => 'for_closing')["indices"] }.equals({})
194
+ end
195
+
196
+ context "open index" do
197
+ setup do
198
+ client.create_index :index => 'for_reopening'
199
+ client.close_index :index => 'for_reopening'
200
+ client.open_index :index => 'for_reopening'
201
+ end
202
+
203
+ asserts("ok") { topic["ok"] }
204
+ asserts("can be requested") { (client.status :index => 'for_reopening')["indices"]['for_reopening'] }
205
+ end
206
+
207
+ context "put mapping" do
208
+ setup do
209
+ client.create_index :index => 'mappings'
210
+ result = client.put_mapping :index => 'mappings',
211
+ :type => 'type1',
212
+ :mapping => {
213
+ :type1 => {
214
+ :_source => { :enabled => false },
215
+ :properties => {
216
+ :field1 => { :type => "string", :index => "not_analyzed" }
217
+ }
218
+ }
219
+ }
220
+ sleep 0.5
221
+ result
222
+ end
223
+
224
+ asserts("ok") { topic["ok"] }
225
+ asserts("mapping can be retrieved") do
226
+ (
227
+ client.get_mapping :index => "mappings",
228
+ :type => 'type1'
229
+ )['type1']['_source']['enabled']
230
+ end.equals(false)
231
+ end
232
+
233
+ context "exists?" do
234
+ setup do
235
+ client.exists? :index => 'does_not_exist_at_all'
236
+ end
237
+
238
+ asserts { topic }.equals(false)
239
+ end
240
+
241
+ context "index stats" do
242
+ setup do
243
+ client.index_stats :index => 'default'
244
+ end
245
+
246
+ asserts { topic["ok"] }.equals(true)
247
+ end
248
+
249
+ context "index segments" do
250
+ setup do
251
+ client.segments :index => 'default'
252
+ end
253
+
254
+ asserts { topic["ok"] }.equals(true)
255
+ end
256
+
257
+ end
@@ -0,0 +1,70 @@
1
+ require './test/test_config'
2
+
3
+ context 'HTTP client quickapi' do
4
+ helper(:node) { Node::External.instance }
5
+
6
+ helper(:client) do
7
+ Eson::Client.new(:server => "http://#{node.ip}:#{node.port}",
8
+ :protocol => Eson::HTTP,
9
+ :plugins => [Eson::StatusHandler, Eson::ResponseParser],
10
+ :logger => 'test/test.log')
11
+ end
12
+
13
+ setup do
14
+ #client.create_index :index => "default"
15
+ client.refresh
16
+ end
17
+
18
+ context "query" do
19
+ setup do
20
+ client.query :query => {
21
+ "term" => { "user" => "kimchy" }
22
+ }
23
+ end
24
+
25
+ asserts("has hits") { topic['hits']['total'] }
26
+ asserts("has no failures") { topic["_shards"]["failures"]}.equals(nil)
27
+ end
28
+
29
+ context "count query" do
30
+ setup do
31
+ client.count :query => {
32
+ :match_all => { }
33
+ }
34
+ end
35
+
36
+ asserts("has hits") { topic["count"] }
37
+ asserts("has no failures") { topic["_shards"]["failures"]}.equals(nil)
38
+ end
39
+
40
+ context "mlt query" do
41
+ setup do
42
+ client.index :doc => {"test" => "bar"},
43
+ :type => "bar",
44
+ :id => 200
45
+
46
+ client.more_like_this :index => 'default', :type => "bar", :id => 200
47
+ end
48
+
49
+ asserts("has hits") { topic["timed_out"] }.equals(false)
50
+ asserts("has no failures") { topic["_shards"]["failures"]}.equals(nil)
51
+ end
52
+
53
+ context "filtered queries" do
54
+ setup do
55
+ client.query :query => {
56
+ "term" => { "name.first" => "shay" }
57
+ },
58
+ :filter => {
59
+ "terms" => {
60
+ "name.last" => ["banon", "kimchy"],
61
+ "_name" => "test"
62
+ }
63
+ }
64
+ end
65
+
66
+ asserts("ok") { topic["hits"]["total"] }.equals(0)
67
+ asserts("has no failures") { topic["_shards"]["failures"]}.equals(nil)
68
+ end
69
+
70
+ end