forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding kibana filter expression functions (elastic#94069)
- Loading branch information
Showing
26 changed files
with
851 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/plugins/data/common/search/expressions/exists_filter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 { createMockContext } from '../../../../expressions/common'; | ||
import { functionWrapper } from './utils'; | ||
import { existsFilterFunction } from './exists_filter'; | ||
|
||
describe('interpreter/functions#existsFilter', () => { | ||
const fn = functionWrapper(existsFilterFunction); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(null, { field: { spec: { name: 'test' } } }, createMockContext()); | ||
expect(actual).toMatchInlineSnapshot(` | ||
Object { | ||
"exists": Object { | ||
"field": "test", | ||
}, | ||
"meta": Object { | ||
"alias": null, | ||
"disabled": false, | ||
"index": undefined, | ||
"negate": false, | ||
}, | ||
"type": "kibana_filter", | ||
} | ||
`); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/plugins/data/common/search/expressions/exists_filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; | ||
import { KibanaField, KibanaFilter } from './kibana_context_type'; | ||
import { buildFilter, FILTERS } from '../../es_query/filters'; | ||
import { IndexPattern } from '../../index_patterns/index_patterns'; | ||
|
||
interface Arguments { | ||
field: KibanaField; | ||
negate?: boolean; | ||
} | ||
|
||
export type ExpressionFunctionExistsFilter = ExpressionFunctionDefinition< | ||
'existsFilter', | ||
null, | ||
Arguments, | ||
KibanaFilter | ||
>; | ||
|
||
export const existsFilterFunction: ExpressionFunctionExistsFilter = { | ||
name: 'existsFilter', | ||
type: 'kibana_filter', | ||
inputTypes: ['null'], | ||
help: i18n.translate('data.search.functions.existsFilter.help', { | ||
defaultMessage: 'Create kibana exists filter', | ||
}), | ||
args: { | ||
field: { | ||
types: ['kibana_field'], | ||
required: true, | ||
help: i18n.translate('data.search.functions.existsFilter.field.help', { | ||
defaultMessage: 'Specify the field you want to filter on. Use `field` function.', | ||
}), | ||
}, | ||
negate: { | ||
types: ['boolean'], | ||
default: false, | ||
help: i18n.translate('data.search.functions.existsFilter.negate.help', { | ||
defaultMessage: 'Should the filter be negated.', | ||
}), | ||
}, | ||
}, | ||
|
||
fn(input, args) { | ||
return { | ||
type: 'kibana_filter', | ||
...buildFilter( | ||
({} as any) as IndexPattern, | ||
args.field.spec, | ||
FILTERS.EXISTS, | ||
args.negate || false, | ||
false, | ||
{}, | ||
null | ||
), | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { ExecutionContext } from 'src/plugins/expressions/common'; | ||
import { functionWrapper } from './utils'; | ||
import { fieldFunction } from './field'; | ||
|
||
describe('interpreter/functions#field', () => { | ||
const fn = functionWrapper(fieldFunction); | ||
let context: ExecutionContext; | ||
|
||
beforeEach(() => { | ||
context = { | ||
getSearchContext: () => ({}), | ||
getSearchSessionId: () => undefined, | ||
types: {}, | ||
variables: {}, | ||
abortSignal: {} as any, | ||
inspectorAdapters: {} as any, | ||
}; | ||
}); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(null, { name: 'test', type: 'number' }, context); | ||
expect(actual).toMatchInlineSnapshot(` | ||
Object { | ||
"spec": Object { | ||
"name": "test", | ||
"script": undefined, | ||
"scripted": false, | ||
"type": "number", | ||
}, | ||
"type": "kibana_field", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; | ||
import { KibanaField } from './kibana_context_type'; | ||
|
||
interface Arguments { | ||
name: string; | ||
type: string; | ||
script?: string; | ||
} | ||
|
||
export type ExpressionFunctionField = ExpressionFunctionDefinition< | ||
'field', | ||
null, | ||
Arguments, | ||
KibanaField | ||
>; | ||
|
||
export const fieldFunction: ExpressionFunctionField = { | ||
name: 'field', | ||
type: 'kibana_field', | ||
inputTypes: ['null'], | ||
help: i18n.translate('data.search.functions.field.help', { | ||
defaultMessage: 'Create a Kibana field.', | ||
}), | ||
args: { | ||
name: { | ||
types: ['string'], | ||
required: true, | ||
help: i18n.translate('data.search.functions.field.name.help', { | ||
defaultMessage: 'Name of the field', | ||
}), | ||
}, | ||
type: { | ||
types: ['string'], | ||
required: true, | ||
help: i18n.translate('data.search.functions.field.type.help', { | ||
defaultMessage: 'Type of the field', | ||
}), | ||
}, | ||
script: { | ||
types: ['string'], | ||
help: i18n.translate('data.search.functions.field.script.help', { | ||
defaultMessage: 'A field script, in case the field is scripted.', | ||
}), | ||
}, | ||
}, | ||
|
||
fn(input, args) { | ||
return { | ||
type: 'kibana_field', | ||
spec: { | ||
name: args.name, | ||
type: args.type, | ||
scripted: args.script ? true : false, | ||
script: args.script, | ||
}, | ||
} as KibanaField; | ||
}, | ||
}; |
47 changes: 47 additions & 0 deletions
47
src/plugins/data/common/search/expressions/filters_to_ast.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 { filtersToAst } from './filters_to_ast'; | ||
|
||
describe('interpreter/functions#filtersToAst', () => { | ||
const normalFilter = { | ||
meta: { negate: false, alias: '', disabled: false }, | ||
query: { test: 'something' }, | ||
}; | ||
const negatedFilter = { | ||
meta: { negate: true, alias: '', disabled: false }, | ||
query: { test: 'something' }, | ||
}; | ||
|
||
it('converts a list of filters to an expression AST node', () => { | ||
const actual = filtersToAst([normalFilter, negatedFilter]); | ||
expect(actual).toHaveLength(2); | ||
expect(actual[0].functions[0]).toHaveProperty('name', 'kibanaFilter'); | ||
expect(actual[0].functions[0].arguments).toMatchInlineSnapshot(` | ||
Object { | ||
"negate": Array [ | ||
false, | ||
], | ||
"query": Array [ | ||
"{\\"query\\":{\\"test\\":\\"something\\"}}", | ||
], | ||
} | ||
`); | ||
expect(actual[1].functions[0]).toHaveProperty('name', 'kibanaFilter'); | ||
expect(actual[1].functions[0].arguments).toMatchInlineSnapshot(` | ||
Object { | ||
"negate": Array [ | ||
true, | ||
], | ||
"query": Array [ | ||
"{\\"query\\":{\\"test\\":\\"something\\"}}", | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/plugins/data/common/search/expressions/filters_to_ast.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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 { buildExpression, buildExpressionFunction } from '../../../../expressions/common'; | ||
import { Filter } from '../../es_query/filters'; | ||
import { ExpressionFunctionKibanaFilter } from './kibana_filter'; | ||
|
||
export const filtersToAst = (filters: Filter[] | Filter) => { | ||
return (Array.isArray(filters) ? filters : [filters]).map((filter) => { | ||
const { meta, $state, ...restOfFilter } = filter; | ||
return buildExpression([ | ||
buildExpressionFunction<ExpressionFunctionKibanaFilter>('kibanaFilter', { | ||
query: JSON.stringify(restOfFilter), | ||
negate: filter.meta.negate, | ||
}), | ||
]); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/plugins/data/common/search/expressions/kibana_filter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 { createMockContext } from '../../../../expressions/common'; | ||
import { functionWrapper } from './utils'; | ||
import { kibanaFilterFunction } from './kibana_filter'; | ||
|
||
describe('interpreter/functions#kibanaFilter', () => { | ||
const fn = functionWrapper(kibanaFilterFunction); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(null, { query: '{ "name": "test" }' }, createMockContext()); | ||
expect(actual).toMatchInlineSnapshot(` | ||
Object { | ||
"meta": Object { | ||
"alias": "", | ||
"disabled": false, | ||
"negate": false, | ||
}, | ||
"name": "test", | ||
"type": "kibana_filter", | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.