Skip to content

Commit 97b50de

Browse files
committed
enable use of metadata fields in add_field decorations
By performing event decorations after the metadata field has been populated we enable the supported `add_field` decoration to reference metadata fields. Resolves logstash-plugins#76 Fixes logstash-plugins#78
1 parent 936e925 commit 97b50de

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.1.0
2+
- Enable use of docinfo (@metadata) fields in `add_field` decorations
3+
14
## 4.0.6
25
- Docs: Fix link syntax
36

lib/logstash/inputs/elasticsearch.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def process_next_scroll(output_queue, scroll_id)
170170

171171
def push_hit(hit, output_queue)
172172
event = LogStash::Event.new(hit['_source'])
173-
decorate(event)
174173

175174
if @docinfo
176175
# do not assume event[@docinfo_target] to be in-place updatable. first get it, update it, then at the end set it in the event.
@@ -190,6 +189,8 @@ def push_hit(hit, output_queue)
190189
event.set(@docinfo_target, docinfo_target)
191190
end
192191

192+
decorate(event)
193+
193194
output_queue << event
194195
end
195196

logstash-input-elasticsearch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-input-elasticsearch'
4-
s.version = '4.0.6'
4+
s.version = '4.1.0'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Read from an Elasticsearch cluster, based on search query results"
77
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

spec/inputs/elasticsearch_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@
228228
expect(event.get("[@metadata][_index]")).to eq('logstash-2014.10.12')
229229
expect(event.get("[@metadata][_id]")).to eq(nil)
230230
end
231+
232+
it 'should be able to reference metadata fields in `add_field` decorations' do
233+
config = %q[
234+
input {
235+
elasticsearch {
236+
hosts => ["localhost"]
237+
query => '{ "query": { "match": { "city_name": "Okinawa" } }, "fields": ["message"] }'
238+
docinfo => true
239+
add_field => {
240+
'identifier' => "foo:%{[@metadata][_type]}:%{[@metadata][_id]}"
241+
}
242+
}
243+
}
244+
]
245+
246+
event = input(config) do |pipeline, queue|
247+
queue.pop
248+
end
249+
250+
expect(event.get('identifier')).to eq('foo:logs:C5b2xLQwTZa76jBmHIbwHQ')
251+
end
231252
end
232253

233254
context "when not defining the docinfo" do

0 commit comments

Comments
 (0)