-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding kibana filter expression functions #94069
Merged
ppisljar
merged 9 commits into
elastic:master
from
ppisljar:expressions/filter_functions
Mar 23, 2021
+851
−10
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6fed6da
adding kibanaFilter, rangeFilter, existsFilter and phraseFilter expre…
ppisljar e7e0020
fixes
ppisljar 3daaa59
fixes
ppisljar 0fbb0ff
fixes
ppisljar 7ec0933
fixes
ppisljar 7bbbe60
Refactor filters unboxing
dokmic 57b3d71
fixes
ppisljar 9722fb9
Apply suggestions from code review
ppisljar bdbc1d1
improving tests
ppisljar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
adding kibanaFilter, rangeFilter, existsFilter and phraseFilter expre…
…ssion functions
- Loading branch information
commit 6fed6da7a0686f46d8ec7dca560c23610892112f
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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
/* | ||
* 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 { existsFilterFunction } from './exists_filter'; | ||
|
||
describe('interpreter/functions#existsFilter', () => { | ||
const fn = functionWrapper(existsFilterFunction); | ||
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, { field: { spec: { name: 'test' } } }, context); | ||
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', | ||
ppisljar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
}, | ||
}, | ||
|
||
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 kibana_field', | ||
ppisljar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
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: 'script in case the field is scripted', | ||
ppisljar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}), | ||
}, | ||
}, | ||
|
||
fn(input, args) { | ||
return { | ||
type: 'kibana_field', | ||
spec: { | ||
name: args.name, | ||
type: args.type, | ||
scripted: args.script ? true : false, | ||
script: args.script, | ||
}, | ||
} as KibanaField; | ||
}, | ||
}; |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
/* | ||
* 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('returns an object with the correct structure', () => { | ||
ppisljar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
"query": Array [ | ||
Object { | ||
"test": "something", | ||
}, | ||
], | ||
} | ||
`); | ||
expect(actual[1].functions[0]).toHaveProperty('name', 'kibanaFilter'); | ||
expect(actual[1].functions[0].arguments).toMatchInlineSnapshot(` | ||
Object { | ||
"query": Array [ | ||
Object { | ||
"test": "something", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
/* | ||
* 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) => { | ||
return buildExpression([ | ||
buildExpressionFunction<ExpressionFunctionKibanaFilter>('kibanaFilter', { | ||
query: filter.query, | ||
}), | ||
]); | ||
}); | ||
}; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this is copy-paste from similar tests in this plugin, but I've seen it a lot and wanted to suggest a small improvement. It is better to not create global module variables, like
context
here and not usebeforeEach
utility, which is specific to Jest. This can be done like so:Simply
setup()
function that creates everything needed for each test case every time.