diff --git a/src/core_plugins/timelion/server/series_functions/__tests__/es.js b/src/core_plugins/timelion/server/series_functions/__tests__/es.js index 9802a1abb10d0..e74c3cfde43b4 100644 --- a/src/core_plugins/timelion/server/series_functions/__tests__/es.js +++ b/src/core_plugins/timelion/server/series_functions/__tests__/es.js @@ -173,7 +173,6 @@ describe(filename, () => { }; }); - it('sets the index on the request', () => { config.index = 'beer'; const request = fn(config, tlConfig, emptyScriptedFields); @@ -181,13 +180,6 @@ describe(filename, () => { expect(request.index).to.equal('beer'); }); - it('sets the timeout on the request', () => { - config.index = 'beer'; - const request = fn(config, tlConfig, emptyScriptedFields); - - expect(request.timeout).to.equal('30000ms'); - }); - it('always sets body.size to 0', () => { const request = fn(config, tlConfig, emptyScriptedFields); @@ -205,6 +197,34 @@ describe(filename, () => { expect(filters.bar.query_string.query).to.eql('bar'); }); + describe('timeouts', () => { + + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it('sets the timeout on the request', () => { + config.index = 'beer'; + const request = fn(config, tlConfig, emptyScriptedFields); + + expect(request.timeout).to.equal('30000ms'); + }); + + it('sets no timeout if elasticsearch.shardTimeout is set to 0', () => { + sandbox.stub(tlConfig.server.config(), 'get').withArgs('elasticsearch.shardTimeout').returns(0); + config.index = 'beer'; + const request = fn(config, tlConfig, emptyScriptedFields); + + expect(request).to.not.have.property('timeout'); + }); + }); + describe('query body', () => { beforeEach(() => { tlConfig = _.merge(tlConfigFn(), { diff --git a/src/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js b/src/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js index a33c8845dd58a..0ad145f68ce4f 100644 --- a/src/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js +++ b/src/core_plugins/timelion/server/series_functions/__tests__/fixtures/tlConfig.js @@ -24,6 +24,17 @@ import esResponse from './es_response'; export default function () { + const config = { + get(key) { + switch (key) { + case 'elasticsearch.shardTimeout': + return 30000; + default: + throw new Error(`unexpected config ${key}`); + } + } + }; + const functions = require('../../../lib/load_functions')('series_functions'); const server = { plugins: { @@ -41,18 +52,7 @@ export default function () { }) } }, - config: () => { - return { - get: (key) => { - switch (key) { - case 'elasticsearch.shardTimeout': - return 30000; - default: - throw new Error(`unexpected config ${key}`); - } - } - }; - } + config: () => config, }; const tlConfig = require('../../../handlers/lib/tl_config.js')({ diff --git a/src/core_plugins/timelion/server/series_functions/es/lib/build_request.js b/src/core_plugins/timelion/server/series_functions/es/lib/build_request.js index a7ff47697d639..24427b1bc27b1 100644 --- a/src/core_plugins/timelion/server/series_functions/es/lib/build_request.js +++ b/src/core_plugins/timelion/server/series_functions/es/lib/build_request.js @@ -66,9 +66,8 @@ export default function buildRequest(config, tlConfig, scriptedFields) { _.assign(aggCursor, createDateAgg(config, tlConfig, scriptedFields)); - return { + const request = { index: config.index, - timeout: tlConfig.server.config().get('elasticsearch.shardTimeout') + 'ms', body: { query: { bool: bool @@ -77,4 +76,11 @@ export default function buildRequest(config, tlConfig, scriptedFields) { size: 0 } }; + + const timeout = tlConfig.server.config().get('elasticsearch.shardTimeout'); + if (timeout) { + request.timeout = `${timeout}ms`; + } + + return request; }