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] Add util for getting field names from KQL expression (elastic#1…
…83573) ## Summary Resolves elastic#180555. Adds a utility to kbn-es-query for getting the field names associated with a KQL expression. This utility already (mostly) existed in x-pack/plugins/observability_solution/apm/common/utils/get_kuery_fields.ts but didn't have test coverage for things like wildcards and nested fields. This also updates the utility to be a little more robust in checking the KQL node types. ### Checklist - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Matthew Kime <matt@mattki.me> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
53435ea
commit 564dec5
Showing
11 changed files
with
159 additions
and
117 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
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
84 changes: 84 additions & 0 deletions
84
packages/kbn-es-query/src/kuery/utils/get_kql_fields.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,84 @@ | ||
/* | ||
* 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 { getKqlFieldNamesFromExpression } from './get_kql_fields'; | ||
|
||
describe('getKqlFieldNames', () => { | ||
it('returns single kuery field', () => { | ||
const expression = 'service.name: my-service'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['service.name']); | ||
}); | ||
|
||
it('returns kuery fields with wildcard', () => { | ||
const expression = 'service.name: *'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['service.name']); | ||
}); | ||
|
||
it('returns multiple fields used AND operator', () => { | ||
const expression = 'service.name: my-service AND service.environment: production'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual([ | ||
'service.name', | ||
'service.environment', | ||
]); | ||
}); | ||
|
||
it('returns multiple kuery fields with OR operator', () => { | ||
const expression = 'network.carrier.mcc: test or child.id: 33'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['network.carrier.mcc', 'child.id']); | ||
}); | ||
|
||
it('returns multiple kuery fields with wildcard', () => { | ||
const expression = 'network.carrier.mcc:* or child.id: *'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['network.carrier.mcc', 'child.id']); | ||
}); | ||
|
||
it('returns single kuery fields with gt operator', () => { | ||
const expression = 'transaction.duration.aggregate > 10'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['transaction.duration.aggregate']); | ||
}); | ||
|
||
it('returns duplicate fields', () => { | ||
const expression = 'service.name: my-service and service.name: my-service and trace.id: trace'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual([ | ||
'service.name', | ||
'service.name', | ||
'trace.id', | ||
]); | ||
}); | ||
|
||
it('returns multiple fields with multiple logical operators', () => { | ||
const expression = | ||
'(service.name:opbeans-* OR service.name:kibana) and (service.environment:production)'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual([ | ||
'service.name', | ||
'service.name', | ||
'service.environment', | ||
]); | ||
}); | ||
|
||
it('returns nested fields', () => { | ||
const expression = 'user.names:{ first: "Alice" and last: "White" }'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['user.names']); | ||
}); | ||
|
||
it('returns wildcard fields', () => { | ||
const expression = 'server.*: kibana'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['server.*']); | ||
}); | ||
|
||
// _field_caps doesn't allow escaped wildcards, so for now this behavior is what we want | ||
it('returns escaped fields', () => { | ||
const expression = 'server.\\*: kibana'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual(['server.*']); | ||
}); | ||
|
||
it('do not return if kuery field is null', () => { | ||
const expression = 'opbean'; | ||
expect(getKqlFieldNamesFromExpression(expression)).toEqual([]); | ||
}); | ||
}); |
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 { fromKueryExpression, KueryNode } from '../../..'; | ||
import { nodeTypes } from '../node_types'; | ||
import { functions } from '../functions'; | ||
|
||
export function getKqlFieldNamesFromExpression(expression: string): string[] { | ||
const node = fromKueryExpression(expression); | ||
return getKqlFieldNames(node); | ||
} | ||
|
||
export function getKqlFieldNames(node: KueryNode): string[] { | ||
if (nodeTypes.function.isNode(node)) { | ||
if (functions.and.isNode(node) || functions.or.isNode(node)) { | ||
return node.arguments.reduce<string[]>((result, child) => { | ||
return result.concat(getKqlFieldNames(child)); | ||
}, []); | ||
} else if ( | ||
functions.not.isNode(node) || | ||
functions.exists.isNode(node) || | ||
functions.is.isNode(node) || | ||
functions.nested.isNode(node) || | ||
functions.range.isNode(node) | ||
) { | ||
// For each of these field types, we only need to look at the first argument to determine the fields | ||
const [fieldNode] = node.arguments; | ||
return getKqlFieldNames(fieldNode); | ||
} else { | ||
throw new Error(`KQL function ${node.function} not supported in getKqlFieldNames`); | ||
} | ||
} else if (nodeTypes.literal.isNode(node)) { | ||
if (node.value === null) return []; | ||
return [`${nodeTypes.literal.toElasticsearchQuery(node)}`]; | ||
} else if (nodeTypes.wildcard.isNode(node)) { | ||
return [nodeTypes.wildcard.toElasticsearchQuery(node)]; | ||
} else { | ||
throw new Error(`KQL node type ${node.type} not supported in getKqlFieldNames`); | ||
} | ||
} |
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
70 changes: 0 additions & 70 deletions
70
x-pack/plugins/observability_solution/apm/common/utils/get_kuery_fields.test.ts
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
x-pack/plugins/observability_solution/apm/common/utils/get_kuery_fields.ts
This file was deleted.
Oops, something went wrong.
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