Skip to content

Commit

Permalink
[Mappings Editor] Add support for synthetic _source
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Nov 12, 2024
1 parent aa4f430 commit b234d7a
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@
* 2.0.
*/

import React, { useEffect, useRef, useCallback } from 'react';
import React, { useEffect, useRef } from 'react';
import { EuiSpacer } from '@elastic/eui';

import { FormData } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib';
import { useAppContext } from '../../../../app_context';
import { useForm, Form } from '../../shared_imports';
import { GenericObject, MappingsConfiguration } from '../../types';
import { MapperSizePluginId } from '../../constants';
import { useDispatch } from '../../mappings_state_context';
import { DynamicMappingSection } from './dynamic_mapping_section';
import { SourceFieldSection } from './source_field_section';
import {
SourceFieldSection,
STORED_SOURCE_OPTION,
SYNTHETIC_SOURCE_OPTION,
DISABLED_SOURCE_OPTION,
} from './source_field_section';
import { MetaFieldSection } from './meta_field_section';
import { RoutingSection } from './routing_section';
import { MapperSizePluginSection } from './mapper_size_plugin_section';
import { SubobjectsSection } from './subobjects_section';
import { configurationFormSchema } from './configuration_form_schema';
import { IndexMode } from '../../../../../../common/types/data_streams';

interface Props {
value?: MappingsConfiguration;
/** List of plugins installed in the cluster nodes */
esNodesPlugins: string[];
indexMode?: IndexMode;
}

const formSerializer = (formData: GenericObject, sourceFieldMode?: string) => {
const formSerializer = (formData: GenericObject) => {
const { dynamicMapping, sourceField, metaField, _routing, _size, subobjects } = formData;

const dynamic = dynamicMapping?.enabled
Expand All @@ -37,12 +43,21 @@ const formSerializer = (formData: GenericObject, sourceFieldMode?: string) => {
? 'strict'
: dynamicMapping?.enabled;

const _source =
sourceField?.option === SYNTHETIC_SOURCE_OPTION
? { mode: SYNTHETIC_SOURCE_OPTION }
: sourceField?.option === DISABLED_SOURCE_OPTION
? { enabled: false }
: sourceField?.includes || sourceField?.excludes
? { includes: sourceField?.includes, excludes: sourceField?.excludes }
: undefined;

const serialized = {
dynamic,
numeric_detection: dynamicMapping?.numeric_detection,
date_detection: dynamicMapping?.date_detection,
dynamic_date_formats: dynamicMapping?.dynamic_date_formats,
_source: sourceFieldMode ? { mode: sourceFieldMode } : sourceField,
_source,
_meta: metaField,
_routing,
_size,
Expand All @@ -60,8 +75,9 @@ const formDeserializer = (formData: GenericObject) => {
date_detection,
dynamic_date_formats,
/* eslint-enable @typescript-eslint/naming-convention */
_source: { enabled, includes, excludes } = {} as {
_source: { enabled, mode, includes, excludes } = {} as {
enabled?: boolean;
mode?: string;
includes?: string[];
excludes?: string[];
},
Expand All @@ -81,7 +97,7 @@ const formDeserializer = (formData: GenericObject) => {
dynamic_date_formats,
},
sourceField: {
enabled,
option: mode ?? (enabled === false ? 'disabled' : 'stored'),
includes,
excludes,
},
Expand All @@ -92,21 +108,16 @@ const formDeserializer = (formData: GenericObject) => {
};
};

export const ConfigurationForm = React.memo(({ value, esNodesPlugins }: Props) => {
export const ConfigurationForm = React.memo(({ value, esNodesPlugins, indexMode }: Props) => {
const {
config: { enableMappingsSourceFieldSection },
} = useAppContext();

const isMounted = useRef(false);

const serializerCallback = useCallback(
(formData: FormData) => formSerializer(formData, value?._source?.mode),
[value?._source?.mode]
);

const { form } = useForm({
schema: configurationFormSchema,
serializer: serializerCallback,
schema: configurationFormSchema(indexMode),
serializer: formSerializer,
deserializer: formDeserializer,
defaultValue: value,
id: 'configurationForm',
Expand Down Expand Up @@ -165,7 +176,7 @@ export const ConfigurationForm = React.memo(({ value, esNodesPlugins }: Props) =
<EuiSpacer size="xl" />
<MetaFieldSection />
<EuiSpacer size="xl" />
{enableMappingsSourceFieldSection && !value?._source?.mode && (
{enableMappingsSourceFieldSection && (
<>
<SourceFieldSection /> <EuiSpacer size="xl" />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { EuiLink, EuiCode } from '@elastic/eui';
import { documentationService } from '../../../../services/documentation';
import { FormSchema, FIELD_TYPES, fieldValidators } from '../../shared_imports';
import { ComboBoxOption } from '../../types';
import { STORED_SOURCE_OPTION, SYNTHETIC_SOURCE_OPTION } from './source_field_section';
import { IndexMode } from "@kbn/index-management-plugin/common/types/data_streams";

const { isJsonField } = fieldValidators;

Expand All @@ -30,7 +32,7 @@ const fieldPathComboBoxConfig = {
deserializer: (values: string[]): ComboBoxOption[] => values.map((value) => ({ label: value })),
};

export const configurationFormSchema: FormSchema = {
export const configurationFormSchema = (indexMode?: IndexMode): FormSchema => ({
metaField: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.configuration.metaFieldEditorLabel', {
defaultMessage: '_meta field data',
Expand Down Expand Up @@ -75,12 +77,12 @@ export const configurationFormSchema: FormSchema = {
},
},
sourceField: {
enabled: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.configuration.sourceFieldLabel', {
defaultMessage: 'Enable _source field',
}),
type: FIELD_TYPES.TOGGLE,
defaultValue: true,
option: {
type: FIELD_TYPES.SUPER_SELECT,
defaultValue:
indexMode === 'logsdb' || indexMode === 'time_series'
? SYNTHETIC_SOURCE_OPTION
: STORED_SOURCE_OPTION,
},
includes: {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.configuration.includeSourceFieldsLabel', {
Expand Down Expand Up @@ -192,4 +194,4 @@ export const configurationFormSchema: FormSchema = {
}),
defaultValue: true,
},
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const STORED_SOURCE_OPTION = 'stored';
export const DISABLED_SOURCE_OPTION = 'disabled';
export const SYNTHETIC_SOURCE_OPTION = 'synthetic';
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { STORED_SOURCE_OPTION, DISABLED_SOURCE_OPTION, SYNTHETIC_SOURCE_OPTION} from './constants';

export const sourceOptionLabels = {
[STORED_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.storedSourceFieldsLabel',
{
defaultMessage: 'Stored _source',
}
),
[DISABLED_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.disabledSourceFieldsLabel',
{
defaultMessage: 'Disabled _source',
}
),
[SYNTHETIC_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.syntheticSourceFieldsLabel',
{
defaultMessage: 'Synthetic _source',
}
),
};

export const sourceOptionDescriptions = {
[STORED_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.storedSourceFieldsDescription',
{
defaultMessage: 'Stores content in _source field for future retrieval',
}
),
[DISABLED_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.disabledSourceFieldsDescription',
{
defaultMessage: 'Reconstructs source content to save on disk usage',
}
),
[SYNTHETIC_SOURCE_OPTION]: i18n.translate(
'xpack.idxMgmt.mappingsEditor.configuration.syntheticSourceFieldsDescription',
{
defaultMessage: 'Strongly discouraged, will impact downstream functionality',
}
),
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export { SourceFieldSection } from './source_field_section';
export * from './constants';
Loading

0 comments on commit b234d7a

Please sign in to comment.