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.
kql, lucene and timerange functions (elastic#93043)
- Loading branch information
Showing
20 changed files
with
416 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27843,4 +27843,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 |
---|---|---|
|
@@ -19470,4 +19470,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": "kuery", | ||
"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: 'kuery', | ||
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), | ||
}; | ||
}, | ||
}; |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
/* | ||
* 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 for lucene queies', () => { | ||
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"}'], | ||
}); | ||
}); | ||
|
||
it('returns an object with the correct structure for kql queies', () => { | ||
const actual = queryToAst({ language: 'kuery', query: 'country:US' }); | ||
expect(actual).toHaveProperty('functions'); | ||
expect(actual.functions[0]).toHaveProperty('name', 'kql'); | ||
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 === 'kuery') { | ||
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.