Skip to content

Commit

Permalink
advanced setting to control search request preference (#17271) (#17447)
Browse files Browse the repository at this point in the history
* advanced setting to control search request preference

* add header tests

* add sentince about caching to description

* change courier:setRequestPreference to list and add courier:customRequestPreference

* update setting text
  • Loading branch information
nreese committed Mar 28, 2018
1 parent 2bfa9ed commit 79e89d2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/core_plugins/kibana/ui_setting_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ export function getUiSettingDefaults() {
'When set to true, filter(s) will be ignored for a visualization ' +
'when the visualization\'s index does not contain the filtering field.'
},
'courier:setRequestPreference': {
value: 'sessionId',
options: ['sessionId', 'custom', 'none'],
type: 'select',
description: 'Allows you to set which shards handle your search requests. ' +
'<ul>' +
'<li><strong>sessionId:</strong> restricts operations to execute all search requests on the same shards. ' +
'This has the benefit of reusing shard caches across requests. ' +
'<li><strong>custom:</strong> allows you to define a your own preference. ' +
'Use <strong>courier:customRequestPreference</strong> to customize your preference value. ' +
'<li><strong>none:</strong> means do not set a preference. ' +
'This might provide better performance because requests can be spread across all shard copies. ' +
'However, results might be inconsistent because different shards might be in different refresh states.' +
'</ul>'
},
'courier:customRequestPreference': {
value: '_local',
type: 'string',
description: '<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html" target="_blank" rel="noopener noreferrer">Request Preference</a> ' +
' used when <strong>courier:setRequestPreference</strong> is set to "custom".'
},
'fields:popularLimit': {
value: 10,
description: 'The top N most popular fields to show',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function requestFetchParamsToBody(
Promise,
timeFilter,
kbnIndex,
sessionId) {
sessionId,
config) {
const indexToListMapping = {};
const timeBounds = timeFilter.getActiveBounds();
const promises = requestsFetchParams.map(function (fetchParams) {
Expand Down Expand Up @@ -68,15 +69,19 @@ export function requestFetchParamsToBody(
index = indexList;
}

return JSON.stringify({
const header = {
index,
type: fetchParams.type,
search_type: fetchParams.search_type,
ignore_unavailable: true,
preference: sessionId,
})
+ '\n'
+ toJson(body, JSON.stringify);
};
if (config.get('courier:setRequestPreference') === 'sessionId') {
header.preference = sessionId;
} else if (config.get('courier:setRequestPreference') === 'custom') {
header.preference = config.get('courier:customRequestPreference');
}

return `${JSON.stringify(header)}\n${toJson(body, JSON.stringify)}`;
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { requestFetchParamsToBody } from './request_fetch_params_to_body';
import _ from 'lodash';

const DEFAULT_SESSION_ID = '1';

function requestFetchParamsToBodyWithDefaults(paramOverrides) {
const paramDefaults = {
requestFetchParams: [],
Expand All @@ -9,7 +11,12 @@ function requestFetchParamsToBodyWithDefaults(paramOverrides) {
getActiveBounds: () => undefined,
},
kbnIndex: '.kibana',
sessionId: '1',
sessionId: DEFAULT_SESSION_ID,
config: {
get: () => {
return 'sessionId';
}
}
};
const params = { ...paramDefaults, ...paramOverrides };

Expand All @@ -19,6 +26,7 @@ function requestFetchParamsToBodyWithDefaults(paramOverrides) {
params.timeFilter,
params.kbnIndex,
params.sessionId,
params.config,
);
}

Expand Down Expand Up @@ -73,10 +81,68 @@ describe('when indexList is empty', () => {
}
];

it('queries the kibana index (.kibana) with a must_not match_all boolean', () => {
test('queries the kibana index (.kibana) with a must_not match_all boolean', () => {
return requestFetchParamsToBodyWithDefaults({ requestFetchParams }).then(value => {
expect(_.includes(value, '"index":[".kibana"]')).toBe(true);
expect(_.includes(value, emptyMustNotQuery)).toBe(true);
});
});
});

describe('headers', () => {

const requestFetchParams = [
{
index: ['logstash-123'],
type: 'blah',
search_type: 'blah2',
body: { foo: 'bar' }
}
];

const getHeader = async (paramOverrides) => {
const request = await requestFetchParamsToBodyWithDefaults(paramOverrides);
const requestParts = request.split('\n');
if (requestParts.length < 2) {
throw new Error('fetch Body does not contain expected format header newline body.');
}
return JSON.parse(requestParts[0]);
};

describe('search request preference', async () => {
test('should be set to sessionId when courier:setRequestPreference is "sessionId"', async () => {
const config = {
get: () => {
return 'sessionId';
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(DEFAULT_SESSION_ID);
});

test('should be set to custom string when courier:setRequestPreference is "custom"', async () => {
const CUSTOM_PREFERENCE = '_local';
const config = {
get: (key) => {
if (key === 'courier:setRequestPreference') {
return 'custom';
} else if (key === 'courier:customRequestPreference') {
return CUSTOM_PREFERENCE;
}
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(CUSTOM_PREFERENCE);
});

test('should not be set when courier:setRequestPreference is "none"', async () => {
const config = {
get: () => {
return 'none';
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(undefined);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { requestFetchParamsToBody } from './request_fetch_params_to_body';

export function RequestFetchParamsToBodyProvider(Promise, timefilter, kbnIndex, sessionId) {
export function RequestFetchParamsToBodyProvider(Promise, timefilter, kbnIndex, sessionId, config) {
return (requestsFetchParams) => (
requestFetchParamsToBody(
requestsFetchParams,
Promise,
timefilter,
kbnIndex,
sessionId)
sessionId,
config)
);
}

0 comments on commit 79e89d2

Please sign in to comment.