Skip to content

Commit c93abd1

Browse files
Only add _type if ES version < 8 (logstash-plugins#892)
* Only add _type if ES version < 8 * Adding tests * Updating docs * Trying to fix retry integration test * Fixing up unit tests * Making method class method * Fix more tests and add doc note * Changelog entry and version bump * Update lib/logstash/outputs/elasticsearch/common.rb Co-Authored-By: Colin Surprenant <[email protected]>
1 parent c059723 commit c93abd1

File tree

9 files changed

+72
-15
lines changed

9 files changed

+72
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 10.2.2
2+
- Fixed 8.x type removal compatibility issue [#892](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/892)
3+
14
## 10.2.1
25
- Fixed wording and corrected option in documentation [#881](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/881) [#883](https://github.com/logstash-plugins/logstash-output-elasticsearch/pull/883)
36

docs/index.asciidoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,18 @@ Elasticsearch with the same ID.
298298
* There is no default value for this setting.
299299
* This option is deprecated
300300

301-
Note: This option is deprecated due to the https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html[removal of types in Elasticsearch 6.0].
301+
NOTE: This option is deprecated due to the https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html[removal of types in Elasticsearch 6.0].
302302
It will be removed in the next major version of Logstash.
303+
304+
NOTE: This value is ignored and has no effect for Elasticsearch clusters `8.x`.
305+
303306
This sets the document type to write events to. Generally you should try to write only
304307
similar events to the same 'type'. String expansion `%{foo}` works here.
305308
If you don't set a value for this option:
306309

307-
- for elasticsearch clusters 6.x and above: the value of 'doc' will be used;
310+
- for elasticsearch clusters 8.x: no value will be used;
311+
- for elasticsearch clusters 7.x: the value of '_doc' will be used;
312+
- for elasticsearch clusters 6.x: the value of 'doc' will be used;
308313
- for elasticsearch clusters 5.x and below: the event's 'type' field will be used, if the field is not present the value of 'doc' will be used.
309314

310315
[id="plugins-{type}s-{plugin}-failure_type_logging_whitelist"]

lib/logstash/outputs/elasticsearch/common.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ def event_action_tuple(event)
7070
params = {
7171
:_id => @document_id ? event.sprintf(@document_id) : nil,
7272
:_index => event.sprintf(@index),
73-
:_type => get_event_type(event),
7473
routing_field_name => @routing ? event.sprintf(@routing) : nil
7574
}
7675

76+
params[:_type] = get_event_type(event) if client.maximum_seen_major_version < 8
77+
7778
if @pipeline
7879
params[:pipeline] = event.sprintf(@pipeline)
7980
end
@@ -276,8 +277,10 @@ def get_event_type(event)
276277
event.get("type") || DEFAULT_EVENT_TYPE_ES6
277278
elsif client.maximum_seen_major_version == 6
278279
DEFAULT_EVENT_TYPE_ES6
279-
else
280+
elsif client.maximum_seen_major_version == 7
280281
DEFAULT_EVENT_TYPE_ES7
282+
else
283+
nil
281284
end
282285
end
283286

logstash-output-elasticsearch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-output-elasticsearch'
3-
s.version = '10.2.1'
3+
s.version = '10.2.2'
44
s.licenses = ['apache-2.0']
55
s.summary = "Stores logs in Elasticsearch"
66
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/es_spec_helper.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ def get_client
2727
end
2828

2929
def doc_type
30-
if ESHelper.es_version_satisfies?(">=7")
30+
if ESHelper.es_version_satisfies?(">=8")
31+
nil
32+
elsif ESHelper.es_version_satisfies?(">=7")
3133
"_doc"
3234
else
3335
"doc"
3436
end
3537
end
3638

39+
def self.action_for_version(action)
40+
action_params = action[1]
41+
if ESHelper.es_version_satisfies?(">=8")
42+
action_params.delete(:_type)
43+
end
44+
action[1] = action_params
45+
action
46+
end
47+
3748
def todays_date
3849
Time.now.strftime("%Y.%m.%d")
3950
end
@@ -82,7 +93,6 @@ def self.es_version
8293
end
8394
end
8495

85-
8696
def self.es_version_satisfies?(*requirement)
8797
es_version = RSpec.configuration.filter[:es_version] || ENV['ES_VERSION'] || ENV['ELASTIC_STACK_VERSION']
8898
if es_version.nil?

spec/integration/outputs/compressed_indexing_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@
5050
response = http_client.get("#{index_url}/_search?q=*&size=1000")
5151
result = LogStash::Json.load(response.body)
5252
result["hits"]["hits"].each do |doc|
53-
if ESHelper.es_version_satisfies?(">= 6")
54-
expect(doc["_type"]).to eq(type)
55-
end
53+
expect(doc["_type"]).to eq(type) if ESHelper.es_version_satisfies?(">= 6", "< 8")
54+
expect(doc).not_to include("_type") if ESHelper.es_version_satisfies?(">= 8")
5655
expect(doc["_index"]).to eq(index)
5756
end
5857
end

spec/integration/outputs/index_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@
8080
response = http_client.get("#{index_url}/_search?q=*&size=1000")
8181
result = LogStash::Json.load(response.body)
8282
result["hits"]["hits"].each do |doc|
83-
if ESHelper.es_version_satisfies?(">= 6")
84-
expect(doc["_type"]).to eq(type)
85-
end
83+
expect(doc["_type"]).to eq(type) if ESHelper.es_version_satisfies?(">= 6", "< 8")
84+
expect(doc).not_to include("_type") if ESHelper.es_version_satisfies?(">= 8")
8685
expect(doc["_index"]).to eq(index)
8786
end
8887
end

spec/integration/outputs/retry_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
describe "failures in bulk class expected behavior", :integration => true do
55
let(:template) { '{"template" : "not important, will be updated by :index"}' }
66
let(:event1) { LogStash::Event.new("somevalue" => 100, "@timestamp" => "2014-11-17T20:37:17.223Z", "@metadata" => {"retry_count" => 0}) }
7-
let(:action1) { ["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event1] }
7+
let(:action1) { ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event1]) }
88
let(:event2) { LogStash::Event.new("geoip" => { "location" => [ 0.0, 0.0] }, "@timestamp" => "2014-11-17T20:37:17.223Z", "@metadata" => {"retry_count" => 0}) }
9-
let(:action2) { ["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event2] }
9+
let(:action2) { ESHelper.action_for_version(["index", {:_id=>nil, routing_field_name =>nil, :_index=>"logstash-2014.11.17", :_type=> doc_type }, event2]) }
1010
let(:invalid_event) { LogStash::Event.new("geoip" => { "location" => "notlatlon" }, "@timestamp" => "2014-11-17T20:37:17.223Z") }
1111

1212
def mock_actions_with_response(*resp)

spec/unit/outputs/elasticsearch_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,44 @@
7676
end
7777
end
7878

79+
describe "building an event action tuple" do
80+
context "for 7.x elasticsearch clusters" do
81+
let(:maximum_seen_major_version) { 7 }
82+
it "should include '_type'" do
83+
action_tuple = subject.send(:event_action_tuple, LogStash::Event.new("type" => "foo"))
84+
action_params = action_tuple[1]
85+
expect(action_params).to include(:_type => "_doc")
86+
end
87+
88+
context "with 'document type set'" do
89+
let(:options) { super.merge("document_type" => "bar")}
90+
it "should get the event type from the 'document_type' setting" do
91+
action_tuple = subject.send(:event_action_tuple, LogStash::Event.new("type" => "foo"))
92+
action_params = action_tuple[1]
93+
expect(action_params).to include(:_type => "bar")
94+
end
95+
end
96+
end
97+
98+
context "for 8.x elasticsearch clusters" do
99+
let(:maximum_seen_major_version) { 8 }
100+
it "should not include '_type'" do
101+
action_tuple = subject.send(:event_action_tuple, LogStash::Event.new("type" => "foo"))
102+
action_params = action_tuple[1]
103+
expect(action_params).not_to include(:_type)
104+
end
105+
106+
context "with 'document type set'" do
107+
let(:options) { super.merge("document_type" => "bar")}
108+
it "should not include '_type'" do
109+
action_tuple = subject.send(:event_action_tuple, LogStash::Event.new("type" => "foo"))
110+
action_params = action_tuple[1]
111+
expect(action_params).not_to include(:_type)
112+
end
113+
end
114+
end
115+
end
116+
79117
describe "with auth" do
80118
let(:user) { "myuser" }
81119
let(:password) { ::LogStash::Util::Password.new("mypassword") }

0 commit comments

Comments
 (0)