Skip to content

Commit

Permalink
[Search service] Add support for ES request preference (elastic#49424)
Browse files Browse the repository at this point in the history
* Add support for ES preference

* Fix name of test
  • Loading branch information
lukasolson committed Nov 13, 2019
1 parent 81d1f59 commit 384b037
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/plugins/data/public/search/es_search/es_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@
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> = (
context: ISearchContext,
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>;
},
};
};
46 changes: 46 additions & 0 deletions src/plugins/data/public/search/es_search/get_es_preference.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
29 changes: 29 additions & 0 deletions src/plugins/data/public/search/es_search/get_es_preference.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 384b037

Please sign in to comment.