Skip to content

Commit

Permalink
Don't send flag if specifying fields and _source
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Jan 24, 2022
1 parent 069e50d commit 3c8b2a7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const enhancedEsSearchStrategyProvider = (
...(await getDefaultAsyncSubmitParams(
uiSettingsClient,
searchSessionsClient.getConfig(),
request.params,
options
)),
...request.params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ describe('request utils', () => {
const mockConfig = getMockSearchSessionsConfig({
defaultExpiration: moment.duration(3, 'd'),
});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{}
);
expect(params).toHaveProperty('keep_alive', '1m');
});

Expand All @@ -67,9 +72,14 @@ describe('request utils', () => {
const mockConfig = getMockSearchSessionsConfig({
defaultExpiration: moment.duration(3, 'd'),
});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {
sessionId: 'foo',
});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{
sessionId: 'foo',
}
);
expect(params).toHaveProperty('keep_alive', '259200000ms');
});

Expand All @@ -81,9 +91,14 @@ describe('request utils', () => {
defaultExpiration: moment.duration(3, 'd'),
enabled: false,
});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {
sessionId: 'foo',
});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{
sessionId: 'foo',
}
);
expect(params).toHaveProperty('keep_alive', '1m');
});

Expand All @@ -92,9 +107,14 @@ describe('request utils', () => {
[UI_SETTINGS.SEARCH_INCLUDE_FROZEN]: false,
});
const mockConfig = getMockSearchSessionsConfig({});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {
sessionId: 'foo',
});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{
sessionId: 'foo',
}
);
expect(params).toHaveProperty('keep_on_completion', true);
});

Expand All @@ -106,23 +126,50 @@ describe('request utils', () => {
defaultExpiration: moment.duration(3, 'd'),
enabled: false,
});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {
sessionId: 'foo',
});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{
sessionId: 'foo',
}
);
expect(params).toHaveProperty('keep_on_completion', false);
});

test('Sends `enable_fields_emulation` for BWC with CCS', async () => {
test('Sends `enable_fields_emulation: true` for BWC with CCS if not specifying both fields and _source', async () => {
const mockUiSettingsClient = getMockUiSettingsClient({
[UI_SETTINGS.SEARCH_INCLUDE_FROZEN]: false,
});
const mockConfig = getMockSearchSessionsConfig({
defaultExpiration: moment.duration(3, 'd'),
enabled: false,
});
const params = await getDefaultAsyncSubmitParams(mockUiSettingsClient, mockConfig, {});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: {} },
{}
);
expect(params).toHaveProperty('enable_fields_emulation', true);
});

test('Sends `enable_fields_emulation: false` if specifying both fields and _source', async () => {
const mockUiSettingsClient = getMockUiSettingsClient({
[UI_SETTINGS.SEARCH_INCLUDE_FROZEN]: false,
});
const mockConfig = getMockSearchSessionsConfig({
defaultExpiration: moment.duration(3, 'd'),
enabled: false,
});
const params = await getDefaultAsyncSubmitParams(
mockUiSettingsClient,
mockConfig,
{ body: { fields: ['foo'], _source: { excludes: ['bar'] } } },
{}
);
expect(params).toHaveProperty('enable_fields_emulation', false);
});
});

describe('getDefaultAsyncGetParams', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function getIgnoreThrottled(
export async function getDefaultAsyncSubmitParams(
uiSettingsClient: Pick<IUiSettingsClient, 'get'>,
searchSessionsConfig: SearchSessionsConfigSchema | null,
params: any,
options: ISearchOptions
): Promise<
Pick<
Expand All @@ -56,6 +57,13 @@ export async function getDefaultAsyncSubmitParams(
? `${searchSessionsConfig!.defaultExpiration.asMilliseconds()}ms`
: '1m';

// Specifying specific fields from both "_source" and "fields' while emulating the fields API will throw errors in ES
// See https://github.com/elastic/elasticsearch/pull/75745
const hasFields = Array.isArray(params.body.fields) && params.body.fields.length > 0;
const hasSourceFields =
params.body.hasOwnProperty('_source') && typeof params.body._source !== 'boolean';
const enableFieldsEmulation = !(hasFields && hasSourceFields);

return {
// TODO: adjust for partial results
batched_reduce_size: 64,
Expand All @@ -68,7 +76,7 @@ export async function getDefaultAsyncSubmitParams(
...(await getIgnoreThrottled(uiSettingsClient)),
...(await getDefaultSearchParams(uiSettingsClient)),
// If search sessions are used, set the initial expiration time.
enable_fields_emulation: true, // See https://github.com/elastic/elasticsearch/pull/75745
enable_fields_emulation: enableFieldsEmulation,
};
}

Expand Down

0 comments on commit 3c8b2a7

Please sign in to comment.