elastomer-client 0.7.0 → 0.8.1
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.
- checksums.yaml +4 -4
- data/.overcommit.yml +5 -0
- data/.rubocop.yml +83 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -2
- data/README.md +1 -1
- data/Rakefile +2 -2
- data/elastomer-client.gemspec +4 -2
- data/lib/elastomer/client.rb +42 -37
- data/lib/elastomer/client/bulk.rb +2 -2
- data/lib/elastomer/client/cluster.rb +19 -19
- data/lib/elastomer/client/delete_by_query.rb +7 -7
- data/lib/elastomer/client/docs.rb +81 -24
- data/lib/elastomer/client/errors.rb +2 -2
- data/lib/elastomer/client/index.rb +65 -29
- data/lib/elastomer/client/multi_percolate.rb +127 -0
- data/lib/elastomer/client/multi_search.rb +2 -2
- data/lib/elastomer/client/nodes.rb +4 -4
- data/lib/elastomer/client/percolator.rb +77 -0
- data/lib/elastomer/client/repository.rb +7 -7
- data/lib/elastomer/client/scroller.rb +14 -14
- data/lib/elastomer/client/snapshot.rb +9 -9
- data/lib/elastomer/client/template.rb +3 -3
- data/lib/elastomer/client/warmer.rb +5 -16
- data/lib/elastomer/core_ext/time.rb +1 -1
- data/lib/elastomer/middleware/encode_json.rb +5 -5
- data/lib/elastomer/middleware/opaque_id.rb +3 -3
- data/lib/elastomer/middleware/parse_json.rb +5 -5
- data/lib/elastomer/notifications.rb +4 -4
- data/lib/elastomer/version.rb +1 -1
- data/script/bootstrap +2 -0
- data/script/console +5 -5
- data/test/assertions.rb +26 -24
- data/test/client/bulk_test.rb +111 -111
- data/test/client/cluster_test.rb +58 -58
- data/test/client/delete_by_query_test.rb +53 -53
- data/test/client/docs_test.rb +279 -203
- data/test/client/errors_test.rb +1 -1
- data/test/client/index_test.rb +143 -109
- data/test/client/multi_percolate_test.rb +130 -0
- data/test/client/multi_search_test.rb +30 -28
- data/test/client/nodes_test.rb +30 -29
- data/test/client/percolator_test.rb +52 -0
- data/test/client/repository_test.rb +23 -23
- data/test/client/scroller_test.rb +40 -40
- data/test/client/snapshot_test.rb +15 -15
- data/test/client/stubbed_client_test.rb +15 -15
- data/test/client/template_test.rb +10 -10
- data/test/client/warmer_test.rb +18 -18
- data/test/client_test.rb +53 -53
- data/test/core_ext/time_test.rb +12 -12
- data/test/middleware/encode_json_test.rb +20 -20
- data/test/middleware/opaque_id_test.rb +10 -10
- data/test/middleware/parse_json_test.rb +14 -14
- data/test/notifications_test.rb +32 -32
- data/test/test_helper.rb +24 -24
- metadata +38 -2
@@ -36,7 +36,7 @@ module Elastomer
|
|
36
36
|
#
|
37
37
|
# Returns the response body as a Hash
|
38
38
|
def get( params = {} )
|
39
|
-
response = client.get
|
39
|
+
response = client.get "/_template/{template}", update_params(params, :action => "template.get")
|
40
40
|
response.body
|
41
41
|
end
|
42
42
|
|
@@ -48,7 +48,7 @@ module Elastomer
|
|
48
48
|
#
|
49
49
|
# Returns the response body as a Hash
|
50
50
|
def create( template, params = {} )
|
51
|
-
response = client.put
|
51
|
+
response = client.put "/_template/{template}", update_params(params, :body => template, :action => "template.create")
|
52
52
|
response.body
|
53
53
|
end
|
54
54
|
|
@@ -59,7 +59,7 @@ module Elastomer
|
|
59
59
|
#
|
60
60
|
# Returns the response body as a Hash
|
61
61
|
def delete( params = {} )
|
62
|
-
response = client.delete
|
62
|
+
response = client.delete "/_template/{template}", update_params(params, :action => "template.delete")
|
63
63
|
response.body
|
64
64
|
end
|
65
65
|
|
@@ -1,17 +1,6 @@
|
|
1
1
|
module Elastomer
|
2
2
|
class Client
|
3
3
|
|
4
|
-
# Provides access to warmer API commands.
|
5
|
-
# See http://www.elasticsearch.org/guide/reference/api/admin-indices-warmers/
|
6
|
-
#
|
7
|
-
# index_name - The name of the index as a String
|
8
|
-
# warmer_name - The name of the warmer as a String
|
9
|
-
#
|
10
|
-
# Returns a Warmer instance.
|
11
|
-
def warmer(index_name, warmer_name)
|
12
|
-
Warmer.new(self, index_name, warmer_name)
|
13
|
-
end
|
14
|
-
|
15
4
|
class Warmer
|
16
5
|
|
17
6
|
# Create a new Warmer helper for making warmer API requests.
|
@@ -21,8 +10,8 @@ module Elastomer
|
|
21
10
|
# name - The name of the warmer as a String
|
22
11
|
def initialize(client, index_name, name)
|
23
12
|
@client = client
|
24
|
-
@index_name = @client.assert_param_presence(index_name,
|
25
|
-
@name = @client.assert_param_presence(name,
|
13
|
+
@index_name = @client.assert_param_presence(index_name, "index name")
|
14
|
+
@name = @client.assert_param_presence(name, "warmer name")
|
26
15
|
end
|
27
16
|
|
28
17
|
attr_reader :client, :index_name, :name
|
@@ -39,7 +28,7 @@ module Elastomer
|
|
39
28
|
#
|
40
29
|
# Returns the response body as a Hash
|
41
30
|
def create(query, params = {})
|
42
|
-
response = client.put
|
31
|
+
response = client.put "/{index}{/type}/_warmer/{warmer}", defaults.update(params.update(:body => query))
|
43
32
|
response.body
|
44
33
|
end
|
45
34
|
|
@@ -50,7 +39,7 @@ module Elastomer
|
|
50
39
|
#
|
51
40
|
# Returns the response body as a Hash
|
52
41
|
def delete(params = {})
|
53
|
-
response = client.delete
|
42
|
+
response = client.delete "/{index}{/type}/_warmer/{warmer}", defaults.update(params)
|
54
43
|
response.body
|
55
44
|
end
|
56
45
|
|
@@ -61,7 +50,7 @@ module Elastomer
|
|
61
50
|
#
|
62
51
|
# Returns the response body as a Hash
|
63
52
|
def get(params = {})
|
64
|
-
response = client.get
|
53
|
+
response = client.get "/{index}{/type}/_warmer/{warmer}", defaults.update(params)
|
65
54
|
response.body
|
66
55
|
end
|
67
56
|
|
@@ -8,8 +8,8 @@ module Elastomer
|
|
8
8
|
#
|
9
9
|
# Doesn't try to encode bodies that already are in string form.
|
10
10
|
class EncodeJson < Faraday::Middleware
|
11
|
-
CONTENT_TYPE =
|
12
|
-
MIME_TYPE =
|
11
|
+
CONTENT_TYPE = "Content-Type".freeze
|
12
|
+
MIME_TYPE = "application/json".freeze
|
13
13
|
|
14
14
|
def call(env)
|
15
15
|
match_content_type(env) do |data|
|
@@ -31,16 +31,16 @@ module Elastomer
|
|
31
31
|
|
32
32
|
def process_request?(env)
|
33
33
|
type = request_type(env)
|
34
|
-
has_body?(env)
|
34
|
+
has_body?(env) && (type.empty? || type == MIME_TYPE)
|
35
35
|
end
|
36
36
|
|
37
37
|
def has_body?(env)
|
38
|
-
body = env[:body]
|
38
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
39
39
|
end
|
40
40
|
|
41
41
|
def request_type(env)
|
42
42
|
type = env[:request_headers][CONTENT_TYPE].to_s
|
43
|
-
type = type.split(
|
43
|
+
type = type.split(";", 2).first if type.index(";")
|
44
44
|
type
|
45
45
|
end
|
46
46
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "securerandom"
|
2
2
|
|
3
3
|
module Elastomer
|
4
4
|
module Middleware
|
@@ -19,7 +19,7 @@ module Elastomer
|
|
19
19
|
# header](https://github.com/elasticsearch/elasticsearch/issues/1202)
|
20
20
|
# for more details.
|
21
21
|
class OpaqueId < ::Faraday::Middleware
|
22
|
-
X_OPAQUE_ID =
|
22
|
+
X_OPAQUE_ID = "X-Opaque-Id".freeze
|
23
23
|
COUNTER_MAX = 2**32 - 1
|
24
24
|
|
25
25
|
# Faraday middleware implementation.
|
@@ -47,7 +47,7 @@ module Elastomer
|
|
47
47
|
t = Thread.current
|
48
48
|
|
49
49
|
unless t.key? :opaque_id_base
|
50
|
-
t[:opaque_id_base] = (SecureRandom.urlsafe_base64(12) +
|
50
|
+
t[:opaque_id_base] = (SecureRandom.urlsafe_base64(12) + "%08x").freeze
|
51
51
|
t[:opaque_id_counter] = -1
|
52
52
|
end
|
53
53
|
|
@@ -3,8 +3,8 @@ module Elastomer
|
|
3
3
|
|
4
4
|
# Parse response bodies as JSON.
|
5
5
|
class ParseJson < Faraday::Middleware
|
6
|
-
CONTENT_TYPE =
|
7
|
-
MIME_TYPE =
|
6
|
+
CONTENT_TYPE = "Content-Type".freeze
|
7
|
+
MIME_TYPE = "application/json".freeze
|
8
8
|
|
9
9
|
def call(environment)
|
10
10
|
@app.call(environment).on_complete do |env|
|
@@ -16,19 +16,19 @@ module Elastomer
|
|
16
16
|
|
17
17
|
# Parse the response body.
|
18
18
|
def parse(body)
|
19
|
-
MultiJson.load(body) if body.respond_to?(:to_str)
|
19
|
+
MultiJson.load(body) if body.respond_to?(:to_str) && !body.strip.empty?
|
20
20
|
rescue StandardError, SyntaxError => e
|
21
21
|
raise Faraday::Error::ParsingError, e
|
22
22
|
end
|
23
23
|
|
24
24
|
def process_response?(env)
|
25
25
|
type = response_type(env)
|
26
|
-
type.empty?
|
26
|
+
type.empty? || type == MIME_TYPE
|
27
27
|
end
|
28
28
|
|
29
29
|
def response_type(env)
|
30
30
|
type = env[:response_headers][CONTENT_TYPE].to_s
|
31
|
-
type = type.split(
|
31
|
+
type = type.split(";", 2).first if type.index(";")
|
32
32
|
type
|
33
33
|
end
|
34
34
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "active_support/notifications"
|
2
|
+
require "securerandom"
|
3
|
+
require "elastomer/client"
|
4
4
|
|
5
5
|
module Elastomer
|
6
6
|
|
@@ -40,7 +40,7 @@ module Elastomer
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# The name to subscribe to for notifications
|
43
|
-
NAME =
|
43
|
+
NAME = "request.client.elastomer".freeze
|
44
44
|
|
45
45
|
# Internal: Execute the given block and provide instrumentation info to
|
46
46
|
# subscribers. The name we use for subscriptions is
|
data/lib/elastomer/version.rb
CHANGED
data/script/bootstrap
CHANGED
data/script/console
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
2
|
+
require "irb"
|
3
|
+
require "rubygems"
|
4
|
+
require "bundler/setup"
|
5
5
|
|
6
|
-
$LOAD_PATH.unshift
|
7
|
-
require
|
6
|
+
$LOAD_PATH.unshift "lib"
|
7
|
+
require "elastomer/client"
|
8
8
|
|
9
9
|
IRB.start
|
data/test/assertions.rb
CHANGED
@@ -4,7 +4,7 @@ module Minitest::Assertions
|
|
4
4
|
# in index responses. Check for either one so we are compatible
|
5
5
|
# with 0.90 and 1.0.
|
6
6
|
def assert_created(response)
|
7
|
-
assert response[
|
7
|
+
assert response["created"] || response["ok"], "document was not created"
|
8
8
|
end
|
9
9
|
|
10
10
|
#COMPATIBILITY
|
@@ -12,7 +12,7 @@ module Minitest::Assertions
|
|
12
12
|
# in many responses. Check for either one so we are compatible
|
13
13
|
# with 0.90 and 1.0.
|
14
14
|
def assert_acknowledged(response)
|
15
|
-
assert response[
|
15
|
+
assert response["acknowledged"] || response["ok"], "document was not acknowledged"
|
16
16
|
end
|
17
17
|
|
18
18
|
#COMPATIBILITY
|
@@ -20,32 +20,32 @@ module Minitest::Assertions
|
|
20
20
|
# get document response. Check for either one so we are compatible
|
21
21
|
# with 0.90 and 1.0.
|
22
22
|
def assert_found(response)
|
23
|
-
assert response[
|
23
|
+
assert response["found"] || response["exists"], "document was not found"
|
24
24
|
end
|
25
25
|
|
26
26
|
def refute_found(response)
|
27
|
-
refute response[
|
27
|
+
refute response["found"] || response["exists"], "document was unexpectedly found"
|
28
28
|
end
|
29
29
|
|
30
30
|
#COMPATIBILITY
|
31
31
|
# ES 1.0 replaced the 'ok' attribute in the bulk response item with a
|
32
32
|
# 'status' attribute. Here we check for either one for compatibility
|
33
33
|
# with 0.90 and 1.0.
|
34
|
-
def assert_bulk_index(item, message=
|
35
|
-
ok = item[
|
36
|
-
status = item[
|
34
|
+
def assert_bulk_index(item, message="bulk index did not succeed")
|
35
|
+
ok = item["index"]["ok"]
|
36
|
+
status = item["index"]["status"]
|
37
37
|
assert ok == true || status == 201, message
|
38
38
|
end
|
39
39
|
|
40
|
-
def assert_bulk_create(item, message=
|
41
|
-
ok = item[
|
42
|
-
status = item[
|
40
|
+
def assert_bulk_create(item, message="bulk create did not succeed")
|
41
|
+
ok = item["create"]["ok"]
|
42
|
+
status = item["create"]["status"]
|
43
43
|
assert ok == true || status == 201, message
|
44
44
|
end
|
45
45
|
|
46
|
-
def assert_bulk_delete(item, message=
|
47
|
-
ok = item[
|
48
|
-
status = item[
|
46
|
+
def assert_bulk_delete(item, message="bulk delete did not succeed")
|
47
|
+
ok = item["delete"]["ok"]
|
48
|
+
status = item["delete"]["status"]
|
49
49
|
assert ok == true || status == 200, message
|
50
50
|
end
|
51
51
|
|
@@ -55,20 +55,22 @@ module Minitest::Assertions
|
|
55
55
|
# ES 0.90 doesn't have the "mappings" element:
|
56
56
|
# mapping["test-index"]["doco"]
|
57
57
|
def assert_mapping_exists(response, type, message="mapping expected to exist, but doesn't")
|
58
|
-
mapping =
|
59
|
-
response
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
mapping =
|
59
|
+
if response.has_key?("mappings")
|
60
|
+
response["mappings"][type]
|
61
|
+
else
|
62
|
+
response[type]
|
63
|
+
end
|
63
64
|
refute_nil mapping, message
|
64
65
|
end
|
65
66
|
|
66
67
|
def assert_property_exists(response, type, property, message="property expected to exist, but doesn't")
|
67
|
-
mapping =
|
68
|
-
response
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
mapping =
|
69
|
+
if response.has_key?("mappings")
|
70
|
+
response["mappings"][type]
|
71
|
+
else
|
72
|
+
response[type]
|
73
|
+
end
|
74
|
+
assert mapping["properties"].has_key?(property), message
|
73
75
|
end
|
74
76
|
end
|
data/test/client/bulk_test.rb
CHANGED
@@ -1,27 +1,27 @@
|
|
1
|
-
require File.expand_path(
|
1
|
+
require File.expand_path("../../test_helper", __FILE__)
|
2
2
|
|
3
3
|
describe Elastomer::Client::Bulk do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@name =
|
6
|
+
@name = "elastomer-bulk-test"
|
7
7
|
@index = $client.index(@name)
|
8
8
|
|
9
9
|
unless @index.exists?
|
10
10
|
@index.create \
|
11
|
-
:settings => {
|
11
|
+
:settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
|
12
12
|
:mappings => {
|
13
13
|
:tweet => {
|
14
14
|
:_source => { :enabled => true }, :_all => { :enabled => false },
|
15
15
|
:properties => {
|
16
|
-
:message => { :type =>
|
17
|
-
:author => { :type =>
|
16
|
+
:message => { :type => "string", :analyzer => "standard" },
|
17
|
+
:author => { :type => "string", :index => "not_analyzed" }
|
18
18
|
}
|
19
19
|
},
|
20
20
|
:book => {
|
21
21
|
:_source => { :enabled => true }, :_all => { :enabled => false },
|
22
22
|
:properties => {
|
23
|
-
:title => { :type =>
|
24
|
-
:author => { :type =>
|
23
|
+
:title => { :type => "string", :analyzer => "standard" },
|
24
|
+
:author => { :type => "string", :index => "not_analyzed" }
|
25
25
|
}
|
26
26
|
}
|
27
27
|
}
|
@@ -34,7 +34,7 @@ describe Elastomer::Client::Bulk do
|
|
34
34
|
@index.delete if @index.exists?
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
37
|
+
it "performs bulk index actions" do
|
38
38
|
body = [
|
39
39
|
'{"index" : {"_id":"1", "_type":"tweet", "_index":"elastomer-bulk-test"}}',
|
40
40
|
'{"author":"pea53", "message":"just a test tweet"}',
|
@@ -45,16 +45,16 @@ describe Elastomer::Client::Bulk do
|
|
45
45
|
body = body.join "\n"
|
46
46
|
h = $client.bulk body
|
47
47
|
|
48
|
-
assert_bulk_index(h[
|
49
|
-
assert_bulk_index(h[
|
48
|
+
assert_bulk_index(h["items"][0])
|
49
|
+
assert_bulk_index(h["items"][1])
|
50
50
|
|
51
51
|
@index.refresh
|
52
52
|
|
53
|
-
h = @index.docs(
|
54
|
-
assert_equal
|
53
|
+
h = @index.docs("tweet").get :id => 1
|
54
|
+
assert_equal "pea53", h["_source"]["author"]
|
55
55
|
|
56
|
-
h = @index.docs(
|
57
|
-
assert_equal
|
56
|
+
h = @index.docs("book").get :id => 1
|
57
|
+
assert_equal "John Scalzi", h["_source"]["author"]
|
58
58
|
|
59
59
|
|
60
60
|
body = [
|
@@ -66,239 +66,239 @@ describe Elastomer::Client::Bulk do
|
|
66
66
|
body = body.join "\n"
|
67
67
|
h = $client.bulk body, :index => @name
|
68
68
|
|
69
|
-
assert_bulk_index h[
|
70
|
-
assert_bulk_delete h[
|
69
|
+
assert_bulk_index h["items"].first, "expected to index a book"
|
70
|
+
assert_bulk_delete h["items"].last, "expected to delete a book"
|
71
71
|
|
72
72
|
@index.refresh
|
73
73
|
|
74
|
-
h = @index.docs(
|
75
|
-
assert !h[
|
74
|
+
h = @index.docs("book").get :id => 1
|
75
|
+
assert !h["exists"], "was not successfully deleted"
|
76
76
|
|
77
|
-
h = @index.docs(
|
78
|
-
assert_equal
|
77
|
+
h = @index.docs("book").get :id => 2
|
78
|
+
assert_equal "Tolkien", h["_source"]["author"]
|
79
79
|
end
|
80
80
|
|
81
|
-
it
|
81
|
+
it "supports a nice block syntax" do
|
82
82
|
h = @index.bulk do |b|
|
83
|
-
b.index :_id => 1, :_type =>
|
84
|
-
b.index :_id => nil, :_type =>
|
83
|
+
b.index :_id => 1, :_type => "tweet", :author => "pea53", :message => "just a test tweet"
|
84
|
+
b.index :_id => nil, :_type => "book", :author => "John Scalzi", :title => "Old Mans War"
|
85
85
|
end
|
86
|
-
items = h[
|
86
|
+
items = h["items"]
|
87
87
|
|
88
|
-
assert_instance_of Fixnum, h[
|
88
|
+
assert_instance_of Fixnum, h["took"]
|
89
89
|
|
90
|
-
assert_bulk_index h[
|
91
|
-
assert_bulk_create h[
|
90
|
+
assert_bulk_index h["items"].first
|
91
|
+
assert_bulk_create h["items"].last
|
92
92
|
|
93
|
-
book_id = items.last[
|
93
|
+
book_id = items.last["create"]["_id"]
|
94
94
|
assert_match %r/^\S{20,22}$/, book_id
|
95
95
|
|
96
96
|
@index.refresh
|
97
97
|
|
98
|
-
h = @index.docs(
|
99
|
-
assert_equal
|
98
|
+
h = @index.docs("tweet").get :id => 1
|
99
|
+
assert_equal "pea53", h["_source"]["author"]
|
100
100
|
|
101
|
-
h = @index.docs(
|
102
|
-
assert_equal
|
101
|
+
h = @index.docs("book").get :id => book_id
|
102
|
+
assert_equal "John Scalzi", h["_source"]["author"]
|
103
103
|
|
104
104
|
|
105
105
|
h = @index.bulk do |b|
|
106
|
-
b.index :_id =>
|
107
|
-
b.delete :_id => book_id, :_type =>
|
106
|
+
b.index :_id => "", :_type => "book", :author => "Tolkien", :title => "The Silmarillion"
|
107
|
+
b.delete :_id => book_id, :_type => "book"
|
108
108
|
end
|
109
|
-
items = h[
|
109
|
+
items = h["items"]
|
110
110
|
|
111
|
-
assert_bulk_create h[
|
112
|
-
assert_bulk_delete h[
|
111
|
+
assert_bulk_create h["items"].first, "expected to create a book"
|
112
|
+
assert_bulk_delete h["items"].last, "expected to delete a book"
|
113
113
|
|
114
|
-
book_id2 = items.first[
|
114
|
+
book_id2 = items.first["create"]["_id"]
|
115
115
|
assert_match %r/^\S{20,22}$/, book_id2
|
116
116
|
|
117
117
|
@index.refresh
|
118
118
|
|
119
|
-
h = @index.docs(
|
120
|
-
assert !h[
|
119
|
+
h = @index.docs("book").get :id => book_id
|
120
|
+
assert !h["exists"], "was not successfully deleted"
|
121
121
|
|
122
|
-
h = @index.docs(
|
123
|
-
assert_equal
|
122
|
+
h = @index.docs("book").get :id => book_id2
|
123
|
+
assert_equal "Tolkien", h["_source"]["author"]
|
124
124
|
end
|
125
125
|
|
126
|
-
it
|
126
|
+
it "allows documents to be JSON strings" do
|
127
127
|
h = @index.bulk do |b|
|
128
|
-
b.index '{"author":"pea53", "message":"just a test tweet"}', :_id => 1, :_type =>
|
129
|
-
b.create '{"author":"John Scalzi", "title":"Old Mans War"}', :_id => 1, :_type =>
|
128
|
+
b.index '{"author":"pea53", "message":"just a test tweet"}', :_id => 1, :_type => "tweet"
|
129
|
+
b.create '{"author":"John Scalzi", "title":"Old Mans War"}', :_id => 1, :_type => "book"
|
130
130
|
end
|
131
|
-
items = h[
|
131
|
+
items = h["items"]
|
132
132
|
|
133
|
-
assert_instance_of Fixnum, h[
|
133
|
+
assert_instance_of Fixnum, h["took"]
|
134
134
|
|
135
|
-
assert_bulk_index h[
|
136
|
-
assert_bulk_create h[
|
135
|
+
assert_bulk_index h["items"].first
|
136
|
+
assert_bulk_create h["items"].last
|
137
137
|
|
138
138
|
@index.refresh
|
139
139
|
|
140
|
-
h = @index.docs(
|
141
|
-
assert_equal
|
140
|
+
h = @index.docs("tweet").get :id => 1
|
141
|
+
assert_equal "pea53", h["_source"]["author"]
|
142
142
|
|
143
|
-
h = @index.docs(
|
144
|
-
assert_equal
|
143
|
+
h = @index.docs("book").get :id => 1
|
144
|
+
assert_equal "John Scalzi", h["_source"]["author"]
|
145
145
|
|
146
146
|
|
147
147
|
h = @index.bulk do |b|
|
148
|
-
b.index '{"author":"Tolkien", "title":"The Silmarillion"}', :_id => 2, :_type =>
|
149
|
-
b.delete :_id => 1, :_type =>
|
148
|
+
b.index '{"author":"Tolkien", "title":"The Silmarillion"}', :_id => 2, :_type => "book"
|
149
|
+
b.delete :_id => 1, :_type => "book"
|
150
150
|
end
|
151
|
-
items = h[
|
151
|
+
items = h["items"]
|
152
152
|
|
153
|
-
assert_bulk_index h[
|
154
|
-
assert_bulk_delete h[
|
153
|
+
assert_bulk_index h["items"].first, "expected to index a book"
|
154
|
+
assert_bulk_delete h["items"].last, "expected to delete a book"
|
155
155
|
|
156
156
|
@index.refresh
|
157
157
|
|
158
|
-
h = @index.docs(
|
159
|
-
assert !h[
|
158
|
+
h = @index.docs("book").get :id => 1
|
159
|
+
assert !h["exists"], "was not successfully deleted"
|
160
160
|
|
161
|
-
h = @index.docs(
|
162
|
-
assert_equal
|
161
|
+
h = @index.docs("book").get :id => 2
|
162
|
+
assert_equal "Tolkien", h["_source"]["author"]
|
163
163
|
end
|
164
164
|
|
165
|
-
it
|
165
|
+
it "executes a bulk API call when a request size is reached" do
|
166
166
|
ary = []
|
167
167
|
ary << @index.bulk(:request_size => 300) do |b|
|
168
168
|
2.times { |num|
|
169
|
-
document = {:_id => num, :_type =>
|
169
|
+
document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "tweet #{num} is a 100 character request"}
|
170
170
|
ary << b.index(document)
|
171
171
|
}
|
172
172
|
ary.compact!
|
173
173
|
assert_equal 0, ary.length
|
174
174
|
|
175
175
|
7.times { |num|
|
176
|
-
document = {:_id => num+2, :_type =>
|
176
|
+
document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "tweet #{num+2} is a 100 character request"}
|
177
177
|
ary << b.index(document)
|
178
178
|
}
|
179
179
|
ary.compact!
|
180
180
|
assert_equal 3, ary.length
|
181
181
|
|
182
|
-
document = {:_id => 10, :_type =>
|
182
|
+
document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "tweet 10 is a 102 character request"}
|
183
183
|
ary << b.index(document)
|
184
184
|
end
|
185
185
|
ary.compact!
|
186
186
|
|
187
187
|
assert_equal 4, ary.length
|
188
|
-
ary.each { |a| a[
|
188
|
+
ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
|
189
189
|
|
190
190
|
@index.refresh
|
191
|
-
h = @index.docs.search :q =>
|
191
|
+
h = @index.docs.search :q => "*:*", :search_type => "count"
|
192
192
|
|
193
|
-
assert_equal 10, h[
|
193
|
+
assert_equal 10, h["hits"]["total"]
|
194
194
|
end
|
195
195
|
|
196
|
-
it
|
196
|
+
it "executes a bulk API call when an action count is reached" do
|
197
197
|
ary = []
|
198
198
|
ary << @index.bulk(:action_count => 3) do |b|
|
199
199
|
2.times { |num|
|
200
|
-
document = {:_id => num, :_type =>
|
200
|
+
document = {:_id => num, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num}"}
|
201
201
|
ary << b.index(document)
|
202
202
|
}
|
203
203
|
ary.compact!
|
204
204
|
assert_equal 0, ary.length
|
205
205
|
|
206
206
|
7.times { |num|
|
207
|
-
document = {:_id => num+2, :_type =>
|
207
|
+
document = {:_id => num+2, :_type => "tweet", :author => "pea53", :message => "this is tweet number #{num+2}"}
|
208
208
|
ary << b.index(document)
|
209
209
|
}
|
210
210
|
ary.compact!
|
211
211
|
assert_equal 3, ary.length
|
212
212
|
|
213
|
-
document = {:_id => 10, :_type =>
|
213
|
+
document = {:_id => 10, :_type => "tweet", :author => "pea53", :message => "this is tweet number 10"}
|
214
214
|
ary << b.index(document)
|
215
215
|
end
|
216
216
|
ary.compact!
|
217
217
|
|
218
218
|
assert_equal 4, ary.length
|
219
|
-
ary.each { |a| a[
|
219
|
+
ary.each { |a| a["items"].each { |b| assert_bulk_index(b) } }
|
220
220
|
|
221
221
|
@index.refresh
|
222
|
-
h = @index.docs.search :q =>
|
222
|
+
h = @index.docs.search :q => "*:*", :search_type => "count"
|
223
223
|
|
224
|
-
assert_equal 10, h[
|
224
|
+
assert_equal 10, h["hits"]["total"]
|
225
225
|
end
|
226
226
|
|
227
|
-
it
|
227
|
+
it "uses :id from parameters" do
|
228
228
|
response = @index.bulk do |b|
|
229
|
-
document = { :_type =>
|
230
|
-
params = { :id =>
|
229
|
+
document = { :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
|
230
|
+
params = { :id => "foo" }
|
231
231
|
|
232
232
|
b.index document, params
|
233
233
|
end
|
234
234
|
|
235
|
-
assert_instance_of Fixnum, response[
|
235
|
+
assert_instance_of Fixnum, response["took"]
|
236
236
|
|
237
|
-
items = response[
|
237
|
+
items = response["items"]
|
238
238
|
assert_bulk_index(items[0])
|
239
239
|
|
240
|
-
assert_equal
|
240
|
+
assert_equal "foo", items[0]["index"]["_id"]
|
241
241
|
end
|
242
242
|
|
243
|
-
it
|
243
|
+
it "supports symbol and string parameters" do
|
244
244
|
response = @index.bulk do |b|
|
245
|
-
doc1 = { :author =>
|
246
|
-
b.index doc1, { :id =>
|
245
|
+
doc1 = { :author => "pea53", :message => "a tweet about foo" }
|
246
|
+
b.index doc1, { :id => "foo", :type => "tweet" }
|
247
247
|
|
248
|
-
doc2 = { :author =>
|
249
|
-
b.index doc2, {
|
248
|
+
doc2 = { :author => "pea53", :message => "a tweet about bar" }
|
249
|
+
b.index doc2, { "id" => "bar", "type" => "tweet" }
|
250
250
|
end
|
251
251
|
|
252
|
-
assert_instance_of Fixnum, response[
|
252
|
+
assert_instance_of Fixnum, response["took"]
|
253
253
|
|
254
|
-
items = response[
|
254
|
+
items = response["items"]
|
255
255
|
assert_bulk_index(items[0])
|
256
256
|
assert_bulk_index(items[1])
|
257
257
|
|
258
|
-
assert_equal
|
259
|
-
assert_equal
|
258
|
+
assert_equal "a tweet about foo", @index.docs("tweet").get(:id => "foo")["_source"]["message"]
|
259
|
+
assert_equal "a tweet about bar", @index.docs("tweet").get(:id => "bar")["_source"]["message"]
|
260
260
|
end
|
261
261
|
|
262
262
|
it 'doesn\'t override parameters from the document' do
|
263
|
-
document = { :_id => 1, :_type =>
|
263
|
+
document = { :_id => 1, :_type => "tweet", :author => "pea53", :message => "just a test tweet" }
|
264
264
|
params = { :id => 2 }
|
265
265
|
|
266
266
|
response = @index.bulk do |b|
|
267
267
|
b.index document, params
|
268
268
|
end
|
269
269
|
|
270
|
-
assert_instance_of Fixnum, response[
|
270
|
+
assert_instance_of Fixnum, response["took"]
|
271
271
|
|
272
|
-
items = response[
|
272
|
+
items = response["items"]
|
273
273
|
assert_bulk_index(items[0])
|
274
274
|
|
275
|
-
refute_found @index.docs(
|
276
|
-
assert_equal
|
275
|
+
refute_found @index.docs("tweet").get(:id => 1)
|
276
|
+
assert_equal "just a test tweet", @index.docs("tweet").get(:id => 2)["_source"]["message"]
|
277
277
|
end
|
278
278
|
|
279
279
|
it 'doesn\'t upgrade non-prefixed keys to parameters' do
|
280
|
-
document = { :id => 1, :type =>
|
281
|
-
params = { :id => 2, :type =>
|
280
|
+
document = { :id => 1, :type => "book", :version => 5, :author => "pea53", :message => "just a test tweet" }
|
281
|
+
params = { :id => 2, :type => "tweet" }
|
282
282
|
|
283
283
|
response = @index.bulk do |b|
|
284
284
|
b.index document, params
|
285
285
|
end
|
286
286
|
|
287
|
-
assert_instance_of Fixnum, response[
|
287
|
+
assert_instance_of Fixnum, response["took"]
|
288
288
|
|
289
|
-
items = response[
|
289
|
+
items = response["items"]
|
290
290
|
assert_bulk_index(items[0])
|
291
291
|
|
292
|
-
assert_equal
|
293
|
-
assert_equal
|
294
|
-
assert_equal 1, items[0][
|
292
|
+
assert_equal "2", items[0]["index"]["_id"]
|
293
|
+
assert_equal "tweet", items[0]["index"]["_type"]
|
294
|
+
assert_equal 1, items[0]["index"]["_version"]
|
295
295
|
end
|
296
296
|
|
297
|
-
it
|
297
|
+
it "streams bulk responses" do
|
298
298
|
ops = [
|
299
|
-
[:index, { :message =>
|
300
|
-
[:index, { :message =>
|
301
|
-
[:index, { :message =>
|
299
|
+
[:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
|
300
|
+
[:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
|
301
|
+
[:index, { :message => "tweet 3" }, { :_id => 3, :_type => "book", :_index => @index.name }]
|
302
302
|
]
|
303
303
|
responses = $client.bulk_stream_responses(ops, { :action_count => 2 }).to_a
|
304
304
|
assert_equal(2, responses.length)
|
@@ -307,11 +307,11 @@ describe Elastomer::Client::Bulk do
|
|
307
307
|
assert_bulk_index(responses[1]["items"][0])
|
308
308
|
end
|
309
309
|
|
310
|
-
it
|
310
|
+
it "streams bulk items" do
|
311
311
|
ops = [
|
312
|
-
[:index, { :message =>
|
313
|
-
[:index, { :message =>
|
314
|
-
[:index, { :message =>
|
312
|
+
[:index, { :message => "tweet 1" }, { :_id => 1, :_type => "book", :_index => @index.name }],
|
313
|
+
[:index, { :message => "tweet 2" }, { :_id => 2, :_type => "book", :_index => @index.name }],
|
314
|
+
[:index, { :message => "tweet 3" }, { :_id => 3, :_type => "book", :_index => @index.name }]
|
315
315
|
]
|
316
316
|
items = []
|
317
317
|
stats = $client.bulk_stream_items(ops, { :action_count => 2 }) { |item| items << item }
|