Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Use fields API to retrieve fields #83891

Merged
merged 101 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
a232550
Add search source to example plugin.
lukeelmers Oct 27, 2020
ba796e0
Add uiSetting for fields API.
lukeelmers Oct 27, 2020
9fbcc82
Update SearchSource to support fields API.
lukeelmers Oct 27, 2020
5afd264
[PoC] reading from the fields API in Discover
Nov 20, 2020
ac1e273
Add N fields as a default column
Nov 20, 2020
e6f2a22
Make fields column non-removeable
Nov 20, 2020
c420e96
Merge master
Nov 25, 2020
8ad5f99
Do not add 'fields' to state
Nov 25, 2020
64f0868
Remove fields from app state and read from source when needed
Nov 26, 2020
b843d33
Remove fields column if a new column is added
Nov 30, 2020
1a3196d
Add search source to example plugin.
lukeelmers Oct 27, 2020
0cf84e4
Add uiSetting for fields API.
lukeelmers Oct 27, 2020
ad23b7f
Update SearchSource to support fields API.
lukeelmers Oct 27, 2020
3681311
Improve error handling in search examples plugin.
lukeelmers Nov 16, 2020
5ac33bb
Add unit tests for legacy behavior.
lukeelmers Nov 20, 2020
9e6c89f
Remove uiSettings feature flag; add fieldsFromSource config.
lukeelmers Nov 20, 2020
d34ab66
Rewrite flatten() based on final API design.
lukeelmers Nov 20, 2020
4773949
Update example app based on final API design.
lukeelmers Nov 23, 2020
d272609
Update maps app to use legacy fieldsFromSource.
lukeelmers Nov 23, 2020
30c7c5f
Update Discover to use legacy fieldsFromSource.
lukeelmers Nov 23, 2020
24a5f9d
Rename source filters to field filters.
lukeelmers Nov 24, 2020
9c7a967
Address feedback.
lukeelmers Nov 30, 2020
5c124f8
Update generated docs.
lukeelmers Nov 30, 2020
79e59a8
Update maps functional test.
lukeelmers Nov 30, 2020
f7d304b
Formatting fields column similar to _source
Nov 30, 2020
4053bf1
Merge upstream
Dec 1, 2020
ee33143
Moving logic for using search API to updating search source
Dec 1, 2020
e3ace69
Fix small merge error
Dec 1, 2020
84136f6
Merge branch 'master' into feat/ssource-fields
kibanamachine Dec 1, 2020
804c5f8
Merge branch 'feat/ssource-fields' of https://github.com/lukeelmers/k…
Dec 2, 2020
29fe624
Move useSource switch to Discover section of advanced settings
Dec 2, 2020
900637b
Do not use fields and source at the same time
Dec 2, 2020
9fc9517
Remove unmapped fields switch
Dec 2, 2020
92253eb
Add basic support for grouping multifields
Dec 3, 2020
00d6c68
Merge master
Dec 3, 2020
5e6162c
Remove output.txt
Dec 3, 2020
e16cdcb
Fix some merge leftovers
Dec 3, 2020
5e0f998
Fix some merge leftovers
Dec 3, 2020
9791498
Merge branch 'master' into discover-ssource-fields
Dec 3, 2020
63e49d4
Fix merge errors
Dec 3, 2020
a80d44d
Fix typescript errors and update nested fields logic
Dec 4, 2020
60119b9
Add a unit test
Dec 4, 2020
8583723
Fixing field formats
Dec 4, 2020
e21e478
Merge master
Dec 4, 2020
5108654
Fix multifield selection logic
Dec 5, 2020
448bc7a
Request all fields from source
Dec 6, 2020
dc5f148
Fix eslint
Dec 6, 2020
f260376
Fix default columns when switching between _source and fields
Dec 6, 2020
a2273f7
Merge branch 'master' into discover-ssource-fields
Dec 6, 2020
c8c4f47
More unit tests
Dec 6, 2020
57319da
Update API changes
Dec 7, 2020
d07b676
Add unit test for discover field details footer
Dec 7, 2020
50f57ef
Remove unused file
Dec 7, 2020
6990015
Remove fields formatting from index pattern
Dec 8, 2020
0965f30
Remove unnecessary check
Dec 8, 2020
ab4edb4
Addressing design comments
Dec 8, 2020
be9b9f4
Merge branch 'master' into discover-ssource-fields
Dec 8, 2020
1a6c620
Fixing fields column display and renaming it to Document
Dec 9, 2020
95ea135
Adding more unit tests
Dec 9, 2020
fc12250
Adding a missing check for useNewFieldsAPI; minor fixes
Dec 10, 2020
eb309ee
Fixing typescript error
Dec 10, 2020
b7a79d6
Remove unnecessary console statement
Dec 10, 2020
2b63157
Add missing prop
Dec 10, 2020
0f65832
Merge branch 'master' into discover-ssource-fields
kibanamachine Dec 10, 2020
fad95e7
Fixing import order
Dec 11, 2020
d3d38ef
Merge branch 'master' into discover-ssource-fields
kibanamachine Dec 14, 2020
e374d6f
Adding functional test to test fields API
Dec 14, 2020
5efe636
[Functional test] Clean up in after
Dec 14, 2020
24ab2ae
Fixing context app
Dec 15, 2020
88e81f0
Addressing PR comments
Dec 16, 2020
711c757
Merge branch 'master' into discover-ssource-fields
Dec 16, 2020
f03e800
Updating failed snapshot
Dec 16, 2020
0d150e6
Addressing PR comments
Dec 18, 2020
06112da
Fixing i18n translations, updating type
Dec 18, 2020
5eb9917
Addressing PR comments
Dec 18, 2020
b5bfbc0
Merge branch 'master' into discover-ssource-fields
Dec 18, 2020
3ac1ec3
Resolving merge conflicts
Dec 18, 2020
72fd6d5
Updating a functional test
Dec 19, 2020
1e85041
Add a separate functional test for fields API
Dec 19, 2020
1e1d313
Merge branch 'master' into discover-ssource-fields
kibanamachine Dec 19, 2020
88e07b4
Merge branch 'master' into discover-ssource-fields
kibanamachine Dec 21, 2020
e893b93
Read fields from source in a functional test
Dec 21, 2020
b917a39
Skip buggy test
Dec 21, 2020
67606c2
Use default behavior in functional tests
Dec 21, 2020
6ea034e
Fixing remaining failing tests
Dec 21, 2020
5083d44
Fixing date-nanos test
Dec 22, 2020
b1a7158
Updating FLS test
Dec 22, 2020
a16bebd
Merge master
Dec 23, 2020
756d240
Fixing yet another functional test
Dec 23, 2020
64a57df
Skipping non-relevant tests
Dec 24, 2020
f677240
Fixing more tests
Dec 24, 2020
ca8e798
Merge master
Jan 4, 2021
36251a8
Merge branch 'master' into discover-ssource-fields
kibanamachine Jan 11, 2021
8424d71
Merge branch 'master' into discover-ssource-fields
kibanamachine Jan 13, 2021
1caa777
Merge branch 'master' into discover-ssource-fields
Jan 13, 2021
290dfdf
Update stub import in test
Jan 14, 2021
27d77d6
Merge branch 'discover-ssource-fields' of https://github.com/majagrub…
Jan 14, 2021
02c37cf
Fix import
Jan 14, 2021
0eec11a
Fix invalid import
Jan 14, 2021
f1bd9e7
Merge branch 'master' into discover-ssource-fields
kibanamachine Jan 14, 2021
a37612a
Merge branch 'master' into discover-ssource-fields
Jan 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
StringFormat,
TruncateFormat,
UrlFormat,
FieldsFormat,
} from '../converters';

export const baseFormatters: FieldFormatInstanceType[] = [
Expand All @@ -49,4 +50,5 @@ export const baseFormatters: FieldFormatInstanceType[] = [
StringFormat,
TruncateFormat,
UrlFormat,
FieldsFormat,
];
49 changes: 49 additions & 0 deletions src/plugins/data/common/field_formats/converters/fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { keys } from 'lodash';
import { doTemplate, SourceFormat } from './source';
import { FIELD_FORMAT_IDS, HtmlContextTypeConvert } from '../types';
import { KBN_FIELD_TYPES } from '../../kbn_field_types/types';
import { shortenDottedString } from '../../utils';

export class FieldsFormat extends SourceFormat {
static id = FIELD_FORMAT_IDS.FIELDS;
static title = 'fields';
static fieldType = KBN_FIELD_TYPES._SOURCE;

htmlConvert: HtmlContextTypeConvert = (value, options = {}) => {
const { hit, indexPattern } = options;

const highlights = (hit && hit.highlight) || {};
const formatted = indexPattern.formatHit(hit);
const highlightPairs: any[] = [];
const sourcePairs: any[] = [];
const isShortDots = false;

keys(formatted).forEach((key) => {
const pairs = highlights[key] ? highlightPairs : sourcePairs;
const newField = isShortDots ? shortenDottedString(key) : key;
const val = formatted[key];
pairs.push([newField, val]);
}, []);

return doTemplate({ defPairs: highlightPairs.concat(sourcePairs) });
};
}
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/converters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export { ColorFormat } from './color';
export { TruncateFormat } from './truncate';
export { BoolFormat } from './boolean';
export { StaticLookupFormat } from './static_lookup';
export { FieldsFormat } from './fields';
2 changes: 1 addition & 1 deletion src/plugins/data/common/field_formats/converters/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const templateHtml = `
<%= ' ' %>
<% }); %>
</dl>`;
const doTemplate = template(noWhiteSpace(templateHtml));
export const doTemplate = template(noWhiteSpace(templateHtml));

export class SourceFormat extends FieldFormat {
static id = FIELD_FORMAT_IDS._SOURCE;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export {
UrlFormat,
StringFormat,
TruncateFormat,
FieldsFormat,
} from './converters';

export { getHighlightRequest } from './utils';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export enum FIELD_FORMAT_IDS {
STRING = 'string',
TRUNCATE = 'truncate',
URL = 'url',
FIELDS = 'fields',
}

export interface FieldFormatConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import _, { each, reject } from 'lodash';
import { FieldAttrs, FieldAttrSet } from '../..';
import { DuplicateField } from '../../../../kibana_utils/common';

import { ES_FIELD_TYPES, KBN_FIELD_TYPES, IIndexPattern, IFieldType } from '../../../common';
import { IndexPatternField, IIndexPatternFieldList, fieldList } from '../fields';
import { ES_FIELD_TYPES, IFieldType, IIndexPattern, KBN_FIELD_TYPES } from '../../../common';
import { fieldList, IIndexPatternFieldList, IndexPatternField } from '../fields';
import { formatHitProvider } from './format_hit';
import { flattenHitWrapper } from './flatten_hit';
import { FieldFormatsStartCommon, FieldFormat } from '../../field_formats';
import { IndexPatternSpec, TypeMeta, SourceFilter, IndexPatternFieldMap } from '../types';
import { FieldFormat, FieldFormatsStartCommon } from '../../field_formats';
import { IndexPatternFieldMap, IndexPatternSpec, SourceFilter, TypeMeta } from '../types';
import { SerializedFieldFormat } from '../../../../expressions/common';

interface IndexPatternDeps {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/discover/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export const CONTEXT_DEFAULT_SIZE_SETTING = 'context:defaultSize';
export const CONTEXT_STEP_SETTING = 'context:step';
export const CONTEXT_TIE_BREAKER_FIELDS_SETTING = 'context:tieBreakerFields';
export const MODIFY_COLUMNS_ON_SWITCH = 'discover:modifyColumnsOnSwitch';
export const SEARCH_FIELDS_FROM_SOURCE = 'discover:searchFieldsFromSource';
71 changes: 58 additions & 13 deletions src/plugins/discover/public/application/angular/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
DEFAULT_COLUMNS_SETTING,
MODIFY_COLUMNS_ON_SWITCH,
SAMPLE_SIZE_SETTING,
SEARCH_FIELDS_FROM_SOURCE,
SEARCH_ON_PAGE_LOAD_SETTING,
} from '../../../common';
import { loadIndexPattern, resolveIndexPattern } from '../helpers/resolve_index_pattern';
Expand Down Expand Up @@ -192,6 +193,9 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab
$scope.searchSource,
toastNotifications
);
$scope.useNewFieldsApi = !config.get(SEARCH_FIELDS_FROM_SOURCE);

const FIRST_N_COLUMNS_FROM_FIELDS_RESPONSE = 3;
//used for functional testing
$scope.fetchCounter = 0;

Expand Down Expand Up @@ -303,7 +307,8 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab
nextIndexPattern,
$scope.state.columns,
$scope.state.sort,
config.get(MODIFY_COLUMNS_ON_SWITCH)
config.get(MODIFY_COLUMNS_ON_SWITCH),
$scope.useNewFieldsApi
);
await setAppState(nextAppState);
}
Expand Down Expand Up @@ -408,15 +413,40 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab

setBreadcrumbsTitle(savedSearch, chrome);

function removeElFromColumns(el, columns) {
const index = columns.indexOf(el);
if (index !== -1) {
columns.splice(index, 1);
}
return columns;
}

function removeSourceFromColumns(columns) {
return removeElFromColumns('_source', columns);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Replacing by a simple filter will imho read nicer and prevents in place modification of the array, that's used somewhere else and can have weird side-effects if we forget about that (see avoid mutability):

Suggested change
return removeElFromColumns('_source', columns);
return columns.filter(col => col !== '_source');

}

function removeFieldsFromColumns(columns) {
return removeElFromColumns('fields', columns);
}

function getDefaultColumns() {
kertal marked this conversation as resolved.
Show resolved Hide resolved
const columns = [...savedSearch.columns];

if ($scope.useNewFieldsApi) {
return removeSourceFromColumns(columns);
} else if (columns.length > 0) {
return removeFieldsFromColumns(columns);
}
return config.get(DEFAULT_COLUMNS_SETTING).slice();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 According to our style guide let's please prefer modern JS syntax here:

Suggested change
return config.get(DEFAULT_COLUMNS_SETTING).slice();
return [...config.get(DEFAULT_COLUMNS_SETTING)];

}

function getStateDefaults() {
const query = $scope.searchSource.getField('query') || data.query.queryString.getDefaultQuery();
const columns = getDefaultColumns();
return {
query,
sort: getSortArray(savedSearch.sort, $scope.indexPattern),
columns:
savedSearch.columns.length > 0
? savedSearch.columns
: config.get(DEFAULT_COLUMNS_SETTING).slice(),
columns,
index: $scope.indexPattern.id,
interval: 'auto',
filters: _.cloneDeep($scope.searchSource.getOwnField('filter')),
Expand Down Expand Up @@ -659,7 +689,18 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab
}

$scope.hits = resp.hits.total;
$scope.rows = resp.hits.hits;
$scope.rows = resp.hits.hits.map((hit) => {
if ($scope.useNewFieldsApi) {
const fields = {};
Object.keys(hit.fields)
.splice(0, FIRST_N_COLUMNS_FROM_FIELDS_RESPONSE)
.forEach((key) => {
fields[key] = hit.fields[key];
});
return { ...hit };
}
return hit;
});

$scope.fieldCounts = calcFieldCounts(
$scope.fieldCounts || {},
Expand Down Expand Up @@ -721,10 +762,14 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab
};

$scope.updateDataSource = () => {
updateSearchSource($scope.searchSource, {
indexPattern: $scope.indexPattern,
const { indexPattern, searchSource, useNewFieldsApi } = $scope;
const { columns, sort } = $scope.state;
updateSearchSource(searchSource, {
indexPattern,
services,
sort: $scope.state.sort,
sort,
columns,
useNewFieldsApi,
});
return Promise.resolve();
};
Expand Down Expand Up @@ -752,20 +797,20 @@ function discoverController($element, $route, $scope, $timeout, Promise, uiCapab
};

$scope.addColumn = function addColumn(columnName) {
const { indexPattern, useNewFieldsApi } = $scope;
if (uiCapabilities.discover.save) {
const { indexPattern } = $scope;
popularizeField(indexPattern, columnName, indexPatterns);
}
const columns = columnActions.addColumn($scope.state.columns, columnName);
const columns = columnActions.addColumn($scope.state.columns, columnName, useNewFieldsApi);
setAppState({ columns });
};

$scope.removeColumn = function removeColumn(columnName) {
const { indexPattern, useNewFieldsApi } = $scope;
if (uiCapabilities.discover.save) {
const { indexPattern } = $scope;
popularizeField(indexPattern, columnName, indexPatterns);
}
const columns = columnActions.removeColumn($scope.state.columns, columnName);
const columns = columnActions.removeColumn($scope.state.columns, columnName, useNewFieldsApi);
// The state's sort property is an array of [sortByColumn,sortDirection]
const sort = $scope.state.sort.length
? $scope.state.sort.filter((subArr) => subArr[0] !== columnName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
top-nav-menu="topNavMenu"
update-query="handleRefresh"
update-saved-query-id="updateSavedQueryId"
use-new-fields-api="useNewFieldsApi"
>
</discover-legacy>
</discover-app>
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,34 @@
* Helper function to provide a fallback to a single _source column if the given array of columns
* is empty, and removes _source if there are more than 1 columns given
* @param columns
* @param useNewFieldsApi should a new fields API be used
*/
function buildColumns(columns: string[]) {
function buildColumns(columns: string[], useNewFieldsApi = false) {
if (columns.length > 1 && columns.indexOf('_source') !== -1) {
return columns.filter((col) => col !== '_source');
} else if (columns.length > 1 && useNewFieldsApi && columns.indexOf('fields') !== -1) {
return columns.filter((col) => col !== 'fields');
} else if (columns.length !== 0) {
return columns;
}
return ['_source'];
return useNewFieldsApi ? ['fields'] : ['_source'];
}

export function addColumn(columns: string[], columnName: string) {
export function addColumn(columns: string[], columnName: string, useNewFieldsApi?: boolean) {
if (columns.includes(columnName)) {
return columns;
}
return buildColumns([...columns, columnName]);
return buildColumns([...columns, columnName], useNewFieldsApi);
}

export function removeColumn(columns: string[], columnName: string) {
export function removeColumn(columns: string[], columnName: string, useNewFieldsApi?: boolean) {
if (!columns.includes(columnName)) {
return columns;
}
return buildColumns(columns.filter((col) => col !== columnName));
return buildColumns(
columns.filter((col) => col !== columnName),
useNewFieldsApi
);
}

export function moveColumn(columns: string[], columnName: string, newIndex: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getDisplayedColumns(
name: column,
displayName: field ? field.displayName : column,
isSortable: field && field.sortable ? true : false,
isRemoveable: column !== '_source' || columns.length > 1,
isRemoveable: (column !== '_source' && column !== 'fields') || columns.length > 1,
colLeftIdx: idx - 1 < 0 ? -1 : idx - 1,
colRightIdx: idx + 1 >= columns.length ? -1 : idx + 1,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import cellTemplateHtml from '../components/table_row/cell.html';
import truncateByHeightTemplateHtml from '../components/table_row/truncate_by_height.html';
import { getServices } from '../../../../kibana_services';
import { getContextUrl } from '../../../helpers/get_context_url';
import { FieldsFormat } from '../../../../../../data/common';

const TAGS_WITH_WS = />\s+</g;

Expand Down Expand Up @@ -145,7 +146,7 @@ export function createTableRowDirective($compile: ng.ICompileService) {
newHtmls.push(
cellTemplate({
timefield: false,
sourcefield: column === '_source',
sourcefield: column === '_source' || column === 'fields',
formatted: _displayField(row, column, true),
filterable: isFilterable,
column,
Expand Down Expand Up @@ -194,7 +195,13 @@ export function createTableRowDirective($compile: ng.ICompileService) {
*/
function _displayField(row: any, fieldName: string, truncate = false) {
const indexPattern = $scope.indexPattern;
const text = indexPattern.formatField(row, fieldName);
let text;
if (fieldName !== 'fields') {
text = indexPattern.formatField(row, fieldName);
} else {
const format = new FieldsFormat();
text = format.htmlConvert(row[fieldName], { hit: row, indexPattern });
}

if (truncate && text.length > MIN_LINE_LENGTH) {
return truncateByHeightTemplate({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
.kbnDocTableCell__dataField {
white-space: pre-wrap;
}

.kbnDocTableCell__toggleDetails {
padding: 4px 0 0 0!important;
padding: $euiSizeXS 0 0 0!important;
}

.kbnDocTableCell__filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export function createDiscoverLegacyDirective(reactDirective: any) {
['topNavMenu', { watchDepth: 'reference' }],
['updateQuery', { watchDepth: 'reference' }],
['updateSavedQueryId', { watchDepth: 'reference' }],
['useNewFieldsApi', { watchDepth: 'reference' }],
]);
}
Loading