Skip to content

Commit

Permalink
[Mappings editor] Add support for version field type (elastic#78206)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Oct 1, 2020
1 parent 827b4a5 commit ee2b30c
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { act } from 'react-dom/test-utils';

import { componentHelpers, MappingsEditorTestBed } from '../helpers';

const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;

// Parameters automatically added to the version datatype when saved (with the default values)
export const defaultVersionParameters = {
type: 'version',
};

describe('Mappings editor: version datatype', () => {
/**
* Variable to store the mappings data forwarded to the consumer component
*/
let data: any;
let onChangeHandler: jest.Mock = jest.fn();
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
let testBed: MappingsEditorTestBed;

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

beforeEach(() => {
onChangeHandler = jest.fn();
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
});

test('supports meta parameter', async () => {
const defaultMappings = {
properties: {
myField: {
type: 'version',
},
},
};

const updatedMappings = { ...defaultMappings };

const metaParameter = {
meta: {
my_metadata: 'foobar',
},
};

await act(async () => {
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
});
testBed.component.update();

const {
component,
actions: {
startEditField,
updateFieldAndCloseFlyout,
showAdvancedSettings,
toggleFormRow,
updateJsonEditor,
},
} = testBed;

// Open the flyout to edit the field
await startEditField('myField');
await showAdvancedSettings();

// Enable the meta parameter and provide a valid object
toggleFormRow('metaParameter');
await act(async () => {
updateJsonEditor('metaParameterEditor', metaParameter.meta);
});
component.update();

// Save the field and close the flyout
await updateFieldAndCloseFlyout();

// It should have the default parameters values added, plus metadata
updatedMappings.properties.myField = {
...defaultVersionParameters,
...metaParameter,
};

({ data } = await getMappingsEditorData(component));
expect(data).toEqual(updatedMappings);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { RankFeatureType } from './rank_feature_type';
import { RuntimeType } from './runtime_type';
import { WildcardType } from './wildcard_type';
import { PointType } from './point_type';
import { VersionType } from './version_type';

const typeToParametersFormMap: { [key in DataType]?: ComponentType<any> } = {
alias: AliasType,
Expand Down Expand Up @@ -64,6 +65,7 @@ const typeToParametersFormMap: { [key in DataType]?: ComponentType<any> } = {
runtime: RuntimeType,
wildcard: WildcardType,
point: PointType,
version: VersionType,
};

export const getParametersFormForType = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import React from 'react';

import { NormalizedField, Field as FieldType, ParameterName } from '../../../../types';
import { getFieldConfig } from '../../../../lib';
import { MetaParameter } from '../../field_parameters';
import { AdvancedParametersSection } from '../edit_field';

interface Props {
field: NormalizedField;
}

const getDefaultToggleValue = (param: ParameterName, field: FieldType) => {
return field[param] !== undefined && field[param] !== getFieldConfig(param).defaultValue;
};

export const VersionType = ({ field }: Props) => {
return (
<AdvancedParametersSection>
<MetaParameter defaultToggleValue={getDefaultToggleValue('meta', field.source)} />
</AdvancedParametersSection>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,35 @@ export const TYPE_DEFINITION: { [key in DataType]: DataTypeDefinition } = {
</p>
),
},
version: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.dataType.versionDescription', {
defaultMessage: 'Version',
}),
value: 'version',
documentation: {
main: '/version.html',
},
description: () => (
<p>
<FormattedMessage
id="xpack.idxMgmt.mappingsEditor.dataType.versionLongDescription"
defaultMessage="Version fields are helpful to handle software version values. This field isn’t optimized for heavy wildcard, regex, or fuzzy searches. For these query types, use the {keywordType}."
values={{
keywordType: (
<EuiLink href={documentationService.getTypeDocLink('keyword')} target="_blank">
{i18n.translate(
'xpack.idxMgmt.mappingsEditor.dataType.versionLongDescription.keywordTypeLink',
{
defaultMessage: 'keyword data type',
}
)}
</EuiLink>
),
}}
/>
</p>
),
},
wildcard: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.dataType.wildcardDescription', {
defaultMessage: 'Wildcard',
Expand Down Expand Up @@ -923,6 +952,7 @@ export const MAIN_TYPES: MainType[] = [
'histogram',
'wildcard',
'point',
'version',
'other',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type MainType =
| 'point'
| 'histogram'
| 'constant_keyword'
| 'version'
| 'wildcard'
/**
* 'other' is a special type that only exists inside of MappingsEditor as a placeholder
Expand Down

0 comments on commit ee2b30c

Please sign in to comment.