-
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
kql, lucene and timerange functions #93043
Merged
ppisljar
merged 15 commits into
elastic:master
from
ppisljar:expressions/kibana_context
Mar 17, 2021
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b0d67d3
expressions kql/lucene/timerange functions
ppisljar 4bc7497
update tests
ppisljar 3fd606b
Merge branch 'master' into expressions/kibana_context
ppisljar ad76bb3
updating docs
ppisljar 605307a
Merge branch 'master' into expressions/kibana_context
ppisljar 07373da
register on server
ppisljar 44bf1c8
Merge branch 'master' into expressions/kibana_context
ppisljar 66b76e2
removing docs changes
ppisljar 40bbdd3
relative import ?
ppisljar 6a7d780
updating docs
ppisljar 86de372
fixing
ppisljar 7e7c1d2
Merge branch 'master' into expressions/kibana_context
ppisljar 3de7093
updating tests
ppisljar 8776573
fixing kql issue
ppisljar 0425a5a
kql to kuery
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27594,4 +27594,4 @@ | |
} | ||
] | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19293,4 +19293,4 @@ | |
} | ||
] | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -33883,4 +33883,4 @@ | |
} | ||
] | ||
} | ||
} | ||
} |
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
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,43 @@ | ||
/* | ||
* 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 { ExpressionValueSearchContext } from './kibana_context_type'; | ||
import { functionWrapper } from './utils'; | ||
import { kqlFunction } from './kql'; | ||
|
||
describe('interpreter/functions#kql', () => { | ||
const fn = functionWrapper(kqlFunction); | ||
let input: Partial<ExpressionValueSearchContext>; | ||
let context: ExecutionContext; | ||
|
||
beforeEach(() => { | ||
input = { timeRange: { from: '0', to: '1' } }; | ||
context = { | ||
getSearchContext: () => ({}), | ||
getSearchSessionId: () => undefined, | ||
types: {}, | ||
variables: {}, | ||
abortSignal: {} as any, | ||
inspectorAdapters: {} as any, | ||
}; | ||
}); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(input, { q: 'test' }, context); | ||
expect(actual).toMatchInlineSnapshot( | ||
` | ||
Object { | ||
"language": "kql", | ||
"query": "test", | ||
"type": "kibana_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 { KibanaQueryOutput } from './kibana_context_type'; | ||
|
||
interface Arguments { | ||
q: string; | ||
} | ||
|
||
export type ExpressionFunctionKql = ExpressionFunctionDefinition< | ||
'kql', | ||
null, | ||
Arguments, | ||
KibanaQueryOutput | ||
>; | ||
|
||
export const kqlFunction: ExpressionFunctionKql = { | ||
name: 'kql', | ||
type: 'kibana_query', | ||
inputTypes: ['null'], | ||
help: i18n.translate('data.search.functions.kql.help', { | ||
defaultMessage: 'Create kibana kql query', | ||
}), | ||
args: { | ||
q: { | ||
types: ['string'], | ||
required: true, | ||
aliases: ['query', '_'], | ||
help: i18n.translate('data.search.functions.kql.q.help', { | ||
defaultMessage: 'Specify Kibana KQL free form text query', | ||
}), | ||
}, | ||
}, | ||
|
||
fn(input, args) { | ||
return { | ||
type: 'kibana_query', | ||
language: 'kql', | ||
query: args.q, | ||
}; | ||
}, | ||
}; |
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,43 @@ | ||
/* | ||
* 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 { ExpressionValueSearchContext } from './kibana_context_type'; | ||
import { functionWrapper } from './utils'; | ||
import { luceneFunction } from './lucene'; | ||
|
||
describe('interpreter/functions#lucene', () => { | ||
const fn = functionWrapper(luceneFunction); | ||
let input: Partial<ExpressionValueSearchContext>; | ||
let context: ExecutionContext; | ||
|
||
beforeEach(() => { | ||
input = { timeRange: { from: '0', to: '1' } }; | ||
context = { | ||
getSearchContext: () => ({}), | ||
getSearchSessionId: () => undefined, | ||
types: {}, | ||
variables: {}, | ||
abortSignal: {} as any, | ||
inspectorAdapters: {} as any, | ||
}; | ||
}); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(input, { q: '{ "test": 1 }' }, context); | ||
expect(actual).toMatchInlineSnapshot(` | ||
Object { | ||
"language": "lucene", | ||
"query": Object { | ||
"test": 1, | ||
}, | ||
"type": "kibana_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 { KibanaQueryOutput } from './kibana_context_type'; | ||
|
||
interface Arguments { | ||
q: string; | ||
} | ||
|
||
export type ExpressionFunctionLucene = ExpressionFunctionDefinition< | ||
'lucene', | ||
null, | ||
Arguments, | ||
KibanaQueryOutput | ||
>; | ||
|
||
export const luceneFunction: ExpressionFunctionLucene = { | ||
name: 'lucene', | ||
type: 'kibana_query', | ||
inputTypes: ['null'], | ||
help: i18n.translate('data.search.functions.lucene.help', { | ||
defaultMessage: 'Create kibana lucene query', | ||
}), | ||
args: { | ||
q: { | ||
types: ['string'], | ||
required: true, | ||
aliases: ['query', '_'], | ||
help: i18n.translate('data.search.functions.lucene.q.help', { | ||
defaultMessage: 'Specify Lucene free form text query', | ||
}), | ||
}, | ||
}, | ||
|
||
fn(input, args) { | ||
return { | ||
type: 'kibana_query', | ||
language: 'lucene', | ||
query: JSON.parse(args.q), | ||
}; | ||
}, | ||
}; |
20 changes: 20 additions & 0 deletions
20
src/plugins/data/common/search/expressions/query_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,20 @@ | ||
/* | ||
* 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 { queryToAst } from './query_to_ast'; | ||
|
||
describe('queryToAst', () => { | ||
it('returns an object with the correct structure', () => { | ||
const actual = queryToAst({ language: 'lucene', query: { country: 'US' } }); | ||
expect(actual).toHaveProperty('functions'); | ||
expect(actual.functions[0]).toHaveProperty('name', 'lucene'); | ||
expect(actual.functions[0]).toHaveProperty('arguments', { | ||
q: ['{"country":"US"}'], | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/plugins/data/common/search/expressions/query_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 { Query } from '../../query'; | ||
import { ExpressionFunctionKql } from './kql'; | ||
import { ExpressionFunctionLucene } from './lucene'; | ||
|
||
export const queryToAst = (query: Query) => { | ||
if (query.language === 'kql') { | ||
return buildExpression([ | ||
buildExpressionFunction<ExpressionFunctionKql>('kql', { q: query.query as string }), | ||
]); | ||
} | ||
return buildExpression([ | ||
buildExpressionFunction<ExpressionFunctionLucene>('lucene', { q: JSON.stringify(query.query) }), | ||
]); | ||
}; |
44 changes: 44 additions & 0 deletions
44
src/plugins/data/common/search/expressions/timerange.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,44 @@ | ||
/* | ||
* 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 { ExpressionValueSearchContext } from './kibana_context_type'; | ||
import { functionWrapper } from './utils'; | ||
import { kibanaTimerangeFunction } from './timerange'; | ||
|
||
describe('interpreter/functions#timerange', () => { | ||
const fn = functionWrapper(kibanaTimerangeFunction); | ||
let input: Partial<ExpressionValueSearchContext>; | ||
let context: ExecutionContext; | ||
|
||
beforeEach(() => { | ||
input = { timeRange: { from: '0', to: '1' } }; | ||
context = { | ||
getSearchContext: () => ({}), | ||
getSearchSessionId: () => undefined, | ||
types: {}, | ||
variables: {}, | ||
abortSignal: {} as any, | ||
inspectorAdapters: {} as any, | ||
}; | ||
}); | ||
|
||
it('returns an object with the correct structure', () => { | ||
const actual = fn(input, { from: 'now', to: 'now-7d', mode: 'absolute' }, context); | ||
expect(actual).toMatchInlineSnapshot( | ||
` | ||
Object { | ||
"from": "now", | ||
"mode": "absolute", | ||
"to": "now-7d", | ||
"type": "timerange", | ||
} | ||
` | ||
); | ||
}); | ||
}); |
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 guess these two type changes are breaking changes, so we need a breaking change note.
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.
as we never store the expression (except in canvas), nor expose expression to the user (except in canvas) and this function is not used in canvas I think we are good