logstash-output-elasticsearch 7.4.3-java → 8.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +5 -18
  3. data/docs/index.asciidoc +13 -50
  4. data/lib/logstash/outputs/elasticsearch/common.rb +39 -43
  5. data/lib/logstash/outputs/elasticsearch/common_configs.rb +2 -11
  6. data/lib/logstash/outputs/elasticsearch/http_client.rb +22 -27
  7. data/lib/logstash/outputs/elasticsearch/http_client/manticore_adapter.rb +2 -2
  8. data/lib/logstash/outputs/elasticsearch/http_client/pool.rb +12 -31
  9. data/lib/logstash/outputs/elasticsearch/template_manager.rb +6 -4
  10. data/logstash-output-elasticsearch.gemspec +1 -1
  11. data/spec/es_spec_helper.rb +0 -6
  12. data/spec/integration/outputs/compressed_indexing_spec.rb +44 -46
  13. data/spec/integration/outputs/delete_spec.rb +49 -51
  14. data/spec/integration/outputs/groovy_update_spec.rb +129 -131
  15. data/spec/integration/outputs/index_version_spec.rb +81 -82
  16. data/spec/integration/outputs/ingest_pipeline_spec.rb +49 -51
  17. data/spec/integration/outputs/painless_update_spec.rb +130 -170
  18. data/spec/integration/outputs/parent_spec.rb +55 -149
  19. data/spec/integration/outputs/sniffer_spec.rb +2 -5
  20. data/spec/integration/outputs/templates_5x_spec.rb +82 -81
  21. data/spec/integration/outputs/templates_spec.rb +81 -81
  22. data/spec/integration/outputs/update_spec.rb +99 -101
  23. data/spec/unit/outputs/elasticsearch/http_client/manticore_adapter_spec.rb +5 -30
  24. data/spec/unit/outputs/elasticsearch/http_client/pool_spec.rb +0 -3
  25. data/spec/unit/outputs/elasticsearch/http_client_spec.rb +12 -11
  26. data/spec/unit/outputs/elasticsearch/template_manager_spec.rb +25 -13
  27. data/spec/unit/outputs/elasticsearch_spec.rb +1 -10
  28. metadata +4 -6
@@ -1,101 +1,100 @@
1
1
  require_relative "../../../spec/es_spec_helper"
2
2
  require "logstash/outputs/elasticsearch"
3
3
 
4
- if ESHelper.es_version_satisfies?(">= 2")
5
- describe "Versioned indexing", :integration => true do
6
- require "logstash/outputs/elasticsearch"
7
4
 
8
- let(:es) { get_client }
5
+ describe "Versioned indexing", :integration => true, :version_greater_than_equal_to_2x => true do
6
+ require "logstash/outputs/elasticsearch"
9
7
 
10
- before :each do
11
- # Delete all templates first.
12
- # Clean ES of data before we start.
13
- es.indices.delete_template(:name => "*")
14
- # This can fail if there are no indexes, ignore failure.
15
- es.indices.delete(:index => "*") rescue nil
16
- es.indices.refresh
17
- end
8
+ let(:es) { get_client }
18
9
 
19
- context "when index only" do
20
- subject { LogStash::Outputs::ElasticSearch.new(settings) }
10
+ before :each do
11
+ # Delete all templates first.
12
+ # Clean ES of data before we start.
13
+ es.indices.delete_template(:name => "*")
14
+ # This can fail if there are no indexes, ignore failure.
15
+ es.indices.delete(:index => "*") rescue nil
16
+ es.indices.refresh
17
+ end
21
18
 
22
- before do
23
- subject.register
24
- end
19
+ context "when index only" do
20
+ subject { LogStash::Outputs::ElasticSearch.new(settings) }
25
21
 
26
- describe "unversioned output" do
27
- let(:settings) do
28
- {
29
- "manage_template" => true,
30
- "index" => "logstash-index",
31
- "template_overwrite" => true,
32
- "hosts" => get_host_port(),
33
- "action" => "index",
34
- "script_lang" => "groovy",
35
- "document_id" => "%{my_id}"
36
- }
37
- end
22
+ before do
23
+ subject.register
24
+ end
38
25
 
39
- it "should default to ES version" do
40
- subject.multi_receive([LogStash::Event.new("my_id" => "123", "message" => "foo")])
41
- r = es.get(:index => 'logstash-index', :type => 'logs', :id => "123", :refresh => true)
42
- expect(r["_version"]).to eq(1)
43
- expect(r["_source"]["message"]).to eq('foo')
44
- subject.multi_receive([LogStash::Event.new("my_id" => "123", "message" => "foobar")])
45
- r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => "123", :refresh => true)
46
- expect(r2["_version"]).to eq(2)
47
- expect(r2["_source"]["message"]).to eq('foobar')
48
- end
26
+ describe "unversioned output" do
27
+ let(:settings) do
28
+ {
29
+ "manage_template" => true,
30
+ "index" => "logstash-index",
31
+ "template_overwrite" => true,
32
+ "hosts" => get_host_port(),
33
+ "action" => "index",
34
+ "script_lang" => "groovy",
35
+ "document_id" => "%{my_id}"
36
+ }
49
37
  end
50
38
 
51
- describe "versioned output" do
52
- let(:settings) do
53
- {
54
- "manage_template" => true,
55
- "index" => "logstash-index",
56
- "template_overwrite" => true,
57
- "hosts" => get_host_port(),
58
- "action" => "index",
59
- "script_lang" => "groovy",
60
- "document_id" => "%{my_id}",
61
- "version" => "%{my_version}",
62
- "version_type" => "external",
63
- }
64
- end
39
+ it "should default to ES version" do
40
+ subject.multi_receive([LogStash::Event.new("my_id" => "123", "message" => "foo")])
41
+ r = es.get(:index => 'logstash-index', :type => 'logs', :id => "123", :refresh => true)
42
+ expect(r["_version"]).to eq(1)
43
+ expect(r["_source"]["message"]).to eq('foo')
44
+ subject.multi_receive([LogStash::Event.new("my_id" => "123", "message" => "foobar")])
45
+ r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => "123", :refresh => true)
46
+ expect(r2["_version"]).to eq(2)
47
+ expect(r2["_source"]["message"]).to eq('foobar')
48
+ end
49
+ end
65
50
 
66
- it "should respect the external version" do
67
- id = "ev1"
68
- subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
69
- r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
70
- expect(r["_version"]).to eq(99)
71
- expect(r["_source"]["message"]).to eq('foo')
72
- end
51
+ describe "versioned output" do
52
+ let(:settings) do
53
+ {
54
+ "manage_template" => true,
55
+ "index" => "logstash-index",
56
+ "template_overwrite" => true,
57
+ "hosts" => get_host_port(),
58
+ "action" => "index",
59
+ "script_lang" => "groovy",
60
+ "document_id" => "%{my_id}",
61
+ "version" => "%{my_version}",
62
+ "version_type" => "external",
63
+ }
64
+ end
65
+
66
+ it "should respect the external version" do
67
+ id = "ev1"
68
+ subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
69
+ r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
70
+ expect(r["_version"]).to eq(99)
71
+ expect(r["_source"]["message"]).to eq('foo')
72
+ end
73
73
 
74
- it "should ignore non-monotonic external version updates" do
75
- id = "ev2"
76
- subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
77
- r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
78
- expect(r["_version"]).to eq(99)
79
- expect(r["_source"]["message"]).to eq('foo')
74
+ it "should ignore non-monotonic external version updates" do
75
+ id = "ev2"
76
+ subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
77
+ r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
78
+ expect(r["_version"]).to eq(99)
79
+ expect(r["_source"]["message"]).to eq('foo')
80
80
 
81
- subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "98", "message" => "foo")])
82
- r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
83
- expect(r2["_version"]).to eq(99)
84
- expect(r2["_source"]["message"]).to eq('foo')
85
- end
81
+ subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "98", "message" => "foo")])
82
+ r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
83
+ expect(r2["_version"]).to eq(99)
84
+ expect(r2["_source"]["message"]).to eq('foo')
85
+ end
86
86
 
87
- it "should commit monotonic external version updates" do
88
- id = "ev3"
89
- subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
90
- r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
91
- expect(r["_version"]).to eq(99)
92
- expect(r["_source"]["message"]).to eq('foo')
87
+ it "should commit monotonic external version updates" do
88
+ id = "ev3"
89
+ subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "99", "message" => "foo")])
90
+ r = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
91
+ expect(r["_version"]).to eq(99)
92
+ expect(r["_source"]["message"]).to eq('foo')
93
93
 
94
- subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "100", "message" => "foo")])
95
- r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
96
- expect(r2["_version"]).to eq(100)
97
- expect(r2["_source"]["message"]).to eq('foo')
98
- end
94
+ subject.multi_receive([LogStash::Event.new("my_id" => id, "my_version" => "100", "message" => "foo")])
95
+ r2 = es.get(:index => 'logstash-index', :type => 'logs', :id => id, :refresh => true)
96
+ expect(r2["_version"]).to eq(100)
97
+ expect(r2["_source"]["message"]).to eq('foo')
99
98
  end
100
99
  end
101
100
  end
@@ -1,19 +1,18 @@
1
1
  require_relative "../../../spec/es_spec_helper"
2
2
 
3
- if ESHelper.es_version_satisfies?(">= 5")
4
- describe "Ingest pipeline execution behavior", :integration => true do
5
- subject! do
6
- require "logstash/outputs/elasticsearch"
7
- settings = {
8
- "hosts" => "#{get_host_port()}",
9
- "pipeline" => "apache-logs"
10
- }
11
- next LogStash::Outputs::ElasticSearch.new(settings)
12
- end
3
+ describe "Ingest pipeline execution behavior", :integration => true, :version_greater_than_equal_to_5x => true do
4
+ subject! do
5
+ require "logstash/outputs/elasticsearch"
6
+ settings = {
7
+ "hosts" => "#{get_host_port()}",
8
+ "pipeline" => "apache-logs"
9
+ }
10
+ next LogStash::Outputs::ElasticSearch.new(settings)
11
+ end
13
12
 
14
- let(:ftw_client) { FTW::Agent.new }
15
- let(:ingest_url) { "http://#{get_host_port()}/_ingest/pipeline/apache-logs" }
16
- let(:apache_logs_pipeline) { '
13
+ let(:ftw_client) { FTW::Agent.new }
14
+ let(:ingest_url) { "http://#{get_host_port()}/_ingest/pipeline/apache-logs" }
15
+ let(:apache_logs_pipeline) { '
17
16
  {
18
17
  "description" : "Pipeline to parse Apache logs",
19
18
  "processors" : [
@@ -25,53 +24,52 @@ if ESHelper.es_version_satisfies?(">= 5")
25
24
  }
26
25
  ]
27
26
  }'
28
- }
27
+ }
29
28
 
30
- before :each do
31
- # Delete all templates first.
32
- require "elasticsearch"
29
+ before :each do
30
+ # Delete all templates first.
31
+ require "elasticsearch"
33
32
 
34
- # Clean ES of data before we start.
35
- @es = get_client
36
- @es.indices.delete_template(:name => "*")
33
+ # Clean ES of data before we start.
34
+ @es = get_client
35
+ @es.indices.delete_template(:name => "*")
37
36
 
38
- # This can fail if there are no indexes, ignore failure.
39
- @es.indices.delete(:index => "*") rescue nil
37
+ # This can fail if there are no indexes, ignore failure.
38
+ @es.indices.delete(:index => "*") rescue nil
40
39
 
41
- # delete existing ingest pipeline
42
- req = ftw_client.delete(ingest_url)
43
- ftw_client.execute(req)
40
+ # delete existing ingest pipeline
41
+ req = ftw_client.delete(ingest_url)
42
+ ftw_client.execute(req)
44
43
 
45
- # register pipeline
46
- req = ftw_client.put(ingest_url, :body => apache_logs_pipeline)
47
- req.headers["Content-Type"] = "application/json"
48
- ftw_client.execute(req)
44
+ # register pipeline
45
+ req = ftw_client.put(ingest_url, :body => apache_logs_pipeline)
46
+ req.headers["Content-Type"] = "application/json"
47
+ ftw_client.execute(req)
49
48
 
50
- #TODO: Use esclient
51
- #@es.ingest.put_pipeline :id => 'apache_pipeline', :body => pipeline_defintion
49
+ #TODO: Use esclient
50
+ #@es.ingest.put_pipeline :id => 'apache_pipeline', :body => pipeline_defintion
52
51
 
53
- subject.register
54
- subject.multi_receive([LogStash::Event.new("message" => '183.60.215.50 - - [01/Jun/2015:18:00:00 +0000] "GET /scripts/netcat-webserver HTTP/1.1" 200 182 "-" "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"')])
55
- @es.indices.refresh
52
+ subject.register
53
+ subject.multi_receive([LogStash::Event.new("message" => '183.60.215.50 - - [01/Jun/2015:18:00:00 +0000] "GET /scripts/netcat-webserver HTTP/1.1" 200 182 "-" "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"')])
54
+ @es.indices.refresh
56
55
 
57
- #Wait or fail until everything's indexed.
58
- Stud::try(20.times) do
59
- r = @es.search
60
- insist { r["hits"]["total"] } == 1
61
- end
56
+ #Wait or fail until everything's indexed.
57
+ Stud::try(20.times) do
58
+ r = @es.search
59
+ insist { r["hits"]["total"] } == 1
62
60
  end
61
+ end
63
62
 
64
- it "indexes using the proper pipeline" do
65
- results = @es.search(:index => 'logstash-*', :q => "message:\"netcat\"")
66
- insist { results["hits"]["total"] } == 1
67
- insist { results["hits"]["hits"][0]["_source"]["response"] } == "200"
68
- insist { results["hits"]["hits"][0]["_source"]["bytes"] } == "182"
69
- insist { results["hits"]["hits"][0]["_source"]["verb"] } == "GET"
70
- insist { results["hits"]["hits"][0]["_source"]["request"] } == "/scripts/netcat-webserver"
71
- insist { results["hits"]["hits"][0]["_source"]["auth"] } == "-"
72
- insist { results["hits"]["hits"][0]["_source"]["ident"] } == "-"
73
- insist { results["hits"]["hits"][0]["_source"]["clientip"] } == "183.60.215.50"
74
- insist { results["hits"]["hits"][0]["_source"]["junkfieldaaaa"] } == nil
75
- end
63
+ it "indexes using the proper pipeline" do
64
+ results = @es.search(:index => 'logstash-*', :q => "message:\"netcat\"")
65
+ insist { results["hits"]["total"] } == 1
66
+ insist { results["hits"]["hits"][0]["_source"]["response"] } == "200"
67
+ insist { results["hits"]["hits"][0]["_source"]["bytes"] } == "182"
68
+ insist { results["hits"]["hits"][0]["_source"]["verb"] } == "GET"
69
+ insist { results["hits"]["hits"][0]["_source"]["request"] } == "/scripts/netcat-webserver"
70
+ insist { results["hits"]["hits"][0]["_source"]["auth"] } == "-"
71
+ insist { results["hits"]["hits"][0]["_source"]["ident"] } == "-"
72
+ insist { results["hits"]["hits"][0]["_source"]["clientip"] } == "183.60.215.50"
73
+ insist { results["hits"]["hits"][0]["_source"]["junkfieldaaaa"] } == nil
76
74
  end
77
75
  end
@@ -1,188 +1,148 @@
1
1
  require_relative "../../../spec/es_spec_helper"
2
2
 
3
- if ESHelper.es_version_satisfies?(">= 5")
4
- describe "Update actions using painless scripts", :integration => true, :update_tests => 'painless' do
5
- require "logstash/outputs/elasticsearch"
3
+ describe "Update actions using painless scripts", :integration => true, :update_tests => 'painless', :version_greater_than_equal_to_5x => true do
4
+ require "logstash/outputs/elasticsearch"
5
+
6
+ def get_es_output( options={} )
7
+ settings = {
8
+ "manage_template" => true,
9
+ "index" => "logstash-update",
10
+ "template_overwrite" => true,
11
+ "hosts" => get_host_port(),
12
+ "action" => "update",
13
+ "script_lang" => "painless"
14
+ }
15
+ LogStash::Outputs::ElasticSearch.new(settings.merge!(options))
16
+ end
6
17
 
7
- def get_es_output( options={} )
8
- settings = {
9
- "manage_template" => true,
10
- "index" => "logstash-update",
11
- "template_overwrite" => true,
12
- "hosts" => get_host_port(),
13
- "action" => "update"
14
- }
15
- if ESHelper.es_version_satisfies?('<6')
16
- settings.merge!({"script_lang" => "painless"})
17
- end
18
- LogStash::Outputs::ElasticSearch.new(settings.merge!(options))
19
- end
18
+ before :each do
19
+ @es = get_client
20
+ # Delete all templates first.
21
+ # Clean ES of data before we start.
22
+ @es.indices.delete_template(:name => "*")
23
+ # This can fail if there are no indexes, ignore failure.
24
+ @es.indices.delete(:index => "*") rescue nil
25
+ @es.index(
26
+ :index => 'logstash-update',
27
+ :type => 'logs',
28
+ :id => "123",
29
+ :body => { :message => 'Test', :counter => 1 }
30
+ )
31
+ @es.indices.refresh
32
+ end
20
33
 
21
- before :each do
22
- @es = get_client
23
- # Delete all templates first.
24
- # Clean ES of data before we start.
25
- @es.indices.delete_template(:name => "*")
26
- # This can fail if there are no indexes, ignore failure.
27
- @es.indices.delete(:index => "*") rescue nil
28
- @es.index(
29
- :index => 'logstash-update',
30
- :type => 'logs',
31
- :id => "123",
32
- :body => { :message => 'Test', :counter => 1 }
33
- )
34
- @es.indices.refresh
34
+ context "scripted updates" do
35
+ it "should increment a counter with event/doc 'count' variable" do
36
+ subject = get_es_output({ 'document_id' => "123", 'script' => 'scripted_update', 'script_type' => 'file' })
37
+ subject.register
38
+ subject.multi_receive([LogStash::Event.new("count" => 2)])
39
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
40
+ insist { r["_source"]["counter"] } == 3
35
41
  end
36
42
 
37
- context "scripted updates" do
38
- if ESHelper.es_version_satisfies?('<6')
39
- context 'with file based scripts' do
40
- it "should increment a counter with event/doc 'count' variable" do
41
- subject = get_es_output({ 'document_id' => "123", 'script' => 'scripted_update', 'script_type' => 'file' })
42
- subject.register
43
- subject.multi_receive([LogStash::Event.new("count" => 2)])
44
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
45
- insist { r["_source"]["counter"] } == 3
46
- end
47
-
48
- it "should increment a counter with event/doc '[data][count]' nested variable" do
49
- subject = get_es_output({ 'document_id' => "123", 'script' => 'scripted_update_nested', 'script_type' => 'file' })
50
- subject.register
51
- subject.multi_receive([LogStash::Event.new("data" => { "count" => 3 })])
52
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
53
- insist { r["_source"]["counter"] } == 4
54
- end
55
- end
56
- end
57
-
58
- it "should increment a counter with event/doc 'count' variable with inline script" do
59
- subject = get_es_output({
60
- 'document_id' => "123",
61
- 'script' => 'ctx._source.counter += params.event.counter',
62
- 'script_type' => 'inline'
63
- })
64
- subject.register
65
- subject.multi_receive([LogStash::Event.new("counter" => 3 )])
66
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
67
- insist { r["_source"]["counter"] } == 4
68
- end
69
-
70
- it "should increment a counter with event/doc 'count' variable with event/doc as upsert and inline script" do
71
- subject = get_es_output({
72
- 'document_id' => "123",
73
- 'doc_as_upsert' => true,
74
- 'script' => 'if( ctx._source.containsKey("counter") ){ ctx._source.counter += params.event.counter; } else { ctx._source.counter = params.event.counter; }',
75
- 'script_type' => 'inline'
76
- })
77
- subject.register
78
- subject.multi_receive([LogStash::Event.new("counter" => 3 )])
79
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
80
- insist { r["_source"]["counter"] } == 4
81
- end
82
-
83
- it "should, with new doc, set a counter with event/doc 'count' variable with event/doc as upsert and inline script" do
84
- subject = get_es_output({
85
- 'document_id' => "456",
86
- 'doc_as_upsert' => true,
87
- 'script' => 'if( ctx._source.containsKey("counter") ){ ctx._source.counter += params.event.counter; } else { ctx._source.counter = params.event.counter; }',
88
- 'script_type' => 'inline'
89
- })
90
- subject.register
91
- subject.multi_receive([LogStash::Event.new("counter" => 3 )])
92
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
93
- insist { r["_source"]["counter"] } == 3
94
- end
95
-
96
- context 'with an indexed script' do
97
- it "should increment a counter with event/doc 'count' variable with indexed script" do
98
- if ESHelper.es_version_satisfies?('<6')
99
- @es.perform_request(:put, "_scripts/painless/indexed_update", {}, {"script" => "ctx._source.counter += params.event.count" })
100
- else
101
- @es.perform_request(:put, "_scripts/indexed_update", {}, {"script" => {"source" => "ctx._source.counter += params.event.count", "lang" => "painless"}})
102
- end
103
-
104
- plugin_parameters = {
105
- 'document_id' => "123",
106
- 'script' => 'indexed_update',
107
- 'script_type' => 'indexed'
108
- }
109
-
110
- if ESHelper.es_version_satisfies?('>= 6.0.0')
111
- plugin_parameters.merge!('script_lang' => '')
112
- end
43
+ it "should increment a counter with event/doc '[data][count]' nested variable" do
44
+ subject = get_es_output({ 'document_id' => "123", 'script' => 'scripted_update_nested', 'script_type' => 'file' })
45
+ subject.register
46
+ subject.multi_receive([LogStash::Event.new("data" => { "count" => 3 })])
47
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
48
+ insist { r["_source"]["counter"] } == 4
49
+ end
113
50
 
114
- subject = get_es_output(plugin_parameters)
115
- subject.register
116
- subject.multi_receive([LogStash::Event.new("count" => 4 )])
117
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
118
- insist { r["_source"]["counter"] } == 5
119
- end
120
- end
121
- end
51
+ it "should increment a counter with event/doc 'count' variable with inline script" do
52
+ subject = get_es_output({
53
+ 'document_id' => "123",
54
+ 'script' => 'ctx._source.counter += params.event.counter',
55
+ 'script_lang' => 'painless',
56
+ 'script_type' => 'inline'
57
+ })
58
+ subject.register
59
+ subject.multi_receive([LogStash::Event.new("counter" => 3 )])
60
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
61
+ insist { r["_source"]["counter"] } == 4
62
+ end
122
63
 
123
- context "when update with upsert" do
124
- it "should create new documents with provided upsert" do
125
- subject = get_es_output({ 'document_id' => "456", 'upsert' => '{"message": "upsert message"}' })
126
- subject.register
127
- subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
128
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
129
- insist { r["_source"]["message"] } == 'upsert message'
130
- end
64
+ it "should increment a counter with event/doc 'count' variable with event/doc as upsert and inline script" do
65
+ subject = get_es_output({
66
+ 'document_id' => "123",
67
+ 'doc_as_upsert' => true,
68
+ 'script' => 'if( ctx._source.containsKey("counter") ){ ctx._source.counter += params.event.counter; } else { ctx._source.counter = params.event.counter; }',
69
+ 'script_lang' => 'painless',
70
+ 'script_type' => 'inline'
71
+ })
72
+ subject.register
73
+ subject.multi_receive([LogStash::Event.new("counter" => 3 )])
74
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
75
+ insist { r["_source"]["counter"] } == 4
76
+ end
131
77
 
132
- it "should create new documents with event/doc as upsert" do
133
- subject = get_es_output({ 'document_id' => "456", 'doc_as_upsert' => true })
134
- subject.register
135
- subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
136
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
137
- insist { r["_source"]["message"] } == 'sample message here'
138
- end
78
+ it "should, with new doc, set a counter with event/doc 'count' variable with event/doc as upsert and inline script" do
79
+ subject = get_es_output({
80
+ 'document_id' => "456",
81
+ 'doc_as_upsert' => true,
82
+ 'script' => 'if( ctx._source.containsKey("counter") ){ ctx._source.counter += params.event.counter; } else { ctx._source.counter = params.event.counter; }',
83
+ 'script_lang' => 'painless',
84
+ 'script_type' => 'inline'
85
+ })
86
+ subject.register
87
+ subject.multi_receive([LogStash::Event.new("counter" => 3 )])
88
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
89
+ insist { r["_source"]["counter"] } == 3
90
+ end
139
91
 
140
- it "should fail on documents with event/doc as upsert at external version" do
141
- subject = get_es_output({ 'document_id' => "456", 'doc_as_upsert' => true, 'version' => 999, "version_type" => "external" })
142
- expect { subject.register }.to raise_error(LogStash::ConfigurationError)
143
- end
92
+ it "should increment a counter with event/doc 'count' variable with indexed script" do
93
+ @es.put_script lang: 'painless', id: 'indexed_update', body: { script: 'ctx._source.counter += params.event.count' }
94
+ subject = get_es_output({
95
+ 'document_id' => "123",
96
+ 'script' => 'indexed_update',
97
+ 'script_lang' => 'painless',
98
+ 'script_type' => 'indexed'
99
+ })
100
+ subject.register
101
+ subject.multi_receive([LogStash::Event.new("count" => 4 )])
102
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "123", :refresh => true)
103
+ insist { r["_source"]["counter"] } == 5
104
+ end
105
+ end
106
+
107
+ context "when update with upsert" do
108
+ it "should create new documents with provided upsert" do
109
+ subject = get_es_output({ 'document_id' => "456", 'upsert' => '{"message": "upsert message"}' })
110
+ subject.register
111
+ subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
112
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
113
+ insist { r["_source"]["message"] } == 'upsert message'
144
114
  end
145
115
 
146
- context "updates with scripted upsert" do
147
- if ESHelper.es_version_satisfies?('<6')
148
- context 'with file based scripts' do
149
- it "should create new documents with upsert content" do
150
- subject = get_es_output({ 'document_id' => "456", 'script' => 'scripted_update', 'upsert' => '{"message": "upsert message"}', 'script_type' => 'file' })
151
- subject.register
152
- subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
153
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
154
- insist { r["_source"]["message"] } == 'upsert message'
155
- end
116
+ it "should create new documents with event/doc as upsert" do
117
+ subject = get_es_output({ 'document_id' => "456", 'doc_as_upsert' => true })
118
+ subject.register
119
+ subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
120
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
121
+ insist { r["_source"]["message"] } == 'sample message here'
122
+ end
156
123
 
157
- it "should create new documents with event/doc as script params" do
158
- subject = get_es_output({ 'document_id' => "456", 'script' => 'scripted_upsert', 'scripted_upsert' => true, 'script_type' => 'file' })
159
- subject.register
160
- subject.multi_receive([LogStash::Event.new("counter" => 1)])
161
- @es.indices.refresh
162
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
163
- insist { r["_source"]["counter"] } == 1
164
- end
165
- end
166
- end
124
+ it "should fail on documents with event/doc as upsert at external version" do
125
+ subject = get_es_output({ 'document_id' => "456", 'doc_as_upsert' => true, 'version' => 999, "version_type" => "external" })
126
+ expect { subject.register }.to raise_error(LogStash::ConfigurationError)
127
+ end
128
+ end
167
129
 
168
- context 'with an inline script' do
169
- it "should create new documents with upsert content" do
170
- subject = get_es_output({ 'document_id' => "456", 'script' => 'ctx._source.counter = params.event.counter', 'upsert' => '{"message": "upsert message"}', 'script_type' => 'inline' })
171
- subject.register
172
- subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
173
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
174
- insist { r["_source"]["message"] } == 'upsert message'
175
- end
130
+ context "updates with scripted upsert" do
131
+ it "should create new documents with upsert content" do
132
+ subject = get_es_output({ 'document_id' => "456", 'script' => 'scripted_update', 'upsert' => '{"message": "upsert message"}', 'script_type' => 'file' })
133
+ subject.register
134
+ subject.multi_receive([LogStash::Event.new("message" => "sample message here")])
135
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
136
+ insist { r["_source"]["message"] } == 'upsert message'
137
+ end
176
138
 
177
- it "should create new documents with event/doc as script params" do
178
- subject = get_es_output({ 'document_id' => "456", 'script' => 'ctx._source.counter = params.event.counter', 'scripted_upsert' => true, 'script_type' => 'inline' })
179
- subject.register
180
- subject.multi_receive([LogStash::Event.new("counter" => 1)])
181
- @es.indices.refresh
182
- r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
183
- insist { r["_source"]["counter"] } == 1
184
- end
185
- end
139
+ it "should create new documents with event/doc as script params" do
140
+ subject = get_es_output({ 'document_id' => "456", 'script' => 'scripted_upsert', 'scripted_upsert' => true, 'script_type' => 'file' })
141
+ subject.register
142
+ subject.multi_receive([LogStash::Event.new("counter" => 1)])
143
+ @es.indices.refresh
144
+ r = @es.get(:index => 'logstash-update', :type => 'logs', :id => "456", :refresh => true)
145
+ insist { r["_source"]["counter"] } == 1
186
146
  end
187
147
  end
188
148
  end