Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport][8.13] Updates helpers #2360

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion elasticsearch/lib/elasticsearch/helpers/bulk_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def initialize(client, index, params = {})
def ingest(docs, params = {}, body = {}, &block)
ingest_docs = docs.map { |doc| { index: { _index: @index, data: doc} } }
if (slice = params.delete(:slice))
ingest_docs.each_slice(slice) { |items| ingest(items, params, &block) }
ingest_docs.each_slice(slice) do |items|
ingest(items.map { |item| item[:index][:data] }, params, &block)
end
else
bulk_request(ingest_docs, params, &block)
end
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch/lib/elasticsearch/helpers/esql_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def self.query(client, query, params = {}, parser: {})
response['values'].map do |value|
(value.length - 1).downto(0).map do |index|
key = columns[index]['name']
value[index] = parser[key].call value[index] if parser[key]
value[index] = parser[key].call(value[index]) if value[index] && parser[key]
{ key => value[index] }
end.reduce({}, :merge)
end
Expand Down
5 changes: 5 additions & 0 deletions elasticsearch/spec/integration/helpers/bulk_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
context 'Elasticsearch client helpers' do
context 'Bulk helper' do
let(:index) { 'bulk_animals' }
let(:index_slice) { 'bulk_animals_slice' }
let(:params) { { refresh: 'wait_for' } }
let(:bulk_helper) { Elasticsearch::Helpers::BulkHelper.new(client, index, params) }
let(:docs) do
Expand All @@ -40,6 +41,7 @@

after do
client.indices.delete(index: index, ignore: 404)
client.indices.delete(index: index_slice, ignore: 404)
end

it 'Ingests documents' do
Expand Down Expand Up @@ -76,10 +78,13 @@

it 'Ingests documents and yields response and docs' do
slice = 2
bulk_helper = Elasticsearch::Helpers::BulkHelper.new(client, index_slice, params)
response = bulk_helper.ingest(docs, {slice: slice}) do |response, docs|
expect(response).to be_an_instance_of Elasticsearch::API::Response
expect(docs.count).to eq slice
end
response = client.search(index: index_slice, size: 200)
expect(response['hits']['hits'].map { |a| a['_source'].transform_keys(&:to_sym) }).to eq docs
end

context 'JSON File helper' do
Expand Down
28 changes: 26 additions & 2 deletions elasticsearch/spec/integration/helpers/esql_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
require_relative 'helpers_spec_helper'
require 'elasticsearch/helpers/esql_helper'
require 'ipaddr'

context 'Elasticsearch client helpers' do
let(:index) { 'esql_helper_test' }
Expand Down Expand Up @@ -76,8 +77,6 @@
end

it 'parses iterated objects when procs are passed in' do
require 'ipaddr'

parser = {
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
'client.ip' => Proc.new { |i| IPAddr.new(i) },
Expand All @@ -91,4 +90,29 @@
expect(r['event.duration']).to be_a String
end
end

it 'parser does not error when value is nil, leaves nil' do
client.index(
index: index,
body: {
'@timestamp' => nil,
'client.ip' => nil,
message: 'Connected to 10.1.0.1',
'event.duration' => 1756465
},
refresh: true
)
parser = {
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
'client.ip' => Proc.new { |i| IPAddr.new(i) },
'event.duration' => Proc.new { |d| d.to_s }
}
response = esql_helper.query(client, query, parser: parser)
response.each do |r|
expect [DateTime, NilClass].include?(r['@timestamp'].class)
expect [IPAddr, NilClass].include?(r['client.ip'].class)
expect(r['message']).to be_a String
expect(r['event.duration']).to be_a String
end
end
end
Loading