Skip to content

Commit

Permalink
[data views] Automatic loading of field lists will no longer error on…
Browse files Browse the repository at this point in the history
… empty field list (#152059)

## Summary

Part of #151670
and follow up to #151788

When a data view is loaded, it automatically loads its field list.
Previously, it would error if the index pattern failed to match an
index. Going forward, this will be treated as a valid empty state -
`allowNoIndices` is being passed to the field_caps requests. When
`allowNoIndices` is set to true, ES will return a valid empty set rather
than a 404 error.


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
mattkime and kibanamachine authored Mar 24, 2023
1 parent 728efb3 commit 9f8b44d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ describe('IndexPatterns', () => {
expect(apiClient.getFieldsForWildcard).toBeCalledTimes(2);
});

test('getFieldsForWildcard called with allowNoIndex set to true as default ', async () => {
const id = '1';
await indexPatterns.get(id);
expect(apiClient.getFieldsForWildcard).toBeCalledWith({
allowNoIndex: true,
indexFilter: undefined,
metaFields: false,
pattern: 'something',
rollupIndex: undefined,
type: undefined,
});
});

test('does cache ad-hoc data views', async () => {
const id = '1';

Expand Down Expand Up @@ -608,9 +621,8 @@ describe('IndexPatterns', () => {
expect(indexPattern.fields.length).toBe(1);
});

test('refreshFields properly includes allowNoIndex', async () => {
test('refreshFields defaults allowNoIndex to true', async () => {
const indexPatternSpec: DataViewSpec = {
allowNoIndex: true,
title: 'test',
};

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,12 @@ export class DataViewsService {
*/
getFieldsForIndexPattern = async (
indexPattern: DataView | DataViewSpec,
options?: GetFieldsOptions
options?: Omit<GetFieldsOptions, 'allowNoIndex'>
) =>
this.getFieldsForWildcard({
type: indexPattern.type,
rollupIndex: indexPattern?.typeMeta?.params?.rollup_index,
allowNoIndex: indexPattern.allowNoIndex,
allowNoIndex: true,
...options,
pattern: indexPattern.title as string,
});
Expand All @@ -530,20 +530,20 @@ export class DataViewsService {
return this.apiClient.getFieldsForWildcard({
type: dataView.type,
rollupIndex: dataView?.typeMeta?.params?.rollup_index,
allowNoIndex: dataView.allowNoIndex,
allowNoIndex: true,
pattern: dataView.getIndexPattern(),
metaFields,
});
};

private getFieldsAndIndicesForWildcard = async (options: GetFieldsOptions) => {
const metaFields = await this.config.get<string[]>(META_FIELDS);
return await this.apiClient.getFieldsForWildcard({
return this.apiClient.getFieldsForWildcard({
pattern: options.pattern,
metaFields,
type: options.type,
rollupIndex: options.rollupIndex,
allowNoIndex: options.allowNoIndex,
allowNoIndex: true,
indexFilter: options.indexFilter,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ export async function getFieldCapabilities(params: FieldCapabilitiesParams) {
indexFilter,
fields,
});
const fieldsFromFieldCapsByName = keyBy(readFieldCapsResponse(esFieldCaps.body), 'name');
const fieldCapsArr = readFieldCapsResponse(esFieldCaps.body);
const fieldsFromFieldCapsByName = keyBy(fieldCapsArr, 'name');

const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName)
// not all meta fields are provided, so remove and manually add
.filter((name) => !fieldsFromFieldCapsByName[name].metadata_field)
.concat(metaFields)
.concat(fieldCapsArr.length ? metaFields : [])
.reduce<{ names: string[]; map: Map<string, string> }>(
(agg, value) => {
// This is intentionally using a Map to be highly optimized with very large indexes AND be safe for user provided data
Expand Down
4 changes: 2 additions & 2 deletions test/functional/apps/discover/group3/_sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await PageObjects.discover.waitUntilSidebarHasLoaded();

expect(await PageObjects.discover.getSidebarAriaDescription()).to.be(
'0 available fields. 0 meta fields.'
'0 available fields. 0 empty fields. 0 meta fields.'
);
await testSubjects.existOrFail(
await testSubjects.missingOrFail(
`${PageObjects.discover.getSidebarSectionSelector('available')}-fetchWarning`
);
await testSubjects.existOrFail(
Expand Down

0 comments on commit 9f8b44d

Please sign in to comment.