Skip to content

Commit

Permalink
Deprecate DataView.flattenHit in favor of data plugin flattenHit (ela…
Browse files Browse the repository at this point in the history
…stic#114517) (elastic#114991)

* WIP replacing indexPattern.flattenHit by tabify

* Fix jest tests

* Read metaFields from index pattern

* Remove old test code

* remove unnecessary changes

* Remove flattenHitWrapper APIs

* Fix imports

* Fix missing metaFields

* Add all meta fields to allowlist

* Improve inline comments

* Move flattenHit test to new implementation

* Add deprecation comment to implementation

Co-authored-by: Kibana Machine <[email protected]>
# Conflicts:
#	src/plugins/data_views/public/index.ts
  • Loading branch information
Tim Roes authored Oct 14, 2021
1 parent 8932ccd commit df8c41a
Show file tree
Hide file tree
Showing 25 changed files with 292 additions and 326 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/plugins/data/common/search/tabify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* Side Public License, v 1.
*/

export { tabifyDocs } from './tabify_docs';
export { tabifyDocs, flattenHit } from './tabify_docs';
export { tabifyAggResponse } from './tabify';
export { tabifyGetColumns } from './get_columns';
170 changes: 118 additions & 52 deletions src/plugins/data/common/search/tabify/tabify_docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,137 @@
* Side Public License, v 1.
*/

import { tabifyDocs } from './tabify_docs';
import { IndexPattern } from '../..';
import { tabifyDocs, flattenHit } from './tabify_docs';
import { IndexPattern, DataView } from '../..';
import type { estypes } from '@elastic/elasticsearch';

describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
};
import { fieldFormatsMock } from '../../../../field_formats/common/mocks';
import { stubbedSavedObjectIndexPattern } from '../../../../data_views/common/data_view.stub';

const index = new IndexPattern({
class MockFieldFormatter {}

fieldFormatsMock.getInstance = jest.fn().mockImplementation(() => new MockFieldFormatter()) as any;

// helper function to create index patterns
function create(id: string) {
const {
type,
version,
attributes: { timeFieldName, fields, title },
} = stubbedSavedObjectIndexPattern(id);

return new DataView({
spec: {
id: 'test-index',
fields: {
sourceTest: { name: 'sourceTest', type: 'number', searchable: true, aggregatable: true },
fieldTest: { name: 'fieldTest', type: 'number', searchable: true, aggregatable: true },
'nested.field': {
name: 'nested.field',
type: 'number',
searchable: true,
aggregatable: true,
},
},
id,
type,
version,
timeFieldName,
fields: JSON.parse(fields),
title,
runtimeFieldMap: {},
},
fieldFormats: fieldFormats as any,
fieldFormats: fieldFormatsMock,
shortDotsEnable: false,
metaFields: ['_id', '_type', '_score', '_routing'],
});
}

// @ts-expect-error not full inteface
const response = {
hits: {
hits: [
describe('tabify_docs', () => {
describe('flattenHit', () => {
let indexPattern: DataView;

// create an indexPattern instance for each test
beforeEach(() => {
indexPattern = create('test-pattern');
});

it('returns sorted object keys that combine _source, fields and metaFields in a defined order', () => {
const response = flattenHit(
{
_id: 'hit-id-value',
_index: 'hit-index-value',
_type: 'hit-type-value',
_score: 77,
_source: { sourceTest: 123 },
fields: { fieldTest: 123, invalidMapping: 345, nested: [{ field: 123 }] },
_index: 'foobar',
_id: 'a',
_source: {
name: 'first',
},
fields: {
date: ['1'],
zzz: ['z'],
_abc: ['a'],
},
},
],
},
} as estypes.SearchResponse<unknown>;

it('converts fields by default', () => {
const table = tabifyDocs(response, index);
expect(table).toMatchSnapshot();
indexPattern
);
const expectedOrder = ['_abc', 'date', 'name', 'zzz', '_id', '_routing', '_score', '_type'];
expect(Object.keys(response)).toEqual(expectedOrder);
expect(Object.entries(response).map(([key]) => key)).toEqual(expectedOrder);
});
});

it('converts source if option is set', () => {
const table = tabifyDocs(response, index, { source: true });
expect(table).toMatchSnapshot();
});
describe('tabifyDocs', () => {
const fieldFormats = {
getInstance: (id: string) => ({ toJSON: () => ({ id }) }),
getDefaultInstance: (id: string) => ({ toJSON: () => ({ id }) }),
};

it('skips nested fields if option is set', () => {
const table = tabifyDocs(response, index, { shallow: true });
expect(table).toMatchSnapshot();
});
const index = new IndexPattern({
spec: {
id: 'test-index',
fields: {
sourceTest: { name: 'sourceTest', type: 'number', searchable: true, aggregatable: true },
fieldTest: { name: 'fieldTest', type: 'number', searchable: true, aggregatable: true },
'nested.field': {
name: 'nested.field',
type: 'number',
searchable: true,
aggregatable: true,
},
},
},
metaFields: ['_id', '_index', '_score', '_type'],
fieldFormats: fieldFormats as any,
});

it('combines meta fields if meta option is set', () => {
const table = tabifyDocs(response, index, { meta: true });
expect(table).toMatchSnapshot();
});
// @ts-expect-error not full inteface
const response = {
hits: {
hits: [
{
_id: 'hit-id-value',
_index: 'hit-index-value',
_type: 'hit-type-value',
_score: 77,
_source: { sourceTest: 123 },
fields: { fieldTest: 123, invalidMapping: 345, nested: [{ field: 123 }] },
},
],
},
} as estypes.SearchResponse<unknown>;

it('converts fields by default', () => {
const table = tabifyDocs(response, index);
expect(table).toMatchSnapshot();
});

it('converts source if option is set', () => {
const table = tabifyDocs(response, index, { source: true });
expect(table).toMatchSnapshot();
});

it('skips nested fields if option is set', () => {
const table = tabifyDocs(response, index, { shallow: true });
expect(table).toMatchSnapshot();
});

it('combines meta fields from index pattern', () => {
const table = tabifyDocs(response, index);
expect(table.columns.map((col) => col.id)).toEqual(
expect.arrayContaining(['_id', '_index', '_score', '_type'])
);
});

it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot();
it('works without provided index pattern', () => {
const table = tabifyDocs(response);
expect(table).toMatchSnapshot();
});
});
});
Loading

0 comments on commit df8c41a

Please sign in to comment.