Skip to content
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: 1 addition & 3 deletions elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ def bulk(arguments = {})
body
end

headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers))
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def msearch(arguments = {})
payload = body
end

headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Utils.update_ndjson_headers!(headers, client.transport.options.dig(:transport_options, :headers))
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
4 changes: 1 addition & 3 deletions elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ def msearch(arguments = {})
payload = body
end

headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers))
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def msearch_template(arguments = {})
payload = body
end

headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Utils.update_ndjson_headers!(headers, transport.options.dig(:transport_options, :headers))
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ def find_structure(arguments = {})
body
end

headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Utils.update_ndjson_headers!(headers, client.transport.options.dig(:transport_options, :headers))
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
15 changes: 15 additions & 0 deletions elasticsearch-api/lib/elasticsearch/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ def rescue_from_not_found(&block)
end
end

# Updates ndjson headers for msearch, bulk, and others
#
def update_ndjson_headers!(headers, client_headers)
current_content = client_headers.keys.find { |c| c.match?(/content-?_?type/i) } || 'content-type'
current_accept = client_headers.keys.find { |c| c.match?(/accept/i) } || 'accept'
version = client_headers[current_content].match(/compatible-with=([0-9]+)/)[1] || 9

headers.merge!(
{
current_content => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}",
current_accept => "application/vnd.elasticsearch+x-ndjson; compatible-with=#{version}"
}
)
end

extend self
end
end
Expand Down
7 changes: 7 additions & 0 deletions elasticsearch-api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def self.included(context)
end
end

Transport ||= Struct.new('Transport', :options)

def dummy_ndjson_headers
allow(client_double).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } })
allow(Elasticsearch::API::Utils).to receive(:update_ndjson_headers!).and_return({})
end

RSpec.configure do |config|
config.include(HelperModule)
config.filter_run_excluding skip: true
Expand Down
41 changes: 26 additions & 15 deletions elasticsearch-api/spec/unit/actions/bulk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
]
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}
let(:headers) { {} }

# This test only cares about the body, there's another test for the ndjson headers.
before do
dummy_ndjson_headers
end

let(:params) { {} }
let(:url) { '_bulk' }
Expand All @@ -53,12 +54,16 @@
end

it 'performs the request' do
expect(client_double.bulk(:body => [
{ :index => { :_index => 'myindexA', :_id => '1', :data => { :title => 'Test' } } },
{ :update => { :_index => 'myindexB', :_id => '2', :data => { :doc => { :title => 'Update' } } } },
{ :delete => { :_index => 'myindexC', :_id => '3' } },
{ :index => { :_index => 'myindexD', :_id => '1', :data => { :data => 'MYDATA' } } },
])).to be_a Elasticsearch::API::Response
expect(
client_double.bulk(
body: [
{ index: { _index: 'myindexA', _id: '1', data: { title: 'Test' } } },
{ update: { _index: 'myindexB', _id: '2', data: { doc: { title: 'Update' } } } },
{ delete: { _index: 'myindexC', _id: '3' } },
{ index: { _index: 'myindexD', _id: '1', data: { data: 'MYDATA' } } }
]
)
).to be_a Elasticsearch::API::Response
end
end

Expand All @@ -72,7 +77,7 @@
params,
body,
headers,
{ defined_params: { index: 'myindex' }, :endpoint=>"bulk"}
{ defined_params: { index: 'myindex' }, endpoint: 'bulk' }
]
end

Expand All @@ -90,8 +95,14 @@
end

it 'performs the request' do
expect(client_double.bulk(body:[ { :update => { :_index => 'myindex', :_id => '1' } },
{ :doc => { :data => { :title => 'Update' } } } ])).to be_a Elasticsearch::API::Response
expect(
client_double.bulk(
body: [
{ update: { _index: 'myindex', _id: '1' } },
{ doc: { data: { title: 'Update' } } }
]
)
).to be_a Elasticsearch::API::Response
end
end

Expand Down Expand Up @@ -137,7 +148,7 @@
params,
body,
headers,
{ defined_params: { index: 'foo^bar' }, :endpoint=>"bulk"}
{ defined_params: { index: 'foo^bar' }, endpoint: 'bulk'}
]
end

Expand Down
21 changes: 5 additions & 16 deletions elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,17 @@
'_fleet/_fleet_msearch',
{},
{},
headers,
{},
{ endpoint: 'fleet.msearch' }
]
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}
# This test only cares about the body, there's another test for the ndjson headers.
before do
dummy_ndjson_headers
end

it 'performs the request' do
expect(client_double.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response
end

let(:client) do
Class.new { include Elasticsearch::API }.new
end

it 'requires the :body argument' do
expect {
client.fleet.msearch
}.to raise_exception(ArgumentError)
end
end
23 changes: 6 additions & 17 deletions elasticsearch-api/spec/unit/actions/msearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,16 @@
{}
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

let(:client) do
Class.new { include Elasticsearch::API }.new
let(:headers) do
{}
end

it 'requires the :body argument' do
expect {
client.msearch
}.to raise_exception(ArgumentError)
# This test only cares about the body, there's another test for the ndjson headers.
before do
dummy_ndjson_headers
end

context 'when the body is an object' do

let(:body) do
<<-PAYLOAD.gsub(/^\s+/, '')
{"index":"foo"}
Expand All @@ -72,15 +64,14 @@

it 'performs the request' do
expect(client_double.msearch body: [
{ index: 'foo', search: { query: { match_all: {} } } },
{ index: 'foo', search: { query: { match_all: {} } } },
{ index: 'bar', search: { query: { match: { foo: 'bar' } } } },
{ search_type: 'count', search: { facets: { tags: {} } } }
])
end
end

context 'when the body is a string' do

let(:body) do
%Q|{"foo":"bar"}\n{"moo":"lam"}|
end
Expand Down Expand Up @@ -141,7 +132,6 @@
end

context 'when the request needs to be URL-escaped' do

let(:url) do
'foo%5Ebar/_msearch'
end
Expand All @@ -167,7 +157,6 @@
end

context 'when the URL params need to be URL-encoded' do

let(:url) do
'_msearch'
end
Expand Down
11 changes: 6 additions & 5 deletions elasticsearch-api/spec/unit/actions/msearch_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
{}
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}
let(:headers) {{}}

# This test only cares about the body, there's another test for the ndjson headers.
before do
dummy_ndjson_headers
end

let(:url) do
'_msearch/template'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@

require 'spec_helper'

describe 'client.text_structure#test_grok_pattern' do
describe 'client.text_structure#find_structure' do
let(:expected_args) do
[
'POST',
'_text_structure/find_structure',
{},
{},
headers,
{},
{ endpoint: 'text_structure.find_structure' }
]
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}
# This test only cares about the body, there's another test for the ndjson headers.
before do
dummy_ndjson_headers
allow(client_double.text_structure.client).to receive(:transport).and_return Transport.new({ transport_options: { headers: {} } })
end

it 'performs the request' do
expect(client_double.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response
Expand Down
Loading