diff --git a/x-pack/legacy/plugins/maps/public/components/single_field_select.js b/x-pack/legacy/plugins/maps/public/components/single_field_select.js index ba9ef1f22c54c..7351ce7691a82 100644 --- a/x-pack/legacy/plugins/maps/public/components/single_field_select.js +++ b/x-pack/legacy/plugins/maps/public/components/single_field_select.js @@ -8,63 +8,50 @@ import _ from 'lodash'; import PropTypes from 'prop-types'; import React from 'react'; -import { EuiComboBox } from '@elastic/eui'; +import { EuiComboBox, EuiHighlight } from '@elastic/eui'; +import { FieldIcon } from '../../../../../../src/plugins/kibana_react/public'; -const sortByLabel = (a, b) => { - if (a.label < b.label) return -1; - if (a.label > b.label) return 1; - return 0; -}; - -// Creates grouped options by grouping fields by field type -export const getGroupedFieldOptions = (fields, filterField) => { +function fieldsToOptions(fields) { if (!fields) { - return; + return []; } - const fieldsByTypeMap = new Map(); - - fields.filter(filterField).forEach(field => { - const fieldLabel = 'label' in field ? field.label : field.name; - if (fieldsByTypeMap.has(field.type)) { - const fieldsList = fieldsByTypeMap.get(field.type); - fieldsList.push({ value: field.name, label: fieldLabel }); - fieldsByTypeMap.set(field.type, fieldsList); - } else { - fieldsByTypeMap.set(field.type, [{ value: field.name, label: fieldLabel }]); - } - }); - - const groupedFieldOptions = []; - fieldsByTypeMap.forEach((fieldsList, fieldType) => { - const sortedOptions = fieldsList.sort(sortByLabel).map(({ value, label }) => { - return { value: value, label: label }; + return fields + .map(field => { + return { + value: field, + label: 'label' in field ? field.label : field.name, + }; + }) + .sort((a, b) => { + return a.label.toLowerCase().localeCompare(b.label.toLowerCase()); }); +} - groupedFieldOptions.push({ - label: fieldType, - options: sortedOptions, - }); - }); - - groupedFieldOptions.sort(sortByLabel); - - return groupedFieldOptions; -}; +function renderOption(option, searchValue, contentClassName) { + return ( + + +   + {option.label} + + ); +} -export function SingleFieldSelect({ fields, filterField, onChange, value, placeholder, ...rest }) { +export function SingleFieldSelect({ fields, onChange, value, placeholder, ...rest }) { const onSelection = selectedOptions => { - onChange(_.get(selectedOptions, '0.value')); + onChange(_.get(selectedOptions, '0.value.name')); }; return ( ); @@ -75,11 +62,4 @@ SingleFieldSelect.propTypes = { fields: PropTypes.array, onChange: PropTypes.func.isRequired, value: PropTypes.string, // fieldName - filterField: PropTypes.func, -}; - -SingleFieldSelect.defaultProps = { - filterField: () => { - return true; - }, }; diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js index 76827e71df9ec..777c8ae0923fe 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js @@ -23,7 +23,6 @@ import { getTermsFields } from '../../../../index_pattern_util'; import { indexPatternService } from '../../../../kibana_services'; import { npStart } from 'ui/new_platform'; -import { isNestedField } from '../../../../../../../../../src/plugins/data/public'; const { IndexPatternSelect } = npStart.plugins.data.ui; export class JoinExpression extends Component { @@ -134,10 +133,6 @@ export class JoinExpression extends Component { return null; } - const filterStringOrNumberFields = field => { - return (field.type === 'string' && !isNestedField(field)) || field.type === 'number'; - }; - return ( diff --git a/x-pack/legacy/plugins/maps/public/index_pattern_util.js b/x-pack/legacy/plugins/maps/public/index_pattern_util.js index 10837bc2f0d0c..96d4a4b19fbfa 100644 --- a/x-pack/legacy/plugins/maps/public/index_pattern_util.js +++ b/x-pack/legacy/plugins/maps/public/index_pattern_util.js @@ -6,6 +6,7 @@ import { indexPatternService } from './kibana_services'; import { isNestedField } from '../../../../../src/plugins/data/public'; +import { ES_GEO_FIELD_TYPE } from '../common/constants'; export async function getIndexPatternsFromIds(indexPatternIds = []) { const promises = []; @@ -29,6 +30,18 @@ export function getTermsFields(fields) { }); } +export const AGGREGATABLE_GEO_FIELD_TYPES = [ES_GEO_FIELD_TYPE.GEO_POINT]; + +export function getAggregatableGeoFields(fields) { + return fields.filter(field => { + return ( + field.aggregatable && + !isNestedField(field) && + AGGREGATABLE_GEO_FIELD_TYPES.includes(field.type) + ); + }); +} + // Returns filtered fields list containing only fields that exist in _source. export function getSourceFields(fields) { return fields.filter(field => { diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js index c32b857b49171..bd074386edb3f 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js @@ -15,16 +15,14 @@ import { NoIndexPatternCallout } from '../../../components/no_index_pattern_call import { i18n } from '@kbn/i18n'; import { EuiFormRow, EuiComboBox, EuiSpacer } from '@elastic/eui'; -import { ES_GEO_FIELD_TYPE } from '../../../../common/constants'; -import { isNestedField } from '../../../../../../../../src/plugins/data/public'; +import { + AGGREGATABLE_GEO_FIELD_TYPES, + getAggregatableGeoFields, +} from '../../../index_pattern_util'; import { npStart } from 'ui/new_platform'; const { IndexPatternSelect } = npStart.plugins.data.ui; -function filterGeoField({ type }) { - return [ES_GEO_FIELD_TYPE.GEO_POINT].includes(type); -} - const requestTypeOptions = [ { label: i18n.translate('xpack.maps.source.esGeoGrid.gridRectangleDropdownOption', { @@ -116,9 +114,7 @@ export class CreateSourceEditor extends Component { }); //make default selection - const geoFields = indexPattern.fields - .filter(field => !isNestedField(field)) - .filter(filterGeoField); + const geoFields = getAggregatableGeoFields(indexPattern.fields); if (geoFields[0]) { this._onGeoFieldSelect(geoFields[0].name); } @@ -173,10 +169,9 @@ export class CreateSourceEditor extends Component { })} value={this.state.geoField} onChange={this._onGeoFieldSelect} - filterField={filterGeoField} fields={ this.state.indexPattern - ? this.state.indexPattern.fields.filter(field => !isNestedField(field)) + ? getAggregatableGeoFields(this.state.indexPattern.fields) : undefined } /> @@ -223,7 +218,7 @@ export class CreateSourceEditor extends Component { placeholder={i18n.translate('xpack.maps.source.esGeoGrid.indexPatternPlaceholder', { defaultMessage: 'Select index pattern', })} - fieldTypes={[ES_GEO_FIELD_TYPE.GEO_POINT]} + fieldTypes={AGGREGATABLE_GEO_FIELD_TYPES} onNoIndexPatterns={this._onNoIndexPatterns} /> diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/create_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/create_source_editor.js index 85d63c9da8a31..5e4727cd7ab0c 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/create_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_pew_pew_source/create_source_editor.js @@ -14,16 +14,13 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiFormRow, EuiCallOut } from '@elastic/eui'; -import { ES_GEO_FIELD_TYPE } from '../../../../common/constants'; -import { isNestedField } from '../../../../../../../../src/plugins/data/public'; +import { + AGGREGATABLE_GEO_FIELD_TYPES, + getAggregatableGeoFields, +} from '../../../index_pattern_util'; import { npStart } from 'ui/new_platform'; const { IndexPatternSelect } = npStart.plugins.data.ui; -const GEO_FIELD_TYPES = [ES_GEO_FIELD_TYPE.GEO_POINT]; - -function filterGeoField({ type }) { - return GEO_FIELD_TYPES.includes(type); -} export class CreateSourceEditor extends Component { static propTypes = { @@ -92,10 +89,7 @@ export class CreateSourceEditor extends Component { return; } - const geoFields = indexPattern.fields - .filter(field => !isNestedField(field)) - .filter(filterGeoField); - + const geoFields = getAggregatableGeoFields(indexPattern.fields); this.setState({ isLoadingIndexPattern: false, indexPattern: indexPattern, @@ -136,6 +130,9 @@ export class CreateSourceEditor extends Component { return null; } + const fields = this.state.indexPattern + ? getAggregatableGeoFields(this.state.indexPattern.fields) + : undefined; return ( @@ -165,12 +161,7 @@ export class CreateSourceEditor extends Component { })} value={this.state.destGeoField} onChange={this._onDestGeoSelect} - filterField={filterGeoField} - fields={ - this.state.indexPattern - ? this.state.indexPattern.fields.filter(field => !isNestedField(field)) - : undefined - } + fields={fields} /> @@ -190,7 +181,7 @@ export class CreateSourceEditor extends Component { placeholder={i18n.translate('xpack.maps.source.pewPew.indexPatternPlaceholder', { defaultMessage: 'Select index pattern', })} - fieldTypes={GEO_FIELD_TYPES} + fieldTypes={AGGREGATABLE_GEO_FIELD_TYPES} /> ); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/__snapshots__/update_source_editor.test.js.snap b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/__snapshots__/update_source_editor.test.js.snap index 9afe22a5f4550..80368fd5d5e3e 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/__snapshots__/update_source_editor.test.js.snap +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/__snapshots__/update_source_editor.test.js.snap @@ -53,7 +53,6 @@ exports[`should enable sort order select when sort field provided 1`] = ` @@ -230,7 +228,6 @@ exports[`should render top hits form when useTopHits is true 1`] = ` diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/create_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/create_source_editor.js index 69e4c09eed118..ad55a279f9cd7 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/create_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/create_source_editor.js @@ -26,8 +26,13 @@ import { isNestedField } from '../../../../../../../../src/plugins/data/public'; import { npStart } from 'ui/new_platform'; const { IndexPatternSelect } = npStart.plugins.data.ui; -function filterGeoField(field) { - return [ES_GEO_FIELD_TYPE.GEO_POINT, ES_GEO_FIELD_TYPE.GEO_SHAPE].includes(field.type); +function getGeoFields(fields) { + return fields.filter(field => { + return ( + !isNestedField(field) && + [ES_GEO_FIELD_TYPE.GEO_POINT, ES_GEO_FIELD_TYPE.GEO_SHAPE].includes(field.type) + ); + }); } const RESET_INDEX_PATTERN_STATE = { indexPattern: undefined, @@ -125,9 +130,7 @@ export class CreateSourceEditor extends Component { }); //make default selection - const geoFields = indexPattern.fields - .filter(field => !isNestedField(field)) - .filter(filterGeoField); + const geoFields = getGeoFields(indexPattern.fields); if (geoFields[0]) { this.onGeoFieldSelect(geoFields[0].name); } @@ -180,11 +183,8 @@ export class CreateSourceEditor extends Component { })} value={this.state.geoField} onChange={this.onGeoFieldSelect} - filterField={filterGeoField} fields={ - this.state.indexPattern - ? this.state.indexPattern.fields.filter(field => !isNestedField(field)) - : undefined + this.state.indexPattern ? getGeoFields(this.state.indexPattern.fields) : undefined } />