From 9a2f73430a82ca20afdd3fe13fa69568d812bc75 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Mon, 1 Jun 2020 16:16:19 +0200 Subject: [PATCH] [ML] filter bool agg support --- .../filter_agg/components/editor_form.tsx | 26 +++++++++++++++--- .../step_define/common/filter_agg/config.ts | 27 ++++++++++++++++--- .../step_define/common/filter_agg/types.ts | 19 ++++++++++--- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/editor_form.tsx b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/editor_form.tsx index 2c02440e965d49..c8adfa0f78d67e 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/editor_form.tsx +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/components/editor_form.tsx @@ -4,9 +4,27 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { FC } from 'react'; -import { EuiCodeEditor } from '@elastic/eui'; +import React from 'react'; +import { EuiCodeEditor, EuiSpacer } from '@elastic/eui'; +import { FilterAggConfigEditor } from '../types'; -export const FilterEditorForm: FC = () => { - return ; +export const FilterEditorForm: FilterAggConfigEditor['aggTypeConfig']['FilterAggFormComponent'] = ({ + config, + onChange, +}) => { + return ( + <> + + { + onChange({ config: d }); + }} + mode="json" + style={{ width: '100%' }} + theme="textmate" + height="300px" + /> + + ); }; diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/config.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/config.ts index ba175b9a73a7bc..eaf071ed7a54ec 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/config.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/config.ts @@ -12,6 +12,9 @@ import { import { FILTERS } from './constants'; import { FilterAggForm, FilterEditorForm, FilterRangeForm, FilterTermForm } from './components'; import { + FilterAggConfigBase, + FilterAggConfigBool, + FilterAggConfigExists, FilterAggConfigRange, FilterAggConfigTerm, FilterAggConfigUnion, @@ -67,7 +70,7 @@ export function getFilterAggConfig( export function getFilterAggTypeConfig( filterAggType: FilterAggConfigUnion['filterAgg'] | FilterAggType, config?: { [key: string]: any } -): FilterAggConfigUnion['aggTypeConfig'] { +): FilterAggConfigUnion['aggTypeConfig'] | FilterAggConfigBase['aggTypeConfig'] { switch (filterAggType) { case FILTERS.TERM: const value = typeof config === 'object' ? Object.values(config)[0] : undefined; @@ -118,13 +121,29 @@ export function getFilterAggTypeConfig( field: fieldName, }; }, - }; + } as FilterAggConfigExists['aggTypeConfig']; + case FILTERS.BOOL: + return { + FilterAggFormComponent: FilterEditorForm, + filterAggConfig: JSON.stringify( + { + must: [], + must_not: [], + should: [], + }, + null, + 2 + ), + getEsAggConfig(fieldName) { + return JSON.parse(this.filterAggConfig!); + }, + } as FilterAggConfigBool['aggTypeConfig']; default: return { FilterAggFormComponent: FilterEditorForm, - filterAggConfig: {}, + filterAggConfig: '', getEsAggConfig() { - return { ...this.filterAggConfig }; + return this.filterAggConfig !== undefined ? JSON.parse(this.filterAggConfig!) : {}; }, }; } diff --git a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/types.ts b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/types.ts index d3bf536e9eaa28..b61ba24e7a4b87 100644 --- a/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/types.ts +++ b/x-pack/plugins/transform/public/app/sections/create_transform/components/step_define/common/filter_agg/types.ts @@ -21,16 +21,16 @@ type FilterAggForm = FC<{ interface FilterAggTypeConfig { /** Form component */ - FilterAggFormComponent?: FilterAggForm; + FilterAggFormComponent?: U extends undefined ? undefined : FilterAggForm; /** Filter agg type configuration*/ - filterAggConfig?: U; + filterAggConfig?: U extends undefined ? undefined : U; /** Converts UI agg config form to ES agg request object */ getEsAggConfig: (field?: string) => { [key: string]: any }; isValid?: () => boolean; } /** Filter agg type definition */ -interface FilterAggProps { +interface FilterAggProps { /** Filter aggregation type */ filterAgg: K; /** Definition of the filter agg config */ @@ -44,8 +44,19 @@ export type FilterAggConfigRange = FilterAggProps< 'range', { gt?: number; lt?: number; lte?: number; gte?: number } >; +/** Filter exists agg */ +export type FilterAggConfigExists = FilterAggProps<'exists', undefined>; +/** Filter bool agg */ +export type FilterAggConfigBool = FilterAggProps<'bool', string>; -export type FilterAggConfigUnion = FilterAggConfigTerm | FilterAggConfigRange; +/** General type for filter agg */ +export type FilterAggConfigEditor = FilterAggProps; + +export type FilterAggConfigUnion = + | FilterAggConfigTerm + | FilterAggConfigRange + | FilterAggConfigBool + | FilterAggConfigExists; /** * Union type for filter aggregations