mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
# Backport This will backport the following commits from `main` to `8.x`: - [[data.search] Clean up code around ES preference usage (#204076)](https://github.com/elastic/kibana/pull/204076) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Lukas Olson","email":"lukas@elastic.co"},"sourceCommit":{"committedDate":"2025-01-06T22:02:48Z","message":"[data.search] Clean up code around ES preference usage (#204076)\n\n## Summary\r\n\r\nWhile refreshing my memory on how we are using the Elasticsearch\r\n`preference` parameter, I noticed we had a method `getEsPreference` that\r\nwas exported but not used. We then had an additional method\r\n`getPreference` that was being used to actually set the parameter. I\r\nconsolidated these into one place and moved the tests so that we can\r\nutilize it when ES|QL supports this parameter.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"df3665fbf59295dfbd664dfc280658b5232498d9","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Search","release_note:skip","v9.0.0","Team:DataDiscovery","backport:prev-minor"],"title":"[data.search] Clean up code around ES preference usage","number":204076,"url":"https://github.com/elastic/kibana/pull/204076","mergeCommit":{"message":"[data.search] Clean up code around ES preference usage (#204076)\n\n## Summary\r\n\r\nWhile refreshing my memory on how we are using the Elasticsearch\r\n`preference` parameter, I noticed we had a method `getEsPreference` that\r\nwas exported but not used. We then had an additional method\r\n`getPreference` that was being used to actually set the parameter. I\r\nconsolidated these into one place and moved the tests so that we can\r\nutilize it when ES|QL supports this parameter.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"df3665fbf59295dfbd664dfc280658b5232498d9"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204076","number":204076,"mergeCommit":{"message":"[data.search] Clean up code around ES preference usage (#204076)\n\n## Summary\r\n\r\nWhile refreshing my memory on how we are using the Elasticsearch\r\n`preference` parameter, I noticed we had a method `getEsPreference` that\r\nwas exported but not used. We then had an additional method\r\n`getPreference` that was being used to actually set the parameter. I\r\nconsolidated these into one place and moved the tests so that we can\r\nutilize it when ES|QL supports this parameter.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"df3665fbf59295dfbd664dfc280658b5232498d9"}}]}] BACKPORT--> Co-authored-by: Lukas Olson <lukas@elastic.co>
This commit is contained in:
parent
8c93503038
commit
b74ac18290
7 changed files with 49 additions and 98 deletions
|
@ -9,7 +9,7 @@
|
|||
|
||||
import { UI_SETTINGS } from '../../../constants';
|
||||
import { GetConfigFn } from '../../../types';
|
||||
import { getSearchParams, getSearchParamsFromRequest } from './get_search_params';
|
||||
import { getSearchParamsFromRequest, getEsPreference } from './get_search_params';
|
||||
import { createStubDataView } from '@kbn/data-views-plugin/common/data_views/data_view.stub';
|
||||
|
||||
function getConfigStub(config: any = {}): GetConfigFn {
|
||||
|
@ -18,11 +18,11 @@ function getConfigStub(config: any = {}): GetConfigFn {
|
|||
|
||||
describe('getSearchParams', () => {
|
||||
test('includes custom preference', () => {
|
||||
const config = getConfigStub({
|
||||
const getConfig = getConfigStub({
|
||||
[UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE]: 'custom',
|
||||
[UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE]: 'aaa',
|
||||
});
|
||||
const searchParams = getSearchParams(config);
|
||||
const searchParams = getSearchParamsFromRequest({ index: 'abc', body: {} }, { getConfig });
|
||||
expect(searchParams.preference).toBe('aaa');
|
||||
});
|
||||
|
||||
|
@ -94,4 +94,39 @@ describe('getSearchParams', () => {
|
|||
);
|
||||
expect(searchParams).not.toHaveProperty('expand_wildcards', 'all');
|
||||
});
|
||||
|
||||
describe('getEsPreference', () => {
|
||||
const mockConfigGet = jest.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
mockConfigGet.mockClear();
|
||||
});
|
||||
|
||||
test('returns the session ID if set to sessionId', () => {
|
||||
mockConfigGet.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'sessionId';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockConfigGet, 'my_session_id');
|
||||
expect(preference).toBe('my_session_id');
|
||||
});
|
||||
|
||||
test('returns the custom preference if set to custom', () => {
|
||||
mockConfigGet.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'custom';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockConfigGet);
|
||||
expect(preference).toBe('foobar');
|
||||
});
|
||||
|
||||
test('returns undefined if set to none', () => {
|
||||
mockConfigGet.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'none';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockConfigGet);
|
||||
expect(preference).toBe(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -12,20 +12,16 @@ import { UI_SETTINGS } from '../../../constants';
|
|||
import { GetConfigFn } from '../../../types';
|
||||
import type { SearchRequest } from './types';
|
||||
|
||||
const sessionId = Date.now();
|
||||
const defaultSessionId = `${Date.now()}`;
|
||||
|
||||
export function getSearchParams(getConfig: GetConfigFn) {
|
||||
return {
|
||||
preference: getPreference(getConfig),
|
||||
};
|
||||
}
|
||||
|
||||
export function getPreference(getConfig: GetConfigFn) {
|
||||
const setRequestPreference = getConfig(UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE);
|
||||
if (setRequestPreference === 'sessionId') return sessionId;
|
||||
return setRequestPreference === 'custom'
|
||||
? getConfig(UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE)
|
||||
: undefined;
|
||||
export function getEsPreference(
|
||||
getConfigFn: GetConfigFn,
|
||||
sessionId = defaultSessionId
|
||||
): SearchRequest['preference'] {
|
||||
const setPreference = getConfigFn<string>(UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE);
|
||||
if (setPreference === 'sessionId') return sessionId;
|
||||
const customPreference = getConfigFn<string>(UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE);
|
||||
return setPreference === 'custom' ? customPreference : undefined;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
|
@ -36,7 +32,7 @@ export function getSearchParamsFromRequest(
|
|||
dependencies: { getConfig: GetConfigFn }
|
||||
): ISearchRequestParams {
|
||||
const { getConfig } = dependencies;
|
||||
const searchParams = getSearchParams(getConfig);
|
||||
const searchParams = { preference: getEsPreference(getConfig) };
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const { track_total_hits, ...body } = searchRequest.body;
|
||||
const dataView = typeof searchRequest.index !== 'string' ? searchRequest.index : undefined;
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
* License v3.0 only", or the "Server Side Public License, v 1".
|
||||
*/
|
||||
|
||||
export { getSearchParams, getSearchParamsFromRequest, getPreference } from './get_search_params';
|
||||
export { getSearchParamsFromRequest, getEsPreference } from './get_search_params';
|
||||
export { RequestFailure } from './request_error';
|
||||
export * from './types';
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the "Elastic License
|
||||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
||||
* Public License v 1"; you may not use this file except in compliance with, at
|
||||
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
||||
* License v3.0 only", or the "Server Side Public License, v 1".
|
||||
*/
|
||||
|
||||
import type { MockedKeys } from '@kbn/utility-types-jest';
|
||||
import { getEsPreference } from './get_es_preference';
|
||||
import { CoreStart } from '@kbn/core/public';
|
||||
import { coreMock } from '@kbn/core/public/mocks';
|
||||
import { UI_SETTINGS } from '../../../common';
|
||||
|
||||
describe('Get ES preference', () => {
|
||||
let mockCoreStart: MockedKeys<CoreStart>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockCoreStart = coreMock.createStart();
|
||||
});
|
||||
|
||||
test('returns the session ID if set to sessionId', () => {
|
||||
mockCoreStart.uiSettings.get.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'sessionId';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockCoreStart.uiSettings, 'my_session_id');
|
||||
expect(preference).toBe('my_session_id');
|
||||
});
|
||||
|
||||
test('returns the custom preference if set to custom', () => {
|
||||
mockCoreStart.uiSettings.get.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'custom';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockCoreStart.uiSettings);
|
||||
expect(preference).toBe('foobar');
|
||||
});
|
||||
|
||||
test('returns undefined if set to none', () => {
|
||||
mockCoreStart.uiSettings.get.mockImplementation((key: string) => {
|
||||
if (key === UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE) return 'none';
|
||||
if (key === UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE) return 'foobar';
|
||||
});
|
||||
const preference = getEsPreference(mockCoreStart.uiSettings);
|
||||
expect(preference).toBe(undefined);
|
||||
});
|
||||
});
|
|
@ -1,20 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the "Elastic License
|
||||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
||||
* Public License v 1"; you may not use this file except in compliance with, at
|
||||
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
||||
* License v3.0 only", or the "Server Side Public License, v 1".
|
||||
*/
|
||||
|
||||
import { IUiSettingsClient } from '@kbn/core/public';
|
||||
import { UI_SETTINGS } from '../../../common';
|
||||
|
||||
const defaultSessionId = `${Date.now()}`;
|
||||
|
||||
export function getEsPreference(uiSettings: IUiSettingsClient, sessionId = defaultSessionId) {
|
||||
const setPreference = uiSettings.get(UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE);
|
||||
if (setPreference === 'sessionId') return `${sessionId}`;
|
||||
const customPreference = uiSettings.get(UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE);
|
||||
return setPreference === 'custom' ? customPreference : undefined;
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the "Elastic License
|
||||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
||||
* Public License v 1"; you may not use this file except in compliance with, at
|
||||
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
||||
* License v3.0 only", or the "Server Side Public License, v 1".
|
||||
*/
|
||||
|
||||
export { getEsPreference } from './get_es_preference';
|
|
@ -48,7 +48,6 @@ export {
|
|||
SEARCH_SESSIONS_MANAGEMENT_ID,
|
||||
waitUntilNextSessionCompletes$,
|
||||
} from './session';
|
||||
export { getEsPreference } from './es_search';
|
||||
|
||||
export type { SearchInterceptorDeps } from './search_interceptor';
|
||||
export { SearchInterceptor } from './search_interceptor';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue