Skip to content

Commit bb7224e

Browse files
authored
Merge pull request #108 from yaauie/support-total_hits-as-object-in-response
Add support for Elasticsearch 7.x `total_hits` response object
2 parents cd8d64d + 10ccf6a commit bb7224e

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.6.0
2+
- Add support for extracting hits total from Elasticsearch 7.x responses
3+
14
## 3.5.0
25
- Added connection check during register to avoid failures during processing
36
- Changed Elasticsearch Client transport to use Manticore

lib/logstash/filters/elasticsearch.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def filter(event)
9494
results = get_client.search(params)
9595
raise "Elasticsearch query error: #{results["_shards"]["failures"]}" if results["_shards"].include? "failures"
9696

97-
event.set("[@metadata][total_hits]", results['hits']['total'])
97+
event.set("[@metadata][total_hits]", extract_total_from_hits(results['hits']))
9898

9999
resultsHits = results["hits"]["hits"]
100100
if !resultsHits.nil? && !resultsHits.empty?
@@ -173,6 +173,21 @@ def extract_value(source, path)
173173
end
174174
end
175175

176+
# Given a "hits" object from an Elasticsearch response, return the total number of hits in
177+
# the result set.
178+
# @param hits [Hash{String=>Object}]
179+
# @return [Integer]
180+
def extract_total_from_hits(hits)
181+
total = hits['total']
182+
183+
# Elasticsearch 7.x produces an object containing `value` and `relation` in order
184+
# to enable unambiguous reporting when the total is only a lower bound; if we get
185+
# an object back, return its `value`.
186+
return total['value'] if total.kind_of?(Hash)
187+
188+
total
189+
end
190+
176191
def test_connection!
177192
get_client.client.ping
178193
end

logstash-filter-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-filter-elasticsearch'
4-
s.version = '3.5.0'
4+
s.version = '3.6.0'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Copies fields from previous log events in Elasticsearch to current events "
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/filters/elasticsearch_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@
113113
end
114114
end
115115

116+
context 'when Elasticsearch 7.x gives us a totals object instead of an integer' do
117+
let(:config) do
118+
{
119+
"hosts" => ["localhost:9200"],
120+
"query" => "response: 404",
121+
"fields" => { "response" => "code" },
122+
"result_size" => 10
123+
}
124+
end
125+
126+
let(:response) do
127+
LogStash::Json.load(File.read(File.join(File.dirname(__FILE__), "fixtures", "elasticsearch_7.x_hits_total_as_object.json")))
128+
end
129+
130+
it "should enhance the current event with new data" do
131+
plugin.filter(event)
132+
expect(event.get("[@metadata][total_hits]")).to eq(13476)
133+
end
134+
end
135+
116136
context "if something wrong happen during connection" do
117137

118138
before(:each) do
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"took": 49,
3+
"timed_out": false,
4+
"_shards": {
5+
"total": 155,
6+
"successful": 155,
7+
"failed": 0
8+
},
9+
"hits": {
10+
"total": {
11+
"value": 13476,
12+
"relation": "eq"
13+
},
14+
"max_score": 1,
15+
"hits": [{
16+
"_index": "logstash-2014.08.26",
17+
"_type": "logs",
18+
"_id": "AVVY76L_AW7v0kX8KXo4",
19+
"_score": 1,
20+
"_source": {
21+
"request": "/doc/index.html?org/elasticsearch/action/search/SearchResponse.html",
22+
"agent": "\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"",
23+
"geoip": {
24+
"timezone": "America/Los_Angeles",
25+
"ip": "66.249.73.185",
26+
"latitude": 37.386,
27+
"continent_code": "NA",
28+
"city_name": "Mountain View",
29+
"country_code2": "US",
30+
"country_name": "United States",
31+
"dma_code": 807,
32+
"country_code3": "US",
33+
"region_name": "California",
34+
"location": [-122.0838,
35+
37.386
36+
],
37+
"postal_code": "94035",
38+
"longitude": -122.0838,
39+
"region_code": "CA"
40+
},
41+
"auth": "-",
42+
"ident": "-",
43+
"verb": "GET",
44+
"useragent": {
45+
"os": "Other",
46+
"major": "2",
47+
"minor": "1",
48+
"name": "Googlebot",
49+
"os_name": "Other",
50+
"device": "Spider"
51+
},
52+
"message": "66.249.73.185 - - [26/Aug/2014:21:22:13 +0000] \"GET /doc/index.html?org/elasticsearch/action/search/SearchResponse.html HTTP/1.1\" 404 294 \"-\" \"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"",
53+
"referrer": "\"-\"",
54+
"@timestamp": "2014-08-26T21:22:13.000Z",
55+
"response": 404,
56+
"bytes": 294,
57+
"clientip": "66.249.73.185",
58+
"@version": "1",
59+
"host": "skywalker",
60+
"httpversion": "1.1",
61+
"timestamp": "26/Aug/2014:21:22:13 +0000"
62+
}
63+
}]
64+
},
65+
"aggregations": {
66+
"bytes_avg": {
67+
"value": 294
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)