[Search service] Add support for ES request preference (#49424)

* Add support for ES preference

* Fix name of test
This commit is contained in:
Lukas Olson 2019-11-13 13:18:04 -07:00 committed by GitHub
parent cf435b83ea
commit 013f3ebe87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 3 deletions

View file

@ -20,6 +20,7 @@
import { Observable } from 'rxjs';
import { ES_SEARCH_STRATEGY, IEsSearchResponse } from '../../../common/search';
import { SYNC_SEARCH_STRATEGY } from '../sync_search_strategy';
import { getEsPreference } from './get_es_preference';
import { TSearchStrategyProvider, ISearchStrategy, ISearchGeneric, ISearchContext } from '..';
export const esSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_STRATEGY> = (
@ -27,11 +28,17 @@ export const esSearchStrategyProvider: TSearchStrategyProvider<typeof ES_SEARCH_
search: ISearchGeneric
): ISearchStrategy<typeof ES_SEARCH_STRATEGY> => {
return {
search: (request, options) =>
search(
search: (request, options) => {
if (typeof request.params.preference === 'undefined') {
const setPreference = context.core.uiSettings.get('courier:setRequestPreference');
const customPreference = context.core.uiSettings.get('courier:customRequestPreference');
request.params.preference = getEsPreference(setPreference, customPreference);
}
return search(
{ ...request, serverStrategy: ES_SEARCH_STRATEGY },
options,
SYNC_SEARCH_STRATEGY
) as Observable<IEsSearchResponse>,
) as Observable<IEsSearchResponse>;
},
};
};

View file

@ -0,0 +1,46 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { getEsPreference } from './get_es_preference';
jest.useFakeTimers();
describe('Get ES preference', () => {
test('returns the session ID if set to sessionId', () => {
const setPreference = 'sessionId';
const customPreference = 'foobar';
const sessionId = 'my_session_id';
const preference = getEsPreference(setPreference, customPreference, sessionId);
expect(preference).toBe(sessionId);
});
test('returns the custom preference if set to custom', () => {
const setPreference = 'custom';
const customPreference = 'foobar';
const preference = getEsPreference(setPreference, customPreference);
expect(preference).toBe(customPreference);
});
test('returns undefined if set to none', () => {
const setPreference = 'none';
const customPreference = 'foobar';
const preference = getEsPreference(setPreference, customPreference);
expect(preference).toBe(undefined);
});
});

View file

@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const defaultSessionId = `${Date.now()}`;
export function getEsPreference(
setRequestPreference: string,
customRequestPreference?: string,
sessionId: string = defaultSessionId
) {
if (setRequestPreference === 'sessionId') return `${sessionId}`;
return setRequestPreference === 'custom' ? customRequestPreference : undefined;
}