-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Show ignored field values #115040
Changes from 20 commits
9765492
76e44ae
4304f55
13bb111
07c2231
983f830
8be2525
5c47379
8292e0c
6ebbf26
921bd6c
066c7aa
5349298
911cf9e
f890df2
ce17147
4f02850
afad073
7829357
18e2205
71ac26c
3dc6746
3dc160c
a746799
ddfaf53
f164811
50799bd
cc07ef4
0337baf
1fd926c
95a31c5
17ed7fa
1d2f19f
6966db1
22be128
97c4984
0e6ef91
7713e5c
81f2b88
9d5f174
57c0130
79abe37
1802dc7
e24d0dd
5e3974a
358b77d
3863d3b
54b03ce
88be299
267334d
0aa05b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,8 +55,17 @@ export interface TabifyDocsOptions { | |
* merged into the flattened document. | ||
*/ | ||
source?: boolean; | ||
/** | ||
* If set to `true` values that have been ignored in ES (ignored_field_values) | ||
* will be merged into the flattened document. | ||
*/ | ||
includeIgnoredValues?: boolean; | ||
} | ||
|
||
// This is an overwrite of the SearchHit type to add the ignored_field_values. | ||
// Can be removed once the estypes.SearchHit knows about ignored_field_values | ||
type Hit<T = unknown> = estypes.SearchHit<T> & { ignored_field_values?: Record<string, unknown[]> }; | ||
|
||
/** | ||
* Flattens an individual hit (from an ES response) into an object. This will | ||
* create flattened field names, like `user.name`. | ||
|
@@ -65,11 +74,7 @@ export interface TabifyDocsOptions { | |
* @param indexPattern The index pattern for the requested index if available. | ||
* @param params Parameters how to flatten the hit | ||
*/ | ||
export function flattenHit( | ||
hit: estypes.SearchHit, | ||
indexPattern?: IndexPattern, | ||
params?: TabifyDocsOptions | ||
) { | ||
export function flattenHit(hit: Hit, indexPattern?: IndexPattern, params?: TabifyDocsOptions) { | ||
const flat = {} as Record<string, any>; | ||
|
||
function flatten(obj: Record<string, any>, keyPrefix: string = '') { | ||
|
@@ -111,6 +116,20 @@ export function flattenHit( | |
flatten(hit._source as Record<string, any>); | ||
} | ||
|
||
if (params?.includeIgnoredValues && hit.ignored_field_values) { | ||
timroes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object.entries(hit.ignored_field_values).forEach(([fieldName, fieldValue]) => { | ||
if (flat[fieldName]) { | ||
if (Array.isArray(flat[fieldName])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think this would be more readable if it was all one
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have mixed feelings around that. I feel making the 2nd if (displayKey && fieldsToShow.includes(key)) {
pairs.push([displayKey, formattedValue]);
} else if (!displayKey) {
pairs.push([key, formattedValue]);
} So maybe we can leave that to a democratic vote :) 🚀 - Flatten this out into one dimension as suggested above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "I don't think it would be more readable the way you suggest, Maja, I'd like to keep it as it is" would have been a perfectly fine response |
||
flat[fieldName] = [...flat[fieldName], ...fieldValue]; | ||
} else { | ||
flat[fieldName] = [flat[fieldName], ...fieldValue]; | ||
} | ||
} else { | ||
flat[fieldName] = fieldValue; | ||
} | ||
}); | ||
} | ||
|
||
// Merge all valid meta fields into the flattened object | ||
// expect for _source (in case that was specified as a meta field) | ||
indexPattern?.metaFields?.forEach((metaFieldName) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import React, { Fragment, useCallback, useMemo, useState } from 'react'; | |
import classNames from 'classnames'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiButtonEmpty, EuiIcon } from '@elastic/eui'; | ||
import { formatFieldValue } from '../../../../../helpers/format_value'; | ||
import { flattenHit } from '../../../../../../../../data/common'; | ||
import { DocViewer } from '../../../../../components/doc_viewer/doc_viewer'; | ||
import { FilterManager, IndexPattern } from '../../../../../../../../data/public'; | ||
|
@@ -58,7 +59,10 @@ export const TableRow = ({ | |
}); | ||
const anchorDocTableRowSubj = row.isAnchor ? ' docTableAnchorRow' : ''; | ||
|
||
const flattenedRow = useMemo(() => flattenHit(row, indexPattern), [indexPattern, row]); | ||
const flattenedRow = useMemo( | ||
() => flattenHit(row, indexPattern, { includeIgnoredValues: true }), | ||
[indexPattern, row] | ||
); | ||
const mapping = useMemo(() => indexPattern.fields.getByName, [indexPattern]); | ||
|
||
// toggle display of the rows details, a full list of the fields from each row | ||
|
@@ -68,7 +72,12 @@ export const TableRow = ({ | |
* Fill an element with the value of a field | ||
*/ | ||
const displayField = (fieldName: string) => { | ||
const formattedField = indexPattern.formatField(row, fieldName); | ||
const formattedField = formatFieldValue( | ||
flattenedRow[fieldName], | ||
row, | ||
indexPattern, | ||
mapping(fieldName) | ||
); | ||
|
||
// field formatters take care of escaping | ||
// eslint-disable-next-line react/no-danger | ||
|
@@ -142,9 +151,9 @@ export const TableRow = ({ | |
} else { | ||
columns.forEach(function (column: string) { | ||
// when useNewFieldsApi is true, addressing to the fields property is safe | ||
if (useNewFieldsApi && !mapping(column) && !row.fields![column]) { | ||
if (useNewFieldsApi && !mapping(column) && row.fields && !row.fields[column]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ This fixes another bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaik, fields API should never not return at least an empty object. maybe worth checking with the ES team to see if this scenario is intentional? |
||
const innerColumns = Object.fromEntries( | ||
Object.entries(row.fields!).filter(([key]) => { | ||
Object.entries(row.fields).filter(([key]) => { | ||
return key.indexOf(`${column}.`) === 0; | ||
}) | ||
); | ||
|
@@ -161,7 +170,13 @@ export const TableRow = ({ | |
/> | ||
); | ||
} else { | ||
const isFilterable = Boolean(mapping(column)?.filterable && filter); | ||
// Check whether the field is defined as filterable in the mapping and does | ||
// NOT have ignored values in it to determine whether we want to allow filtering. | ||
// We should improve this and show a helpful tooltip why the filter buttons are not | ||
// there/disabled when there are ignored values. | ||
const isFilterable = Boolean( | ||
mapping(column)?.filterable && filter && !row._ignored?.includes(column) | ||
); | ||
rowCells.push( | ||
<TableCell | ||
key={column} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { DiscoverGridContext } from './discover_grid_context'; | |
import { JsonCodeEditor } from '../json_code_editor/json_code_editor'; | ||
import { defaultMonacoEditorWidth } from './constants'; | ||
import { EsHitRecord } from '../../types'; | ||
import { formatFieldValue } from '../../helpers/format_value'; | ||
|
||
export const getRenderCellValueFn = | ||
( | ||
|
@@ -191,12 +192,12 @@ export const getRenderCellValueFn = | |
return <span>{JSON.stringify(rowFlattened[columnId])}</span>; | ||
} | ||
|
||
const valueFormatted = indexPattern.formatField(row, columnId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ A field formatter would always return a |
||
const valueFormatted = formatFieldValue(rowFlattened[columnId], row, indexPattern, field); | ||
if (typeof valueFormatted === 'undefined') { | ||
return <span>-</span>; | ||
} | ||
return ( | ||
// eslint-disable-next-line react/no-danger | ||
<span dangerouslySetInnerHTML={{ __html: indexPattern.formatField(row, columnId) }} /> | ||
<span dangerouslySetInnerHTML={{ __html: valueFormatted }} /> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { estypes } from '@elastic/elasticsearch'; | ||
import { DataView, DataViewField, KBN_FIELD_TYPES } from '../../../../data/common'; | ||
import { getServices } from '../../kibana_services'; | ||
|
||
// TODO: need more test coverage | ||
|
||
/** | ||
* Formats the value of a specific field using the appropriate field formatter if available | ||
* or the default string field formatter otherwise. | ||
* | ||
* @param value The value to format | ||
* @param hit The actual search hit (required to get highlight information from) | ||
* @param dataView The data view if available | ||
* @param field The field that value was from if available | ||
*/ | ||
export function formatFieldValue( | ||
value: unknown, | ||
hit: estypes.SearchHit, | ||
dataView?: DataView, | ||
field?: DataViewField | ||
): string { | ||
if (!dataView || !field) { | ||
// If either no field is available or no data view, we'll use the default | ||
// string formatter to format that field. | ||
return getServices() | ||
.fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING) | ||
.convert(value, 'html', { hit, field, indexPattern: dataView }); | ||
} | ||
|
||
// If we have a data view and field we use that fields field formatter | ||
return dataView | ||
.getFormatterForField(field) | ||
.convert(value, 'html', { hit, field, indexPattern: dataView }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason we don't make this default to true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've talked a bit in the PR description about this. I believe that the behavior of merging a response from the
fields
API with ignored values together might be more of an edge-case that we need in Discover (where users are more interested "in the document" than in the fields), thus I don't think we should have that on by default. Those values coming from that ignored API might cause other weird side-effects potentially, so I think we should make merging them into the flattened object and opt-in imho.