elasticsearch 8.9.0 → 9.4.3
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/elasticsearch.gemspec +14 -11
- data/lib/elasticsearch/helpers/bulk_helper.rb +11 -6
- data/lib/elasticsearch/helpers/esql_helper.rb +72 -0
- data/lib/elasticsearch/helpers/scroll_helper.rb +4 -14
- data/lib/elasticsearch/version.rb +1 -1
- data/lib/elasticsearch.rb +42 -9
- metadata +50 -52
- data/Gemfile +0 -27
- data/Rakefile +0 -54
- data/spec/integration/characters_escaping_spec.rb +0 -94
- data/spec/integration/client_integration_spec.rb +0 -63
- data/spec/integration/helpers/bulk_helper_spec.rb +0 -211
- data/spec/integration/helpers/scroll_helper_spec.rb +0 -91
- data/spec/spec_helper.rb +0 -31
- data/spec/unit/api_key_spec.rb +0 -137
- data/spec/unit/cloud_credentials_spec.rb +0 -167
- data/spec/unit/custom_transport_implementation_spec.rb +0 -43
- data/spec/unit/elasticsearch_product_validation_spec.rb +0 -148
- data/spec/unit/headers_spec.rb +0 -55
- data/spec/unit/opaque_id_spec.rb +0 -48
- data/spec/unit/user_agent_spec.rb +0 -69
- data/spec/unit/wrapper_gem_spec.rb +0 -33
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
|
|
18
|
-
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
|
|
19
|
-
|
|
20
|
-
require 'spec_helper'
|
|
21
|
-
|
|
22
|
-
context 'Elasticsearch client' do
|
|
23
|
-
let(:client) do
|
|
24
|
-
Elasticsearch::Client.new(host: ELASTICSEARCH_URL, user: 'elastic', password: 'changeme')
|
|
25
|
-
end
|
|
26
|
-
let(:index) { 'tvs' }
|
|
27
|
-
|
|
28
|
-
after do
|
|
29
|
-
client.indices.delete(index: index)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
context 'escaping spaces in ids' do
|
|
33
|
-
it 'escapes spaces for id when using index' do
|
|
34
|
-
response = client.index(index: index, id: 'a test 1', body: { name: 'A test 1' }, refresh: true)
|
|
35
|
-
expect(response.body['_id']).to eq 'a test 1'
|
|
36
|
-
|
|
37
|
-
response = client.search(index: index)
|
|
38
|
-
expect(response.body['hits']['hits'].first['_id']).to eq 'a test 1'
|
|
39
|
-
|
|
40
|
-
# Raises exception, _id is unrecognized
|
|
41
|
-
expect do
|
|
42
|
-
client.index(index: index, _id: 'a test 2', body: { name: 'A test 2' })
|
|
43
|
-
end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest
|
|
44
|
-
|
|
45
|
-
# Raises exception, id is a query parameter
|
|
46
|
-
expect do
|
|
47
|
-
client.index(index: index, body: { name: 'A test 3', _id: 'a test 3' })
|
|
48
|
-
end.to raise_exception Elastic::Transport::Transport::Errors::BadRequest
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it 'escapes spaces for id when using create' do
|
|
52
|
-
# Works with create
|
|
53
|
-
response = client.create(index: index, id: 'a test 4', body: { name: 'A test 4' })
|
|
54
|
-
expect(response.body['_id']).to eq 'a test 4'
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
it 'escapes spaces for id when using bulk' do
|
|
58
|
-
body = [
|
|
59
|
-
{ create: { _index: index, _id: 'a test 5', data: { name: 'A test 5' } } }
|
|
60
|
-
]
|
|
61
|
-
expect(client.bulk(body: body, refresh: true).status).to eq 200
|
|
62
|
-
|
|
63
|
-
response = client.search(index: index)
|
|
64
|
-
expect(
|
|
65
|
-
response.body['hits']['hits'].select { |a| a['_id'] == 'a test 5' }.size
|
|
66
|
-
).to eq 1
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context 'it doesnae escape plus signs in id' do
|
|
71
|
-
it 'escapes spaces for id when using index' do
|
|
72
|
-
response = client.index(index: index, id: 'a+test+1', body: { name: 'A test 1' })
|
|
73
|
-
expect(response.body['_id']).to eq 'a+test+1'
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it 'escapes spaces for id when using create' do
|
|
77
|
-
# Works with create
|
|
78
|
-
response = client.create(index: index, id: 'a+test+2', body: { name: 'A test 2' })
|
|
79
|
-
expect(response.body['_id']).to eq 'a+test+2'
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
it 'escapes spaces for id when using bulk' do
|
|
83
|
-
body = [
|
|
84
|
-
{ create: { _index: index, _id: 'a+test+3', data: { name: 'A test 3' } } }
|
|
85
|
-
]
|
|
86
|
-
expect(client.bulk(body: body, refresh: true).status).to eq 200
|
|
87
|
-
|
|
88
|
-
response = client.search(index: index)
|
|
89
|
-
expect(
|
|
90
|
-
response.body['hits']['hits'].select { |a| a['_id'] == 'a+test+3' }.size
|
|
91
|
-
).to eq 1
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
end
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
|
|
18
|
-
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
|
|
19
|
-
|
|
20
|
-
require 'spec_helper'
|
|
21
|
-
require 'logger'
|
|
22
|
-
|
|
23
|
-
context 'Elasticsearch client' do
|
|
24
|
-
let(:logger) { Logger.new($stderr) }
|
|
25
|
-
|
|
26
|
-
let(:client) do
|
|
27
|
-
Elasticsearch::Client.new(
|
|
28
|
-
host: ELASTICSEARCH_URL,
|
|
29
|
-
logger: logger,
|
|
30
|
-
user: 'elastic',
|
|
31
|
-
password: 'changeme'
|
|
32
|
-
)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
context 'Integrates with elasticsearch API' do
|
|
36
|
-
it 'should perform the API methods' do
|
|
37
|
-
expect do
|
|
38
|
-
# Index a document
|
|
39
|
-
client.index(index: 'test-index', id: '1', body: { title: 'Test' })
|
|
40
|
-
|
|
41
|
-
# Refresh the index
|
|
42
|
-
client.indices.refresh(index: 'test-index')
|
|
43
|
-
|
|
44
|
-
# Search
|
|
45
|
-
response = client.search(index: 'test-index', body: { query: { match: { title: 'test' } } })
|
|
46
|
-
|
|
47
|
-
expect(response['hits']['total']['value']).to eq 1
|
|
48
|
-
expect(response['hits']['hits'][0]['_source']['title']).to eq 'Test'
|
|
49
|
-
|
|
50
|
-
# Delete the index
|
|
51
|
-
client.indices.delete(index: 'test-index')
|
|
52
|
-
end.not_to raise_error
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
context 'Reports the right meta header' do
|
|
57
|
-
it 'Reports es service name and gem versio' do
|
|
58
|
-
headers = client.transport.connections.first.connection.headers
|
|
59
|
-
version = Class.new.extend(Elastic::Transport::MetaHeader).send(:client_meta_version, Elasticsearch::VERSION)
|
|
60
|
-
expect(headers['x-elastic-client-meta']).to match /^es=#{version}/
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
|
|
18
|
-
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
|
|
19
|
-
|
|
20
|
-
require 'elasticsearch/helpers/bulk_helper'
|
|
21
|
-
require 'spec_helper'
|
|
22
|
-
require 'tempfile'
|
|
23
|
-
|
|
24
|
-
context 'Elasticsearch client helpers' do
|
|
25
|
-
context 'Bulk helper' do
|
|
26
|
-
let(:client) do
|
|
27
|
-
Elasticsearch::Client.new(
|
|
28
|
-
host: ELASTICSEARCH_URL,
|
|
29
|
-
user: 'elastic',
|
|
30
|
-
password: 'changeme'
|
|
31
|
-
)
|
|
32
|
-
end
|
|
33
|
-
let(:index) { 'bulk_animals' }
|
|
34
|
-
let(:params) { { refresh: 'wait_for' } }
|
|
35
|
-
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
|
|
36
|
-
let(:docs) do
|
|
37
|
-
[
|
|
38
|
-
{ scientific_name: 'Lama guanicoe', name:'Guanaco' },
|
|
39
|
-
{ scientific_name: 'Tayassu pecari', name:'White-lipped peccary' },
|
|
40
|
-
{ scientific_name: 'Snycerus caffer', name:'Buffalo, african' },
|
|
41
|
-
{ scientific_name: 'Coluber constrictor', name:'Snake, racer' },
|
|
42
|
-
{ scientific_name: 'Thalasseus maximus', name:'Royal tern' },
|
|
43
|
-
{ scientific_name: 'Centrocercus urophasianus', name:'Hen, sage' },
|
|
44
|
-
{ scientific_name: 'Sitta canadensis', name:'Nuthatch, red-breasted' },
|
|
45
|
-
{ scientific_name: 'Aegypius tracheliotus', name:'Vulture, lappet-faced' },
|
|
46
|
-
{ scientific_name: 'Bucephala clangula', name:'Common goldeneye' },
|
|
47
|
-
{ scientific_name: 'Felis pardalis', name:'Ocelot' }
|
|
48
|
-
]
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
after do
|
|
52
|
-
client.indices.delete(index: index, ignore: 404)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it 'Ingests documents' do
|
|
56
|
-
response = bulk_helper.ingest(docs)
|
|
57
|
-
expect(response).to be_an_instance_of Elasticsearch::API::Response
|
|
58
|
-
expect(response.status).to eq(200)
|
|
59
|
-
expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'Updates documents' do
|
|
63
|
-
docs = [
|
|
64
|
-
{ scientific_name: 'Otocyon megalotos', name: 'Bat-eared fox' },
|
|
65
|
-
{ scientific_name: 'Herpestes javanicus', name: 'Small Indian mongoose' }
|
|
66
|
-
]
|
|
67
|
-
bulk_helper.ingest(docs)
|
|
68
|
-
# Get the ingested documents, add id and modify them to update them:
|
|
69
|
-
animals = client.search(index: index)['hits']['hits']
|
|
70
|
-
# Add id to each doc
|
|
71
|
-
docs = animals.map { |animal| animal['_source'].merge({'id' => animal['_id'] }) }
|
|
72
|
-
docs.map { |doc| doc['scientific_name'].upcase! }
|
|
73
|
-
response = bulk_helper.update(docs)
|
|
74
|
-
expect(response.status).to eq(200)
|
|
75
|
-
expect(response['items'].map { |i| i['update']['result'] }.uniq.first).to eq('updated')
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
it 'Deletes documents' do
|
|
79
|
-
response = bulk_helper.ingest(docs)
|
|
80
|
-
ids = response.body['items'].map { |a| a['index']['_id'] }
|
|
81
|
-
response = bulk_helper.delete(ids)
|
|
82
|
-
expect(response.status).to eq 200
|
|
83
|
-
expect(response['items'].map { |item| item['delete']['result'] }.uniq.first).to eq('deleted')
|
|
84
|
-
expect(client.count(index: index)['count']).to eq(0)
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
it 'Ingests documents and yields response and docs' do
|
|
88
|
-
slice = 2
|
|
89
|
-
response = bulk_helper.ingest(docs, {slice: slice}) do |response, docs|
|
|
90
|
-
expect(response).to be_an_instance_of Elasticsearch::API::Response
|
|
91
|
-
expect(docs.count).to eq slice
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
context 'JSON File helper' do
|
|
96
|
-
let(:file) { Tempfile.new('test-data.json') }
|
|
97
|
-
let(:json) do
|
|
98
|
-
json = <<~JSON
|
|
99
|
-
[
|
|
100
|
-
{
|
|
101
|
-
"character_name": "Anallese Lonie",
|
|
102
|
-
"species": "mouse",
|
|
103
|
-
"catchphrase": "Seamless regional definition",
|
|
104
|
-
"favorite_food": "pizza"
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
"character_name": "Janey Davidovsky",
|
|
108
|
-
"species": "cat",
|
|
109
|
-
"catchphrase": "Down-sized responsive pricing structure",
|
|
110
|
-
"favorite_food": "pizza"
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
"character_name": "Morse Mountford",
|
|
114
|
-
"species": "cat",
|
|
115
|
-
"catchphrase": "Ameliorated modular data-warehouse",
|
|
116
|
-
"favorite_food": "carrots"
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
"character_name": "Saundra Kauble",
|
|
120
|
-
"species": "dog",
|
|
121
|
-
"catchphrase": "Synchronised 24/7 support",
|
|
122
|
-
"favorite_food": "carrots"
|
|
123
|
-
},
|
|
124
|
-
{
|
|
125
|
-
"character_name": "Kain Viggars",
|
|
126
|
-
"species": "cat",
|
|
127
|
-
"catchphrase": "Open-architected asymmetric circuit",
|
|
128
|
-
"favorite_food": "carrots"
|
|
129
|
-
}
|
|
130
|
-
]
|
|
131
|
-
JSON
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
before do
|
|
135
|
-
file.write(json)
|
|
136
|
-
file.rewind
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
after do
|
|
140
|
-
file.close
|
|
141
|
-
file.unlink
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
it 'Ingests a JSON file' do
|
|
145
|
-
response = bulk_helper.ingest_json(file)
|
|
146
|
-
|
|
147
|
-
expect(response).to be_an_instance_of Elasticsearch::API::Response
|
|
148
|
-
expect(response.status).to eq(200)
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
context 'with data not in root of JSON file' do
|
|
152
|
-
let(:json) do
|
|
153
|
-
json = <<~JSON
|
|
154
|
-
{
|
|
155
|
-
"field": "value",
|
|
156
|
-
"status": 200,
|
|
157
|
-
"data": {
|
|
158
|
-
"items": [
|
|
159
|
-
{
|
|
160
|
-
"character_name": "Anallese Lonie",
|
|
161
|
-
"species": "mouse",
|
|
162
|
-
"catchphrase": "Seamless regional definition",
|
|
163
|
-
"favorite_food": "pizza"
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
"character_name": "Janey Davidovsky",
|
|
167
|
-
"species": "cat",
|
|
168
|
-
"catchphrase": "Down-sized responsive pricing structure",
|
|
169
|
-
"favorite_food": "pizza"
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
"character_name": "Morse Mountford",
|
|
173
|
-
"species": "cat",
|
|
174
|
-
"catchphrase": "Ameliorated modular data-warehouse",
|
|
175
|
-
"favorite_food": "carrots"
|
|
176
|
-
},
|
|
177
|
-
{
|
|
178
|
-
"character_name": "Saundra Kauble",
|
|
179
|
-
"species": "dog",
|
|
180
|
-
"catchphrase": "Synchronised 24/7 support",
|
|
181
|
-
"favorite_food": "carrots"
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
"character_name": "Kain Viggars",
|
|
185
|
-
"species": "cat",
|
|
186
|
-
"catchphrase": "Open-architected asymmetric circuit",
|
|
187
|
-
"favorite_food": "carrots"
|
|
188
|
-
}
|
|
189
|
-
]
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
JSON
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
it 'Ingests a JSON file passing keys as Array' do
|
|
196
|
-
response = bulk_helper.ingest_json(file, { keys: ['data', 'items'] })
|
|
197
|
-
expect(response).to be_an_instance_of Elasticsearch::API::Response
|
|
198
|
-
expect(response.status).to eq(200)
|
|
199
|
-
expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
it 'Ingests a JSON file passing keys as String' do
|
|
203
|
-
response = bulk_helper.ingest_json(file, { keys: 'data,items' })
|
|
204
|
-
expect(response).to be_an_instance_of Elasticsearch::API::Response
|
|
205
|
-
expect(response.status).to eq(200)
|
|
206
|
-
expect(response['items'].map { |a| a['index']['status'] }.uniq.first).to eq 201
|
|
207
|
-
end
|
|
208
|
-
end
|
|
209
|
-
end
|
|
210
|
-
end
|
|
211
|
-
end
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
ELASTICSEARCH_URL = ENV['TEST_ES_SERVER'] || "http://localhost:#{(ENV['PORT'] || 9200)}"
|
|
18
|
-
raise URI::InvalidURIError unless ELASTICSEARCH_URL =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
|
|
19
|
-
|
|
20
|
-
require 'spec_helper'
|
|
21
|
-
require 'elasticsearch/helpers/scroll_helper'
|
|
22
|
-
|
|
23
|
-
context 'Elasticsearch client helpers' do
|
|
24
|
-
let(:client) do
|
|
25
|
-
Elasticsearch::Client.new(
|
|
26
|
-
host: ELASTICSEARCH_URL,
|
|
27
|
-
user: 'elastic',
|
|
28
|
-
password: 'changeme'
|
|
29
|
-
)
|
|
30
|
-
end
|
|
31
|
-
let(:index) { 'books' }
|
|
32
|
-
let(:body) { { size: 12, query: { match_all: {} } } }
|
|
33
|
-
let(:scroll_helper) { Elasticsearch::Helpers::ScrollHelper.new(client, index, body) }
|
|
34
|
-
|
|
35
|
-
before do
|
|
36
|
-
documents = [
|
|
37
|
-
{ index: { _index: index, data: {name: "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} } },
|
|
38
|
-
{ index: { _index: index, data: {name: "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} } },
|
|
39
|
-
{ index: { _index: index, data: {name: "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} } },
|
|
40
|
-
{ index: { _index: index, data: {name: "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331} } },
|
|
41
|
-
{ index: { _index: index, data: {name: "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408} } },
|
|
42
|
-
{ index: { _index: index, data: {name: "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454} } },
|
|
43
|
-
{ index: { _index: index, data: {name: "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471} } },
|
|
44
|
-
{ index: { _index: index, data: {name: "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768} } },
|
|
45
|
-
{ index: { _index: index, data: {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} } },
|
|
46
|
-
{ index: { _index: index, data: {name: "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613} } },
|
|
47
|
-
{ index: { _index: index, data: {name: "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324} } },
|
|
48
|
-
{ index: { _index: index, data: {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} } },
|
|
49
|
-
{ index: { _index: index, data: {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} } },
|
|
50
|
-
{ index: { _index: index, data: {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} } },
|
|
51
|
-
{ index: { _index: index, data: {name: "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224} } },
|
|
52
|
-
{ index: { _index: index, data: {name: "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208} } },
|
|
53
|
-
{ index: { _index: index, data: {name: "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275} } },
|
|
54
|
-
{ index: { _index: index, data: {name: "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180} } },
|
|
55
|
-
{ index: { _index: index, data: {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470} } },
|
|
56
|
-
{ index: { _index: index, data: {name: "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271} } },
|
|
57
|
-
{ index: { _index: index, data: {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} } },
|
|
58
|
-
{ index: { _index: index, data: {name: "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335} } },
|
|
59
|
-
{ index: { _index: index, data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } },
|
|
60
|
-
{ index: { _index: index, data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } }
|
|
61
|
-
]
|
|
62
|
-
client.bulk(body: documents, refresh: 'wait_for')
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
after do
|
|
66
|
-
client.indices.delete(index: index)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
it 'instantiates a scroll helper' do
|
|
70
|
-
expect(scroll_helper).to be_an_instance_of Elasticsearch::Helpers::ScrollHelper
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
it 'searches an index' do
|
|
74
|
-
my_documents = []
|
|
75
|
-
while !(documents = scroll_helper.results).empty?
|
|
76
|
-
my_documents << documents
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
expect(my_documents.flatten.size).to eq 24
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
it 'uses enumerable' do
|
|
83
|
-
count = 0
|
|
84
|
-
scroll_helper.each { |a| count += 1 }
|
|
85
|
-
expect(count).to eq 24
|
|
86
|
-
expect(scroll_helper).to respond_to(:count)
|
|
87
|
-
expect(scroll_helper).to respond_to(:reject)
|
|
88
|
-
expect(scroll_helper).to respond_to(:uniq)
|
|
89
|
-
expect(scroll_helper.map { |a| a['_id'] }.uniq.count).to eq 24
|
|
90
|
-
end
|
|
91
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
|
|
18
|
-
require 'elasticsearch'
|
|
19
|
-
require 'rspec'
|
|
20
|
-
|
|
21
|
-
RSpec.configure do |config|
|
|
22
|
-
config.formatter = :documentation
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def meta_version
|
|
26
|
-
client.send(:client_meta_version, Elasticsearch::VERSION)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def jruby?
|
|
30
|
-
defined?(JRUBY_VERSION)
|
|
31
|
-
end
|
data/spec/unit/api_key_spec.rb
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
|
3
|
-
# this work for additional information regarding copyright
|
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
-
# not use this file except in compliance with the License.
|
|
7
|
-
# You may obtain a copy of the License at
|
|
8
|
-
#
|
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
#
|
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
|
12
|
-
# software distributed under the License is distributed on an
|
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
-
# KIND, either express or implied. See the License for the
|
|
15
|
-
# specific language governing permissions and limitations
|
|
16
|
-
# under the License.
|
|
17
|
-
|
|
18
|
-
require 'spec_helper'
|
|
19
|
-
|
|
20
|
-
describe Elasticsearch::Client do
|
|
21
|
-
context 'when using API Key' do
|
|
22
|
-
let(:authorization_header) do
|
|
23
|
-
client.transport.connections.first.connection.headers['Authorization']
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
context 'when an encoded api_key is provided' do
|
|
27
|
-
let(:client) do
|
|
28
|
-
described_class.new(api_key: 'an_api_key')
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'Adds the ApiKey header to the connection' do
|
|
32
|
-
expect(authorization_header).to eq('ApiKey an_api_key')
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
context 'when an un-encoded api_key is provided' do
|
|
37
|
-
let(:client) do
|
|
38
|
-
described_class.new(api_key: { id: 'my_id', api_key: 'my_api_key' })
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
it 'Adds the ApiKey header to the connection' do
|
|
42
|
-
expect(authorization_header).to eq("ApiKey #{Base64.strict_encode64('my_id:my_api_key')}")
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
context 'when basic auth and api_key are provided' do
|
|
47
|
-
let(:client) do
|
|
48
|
-
described_class.new(
|
|
49
|
-
api_key: { id: 'my_id', api_key: 'my_api_key' },
|
|
50
|
-
host: 'http://elastic:password@localhost:9200'
|
|
51
|
-
)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it 'removes basic auth credentials' do
|
|
55
|
-
expect(authorization_header).not_to match(/^Basic/)
|
|
56
|
-
expect(authorization_header).to match(/^ApiKey/)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
context 'when other headers were specified' do
|
|
61
|
-
let(:client) do
|
|
62
|
-
described_class.new(
|
|
63
|
-
api_key: 'elasticsearch_api_key',
|
|
64
|
-
transport_options: { headers: { 'x-test-header' => 'test' } }
|
|
65
|
-
)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
it 'Adds the ApiKey header to the connection and keeps the header' do
|
|
69
|
-
header = client.transport.connections.first.connection.headers
|
|
70
|
-
expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
|
|
71
|
-
expect(header['X-Test-Header']).to eq('test')
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
context 'when sending transport_options but no headers were specified' do
|
|
76
|
-
let(:client) do
|
|
77
|
-
described_class.new(
|
|
78
|
-
api_key: 'elasticsearch_api_key',
|
|
79
|
-
transport_options: { ssl: { verify: false } }
|
|
80
|
-
)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it 'Adds the ApiKey header to the connection and keeps the options' do
|
|
84
|
-
header = client.transport.connections.first.connection.headers
|
|
85
|
-
expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
|
|
86
|
-
expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
|
|
87
|
-
expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
context 'when other headers and options were specified' do
|
|
92
|
-
let(:client) do
|
|
93
|
-
described_class.new(
|
|
94
|
-
api_key: 'elasticsearch_api_key',
|
|
95
|
-
transport_options: {
|
|
96
|
-
headers: { 'x-test-header' => 'test' },
|
|
97
|
-
ssl: { verify: false }
|
|
98
|
-
}
|
|
99
|
-
)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
it 'Adds the ApiKey header to the connection and keeps the header' do
|
|
103
|
-
header = client.transport.connections.first.connection.headers
|
|
104
|
-
expect(header['X-Test-Header']).to eq('test')
|
|
105
|
-
expect(header['Authorization']).to eq('ApiKey elasticsearch_api_key')
|
|
106
|
-
expect(client.transport.options[:transport_options]).to include({ ssl: { verify: false } })
|
|
107
|
-
expect(client.transport.options[:transport_options][:headers]).to include('Authorization' => 'ApiKey elasticsearch_api_key')
|
|
108
|
-
end
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
context 'Metaheader' do
|
|
112
|
-
let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
|
|
113
|
-
let(:meta_header) do
|
|
114
|
-
if jruby?
|
|
115
|
-
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elastic::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
|
|
116
|
-
else
|
|
117
|
-
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elastic::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
context 'when using API Key' do
|
|
122
|
-
let(:client) do
|
|
123
|
-
described_class.new(api_key: 'an_api_key')
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
let(:headers) do
|
|
127
|
-
client.transport.connections.first.connection.headers
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
it 'adds the ApiKey header to the connection' do
|
|
131
|
-
expect(authorization_header).to eq('ApiKey an_api_key')
|
|
132
|
-
expect(headers).to include('x-elastic-client-meta' => meta_header)
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
end
|