Skip to content

Commit

Permalink
Show information when doc summary cuts fields (elastic#119744)
Browse files Browse the repository at this point in the history
* Show information when doc summary cuts fields

* Fix jest tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and TinLe committed Dec 22, 2021
1 parent b8226b6 commit 9892a43
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jest.mock('../../../../kibana_react/public', () => ({
jest.mock('../../kibana_services', () => ({
getServices: () => ({
uiSettings: {
get: jest.fn(),
get: jest.fn((key) => key === 'discover:maxDocFieldsDisplayed' && 200),
},
fieldFormats: {
getDefaultInstance: jest.fn(() => ({ convert: (value: unknown) => (value ? value : '-') })),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import { formatHit } from '../../../utils/format_hit';
import './row_formatter.scss';

interface Props {
defPairs: Array<[string, string]>;
defPairs: Array<readonly [string, string]>;
}
const TemplateComponent = ({ defPairs }: Props) => {
return (
<dl className={'source dscTruncateByHeight'}>
{defPairs.map((pair, idx) => (
<Fragment key={idx}>
<dt>{pair[0]}:</dt>
<dt>
{pair[0]}
{!!pair[1] && ':'}
</dt>
<dd
className="rowFormatter__value"
// eslint-disable-next-line react/no-danger
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/discover/public/utils/format_hit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('formatHit', () => {
(dataViewMock.getFormatterForField as jest.Mock).mockReturnValue({
convert: (value: unknown) => `formatted:${value}`,
});
(discoverServiceMock.uiSettings.get as jest.Mock).mockImplementation(
(key) => key === MAX_DOC_FIELDS_DISPLAYED && 220
);
});

afterEach(() => {
Expand Down Expand Up @@ -72,6 +75,7 @@ describe('formatHit', () => {
expect(formatted).toEqual([
['extension', 'formatted:png'],
['message', 'formatted:foobar'],
['and 3 more fields', ''],
]);
});

Expand Down
20 changes: 18 additions & 2 deletions src/plugins/discover/public/utils/format_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*/

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { i18n } from '@kbn/i18n';
import { DataView, flattenHit } from '../../../data/common';
import { MAX_DOC_FIELDS_DISPLAYED } from '../../common';
import { getServices } from '../kibana_services';
import { formatFieldValue } from './format_value';

const formattedHitCache = new WeakMap<estypes.SearchHit, FormattedHit>();

type FormattedHit = Array<[fieldName: string, formattedValue: string]>;
type FormattedHit = Array<readonly [fieldName: string, formattedValue: string]>;

/**
* Returns a formatted document in form of key/value pairs of the fields name and a formatted value.
Expand Down Expand Up @@ -61,7 +62,22 @@ export function formatHit(
}
});
const maxEntries = getServices().uiSettings.get<number>(MAX_DOC_FIELDS_DISPLAYED);
const formatted = [...highlightPairs, ...sourcePairs].slice(0, maxEntries);
const pairs = [...highlightPairs, ...sourcePairs];
const formatted =
// If document has more formatted fields than configured via MAX_DOC_FIELDS_DISPLAYED we cut
// off additional fields and instead show a summary how many more field exists.
pairs.length <= maxEntries
? pairs
: [
...pairs.slice(0, maxEntries),
[
i18n.translate('discover.utils.formatHit.moreFields', {
defaultMessage: 'and {count} more {count, plural, one {field} other {fields}}',
values: { count: pairs.length - maxEntries },
}),
'',
] as const,
];
formattedHitCache.set(hit, formatted);
return formatted;
}
2 changes: 1 addition & 1 deletion src/plugins/discover/server/ui_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getUiSettings: () => Record<string, UiSettingsParams> = () => ({
}),
value: 200,
description: i18n.translate('discover.advancedSettings.maxDocFieldsDisplayedText', {
defaultMessage: 'Maximum number of fields rendered in the document column',
defaultMessage: 'Maximum number of fields rendered in the document summary',
}),
category: ['discover'],
schema: schema.number(),
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,6 @@
"discover.advancedSettings.docTableHideTimeColumnTitle": "「時刻」列を非表示",
"discover.advancedSettings.fieldsPopularLimitText": "最も頻繁に使用されるフィールドのトップNを表示します",
"discover.advancedSettings.fieldsPopularLimitTitle": "頻繁に使用されるフィールドの制限",
"discover.advancedSettings.maxDocFieldsDisplayedText": "ドキュメント列でレンダリングされたフィールドの最大数",
"discover.advancedSettings.maxDocFieldsDisplayedTitle": "表示される最大ドキュメントフィールド数",
"discover.advancedSettings.sampleSizeText": "表に表示する行数です",
"discover.advancedSettings.sampleSizeTitle": "行数",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,6 @@
"discover.advancedSettings.docTableHideTimeColumnTitle": "隐藏“时间”列",
"discover.advancedSettings.fieldsPopularLimitText": "要显示的排名前 N 最常见字段",
"discover.advancedSettings.fieldsPopularLimitTitle": "常见字段限制",
"discover.advancedSettings.maxDocFieldsDisplayedText": "在文档列中渲染的最大字段数目",
"discover.advancedSettings.maxDocFieldsDisplayedTitle": "显示的最大文档字段数",
"discover.advancedSettings.sampleSizeText": "要在表中显示的行数目",
"discover.advancedSettings.sampleSizeTitle": "行数目",
Expand Down

0 comments on commit 9892a43

Please sign in to comment.