-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathelasticsearch_spec.rb
292 lines (257 loc) · 9.32 KB
/
elasticsearch_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
require "logstash/inputs/elasticsearch"
require "elasticsearch"
describe LogStash::Inputs::Elasticsearch do
it_behaves_like "an interruptible input plugin" do
let(:esclient) { double("elasticsearch-client") }
let(:config) { { } }
before :each do
allow(Elasticsearch::Client).to receive(:new).and_return(esclient)
hit = {
"_index" => "logstash-2014.10.12",
"_type" => "logs",
"_id" => "C5b2xLQwTZa76jBmHIbwHQ",
"_score" => 1.0,
"_source" => { "message" => ["ohayo"] },
"fields" => { "message_copy" => ["ohayo"] }
}
allow(esclient).to receive(:search) { { "hits" => { "hits" => [hit] } } }
allow(esclient).to receive(:scroll) { { "hits" => { "hits" => [hit] } } }
end
end
it "should retrieve json event from elasticseach" do
config = %q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
}
}
]
response = {
"_scroll_id" => "cXVlcnlUaGVuRmV0Y2g",
"took" => 27,
"timed_out" => false,
"_shards" => {
"total" => 169,
"successful" => 169,
"failed" => 0
},
"hits" => {
"total" => 1,
"max_score" => 1.0,
"hits" => [ {
"_index" => "logstash-2014.10.12",
"_type" => "logs",
"_id" => "C5b2xLQwTZa76jBmHIbwHQ",
"_score" => 1.0,
"_source" => { "message" => ["ohayo"] },
"fields" => { "message_copy" => ["ohayo"] }
} ]
}
}
scroll_reponse = {
"_scroll_id" => "r453Wc1jh0caLJhSDg",
"hits" => { "hits" => [] }
}
client = Elasticsearch::Client.new
expect(Elasticsearch::Client).to receive(:new).with(any_args).and_return(client)
expect(client).to receive(:search).with(any_args).and_return(response)
expect(client).to receive(:scroll).with({ :body => { :scroll_id => "cXVlcnlUaGVuRmV0Y2g" }, :scroll=> "1m" }).and_return(scroll_reponse)
event = input(config) do |pipeline, queue|
queue.pop
end
insist { event }.is_a?(LogStash::Event)
insist { event.get("message") } == [ "ohayo" ]
end
context "with Elasticsearch document information" do
let!(:response) do
{
"_scroll_id" => "cXVlcnlUaGVuRmV0Y2g",
"took" => 27,
"timed_out" => false,
"_shards" => {
"total" => 169,
"successful" => 169,
"failed" => 0
},
"hits" => {
"total" => 1,
"max_score" => 1.0,
"hits" => [ {
"_index" => "logstash-2014.10.12",
"_type" => "logs",
"_id" => "C5b2xLQwTZa76jBmHIbwHQ",
"_score" => 1.0,
"_source" => {
"message" => ["ohayo"],
"metadata_with_hash" => { "awesome" => "logstash" },
"metadata_with_string" => "a string"
},
"fields" => { "message_copy" => ["ohayo"] }
} ]
}
}
end
let(:scroll_reponse) do
{
"_scroll_id" => "r453Wc1jh0caLJhSDg",
"hits" => { "hits" => [] }
}
end
let(:client) { Elasticsearch::Client.new }
before do
expect(Elasticsearch::Client).to receive(:new).with(any_args).and_return(client)
expect(client).to receive(:search).with(any_args).and_return(response)
allow(client).to receive(:scroll).with({ :body => {:scroll_id => "cXVlcnlUaGVuRmV0Y2g"}, :scroll => "1m" }).and_return(scroll_reponse)
end
context 'when defining docinfo' do
let(:config_metadata) do
%q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
docinfo => true
}
}
]
end
it 'merges the values if the `docinfo_target` already exist in the `_source` document' do
metadata_field = 'metadata_with_hash'
config_metadata_with_hash = %Q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
docinfo => true
docinfo_target => '#{metadata_field}'
}
}
]
event = input(config_metadata_with_hash) do |pipeline, queue|
queue.pop
end
expect(event.get("[#{metadata_field}][_index]")).to eq('logstash-2014.10.12')
expect(event.get("[#{metadata_field}][_type]")).to eq('logs')
expect(event.get("[#{metadata_field}][_id]")).to eq('C5b2xLQwTZa76jBmHIbwHQ')
expect(event.get("[#{metadata_field}][awesome]")).to eq("logstash")
end
it 'thows an exception if the `docinfo_target` exist but is not of type hash' do
metadata_field = 'metadata_with_string'
config_metadata_with_string = %Q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
docinfo => true
docinfo_target => '#{metadata_field}'
}
}
]
pipeline = new_pipeline_from_string(config_metadata_with_string)
queue = Queue.new
pipeline.instance_eval do
@output_func = lambda { |event| queue << event }
end
expect { pipeline.run }.to raise_error(Exception, /incompatible event/)
end
it "should move the document info to the @metadata field" do
event = input(config_metadata) do |pipeline, queue|
queue.pop
end
expect(event.get("[@metadata][_index]")).to eq('logstash-2014.10.12')
expect(event.get("[@metadata][_type]")).to eq('logs')
expect(event.get("[@metadata][_id]")).to eq('C5b2xLQwTZa76jBmHIbwHQ')
end
it 'should move the document information to the specified field' do
config = %q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
docinfo => true
docinfo_target => 'meta'
}
}
]
event = input(config) do |pipeline, queue|
queue.pop
end
expect(event.get("[meta][_index]")).to eq('logstash-2014.10.12')
expect(event.get("[meta][_type]")).to eq('logs')
expect(event.get("[meta][_id]")).to eq('C5b2xLQwTZa76jBmHIbwHQ')
end
it "should allow to specify which fields from the document info to save to the @metadata field" do
fields = ["_index"]
config = %Q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
docinfo => true
docinfo_fields => #{fields}
}
}]
event = input(config) do |pipeline, queue|
queue.pop
end
expect(event.get("@metadata").keys).to eq(fields)
expect(event.get("[@metadata][_type]")).to eq(nil)
expect(event.get("[@metadata][_index]")).to eq('logstash-2014.10.12')
expect(event.get("[@metadata][_id]")).to eq(nil)
end
end
context "when not defining the docinfo" do
it 'should keep the document information in the root of the event' do
config = %q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
}
}
]
event = input(config) do |pipeline, queue|
queue.pop
end
expect(event.get("[@metadata][_index]")).to eq(nil)
expect(event.get("[@metadata][_type]")).to eq(nil)
expect(event.get("[@metadata][_id]")).to eq(nil)
end
end
context "when query contains script fields but not enumerating script fields" do
it 'should not include the script fields at the root of the event' do
config = %q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "message": "ohayo" } }, "script_fields": { "message_copy": {"script": "doc.message.values"} } }'
}
}
]
event = input(config) do |pipeline, queue|
queue.pop
end
expect(event.get("message_copy")).to eq(nil)
end
end
context "when query contains script fields and enumerating script fields" do
it 'should include the script fields at the root of the event' do
config = %q[
input {
elasticsearch {
hosts => ["localhost"]
query => '{ "query": { "match": { "message": "ohayo" } }, "script_fields": { "message_copy": {"script": "doc.message.values"} } }'
script_fields => ["message_copy"]
}
}
]
event = input(config) do |pipeline, queue|
queue.pop
end
expect(event.get("message_copy")).to eq(["ohayo"])
end
end end
end