[KQL] Define KqlContext type (#135273)

This commit is contained in:
Lukas Olson 2022-06-28 09:19:28 -07:00 committed by GitHub
parent 54d5cc48ea
commit c477a90eca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 36 additions and 19 deletions

View file

@ -10,7 +10,7 @@ import { JsonObject } from '@kbn/utility-types';
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { nodeTypes } from '../node_types';
import { KQLSyntaxError } from '../kuery_syntax_error';
import { KueryNode, KueryParseOptions, KueryQueryOptions } from '../types';
import type { KqlContext, KueryNode, KueryParseOptions, KueryQueryOptions } from '../types';
import { parse as parseKuery } from '../grammar';
import { DataViewBase } from '../..';
@ -68,7 +68,7 @@ export const toElasticsearchQuery = (
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context?: Record<string, any>
context?: KqlContext
): JsonObject => {
if (!node || !node.type || !nodeTypes[node.type]) {
return toElasticsearchQuery(nodeTypes.function.buildNode('and', []), indexPattern);

View file

@ -7,7 +7,8 @@
*/
import * as ast from '../ast';
import { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
export function buildNodeParams(children: KueryNode[]) {
return {
@ -19,7 +20,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
) {
const { filtersInMustClause } = config;
const children = node.arguments || [];

View file

@ -7,8 +7,9 @@
*/
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { DataViewFieldBase, DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { DataViewFieldBase, DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import * as literal from '../node_types/literal';
import type { KqlContext } from '../types';
export function buildNodeParams(fieldName: string) {
return {
@ -20,7 +21,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const {
arguments: [fieldNameArg],

View file

@ -12,10 +12,10 @@ import { getPhraseScript } from '../../filters';
import { getFields } from './utils/get_fields';
import { getTimeZoneFromSettings, getDataViewFieldSubtypeNested } from '../../utils';
import { getFullFieldNameNode } from './utils/get_full_field_name_node';
import { DataViewBase, KueryNode, DataViewFieldBase, KueryQueryOptions } from '../..';
import type { DataViewBase, KueryNode, DataViewFieldBase, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
import * as ast from '../ast';
import * as literal from '../node_types/literal';
import * as wildcard from '../node_types/wildcard';
@ -42,7 +42,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const {
arguments: [fieldNameArg, valueArg, isPhraseArg],

View file

@ -9,7 +9,8 @@
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as ast from '../ast';
import * as literal from '../node_types/literal';
import { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
export function buildNodeParams(path: any, child: any) {
const pathNode =
@ -23,7 +24,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const [path, child] = node.arguments;
const stringPath = ast.toElasticsearchQuery(path) as unknown as string;

View file

@ -8,7 +8,8 @@
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as ast from '../ast';
import { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
export function buildNodeParams(child: KueryNode) {
return {
@ -20,7 +21,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const [argument] = node.arguments;

View file

@ -8,7 +8,8 @@
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import * as ast from '../ast';
import { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
export function buildNodeParams(children: KueryNode[]) {
return {
@ -20,7 +21,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const children = node.arguments || [];

View file

@ -14,6 +14,7 @@ import { getFields } from './utils/get_fields';
import { getDataViewFieldSubtypeNested, getTimeZoneFromSettings } from '../../utils';
import { getFullFieldNameNode } from './utils/get_full_field_name_node';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { KqlContext } from '../types';
export function buildNodeParams(
fieldName: string,
@ -30,7 +31,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const [fieldNameArg, operatorArg, valueArg] = node.arguments;
const fullFieldNameArg = getFullFieldNameNode(

View file

@ -9,8 +9,9 @@
import _ from 'lodash';
import { functions } from '../functions';
import { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import { FunctionName, FunctionTypeBuildNode } from './types';
import type { DataViewBase, KueryNode, KueryQueryOptions } from '../..';
import type { FunctionName, FunctionTypeBuildNode } from './types';
import type { KqlContext } from '../types';
export function buildNode(functionName: FunctionName, ...args: any[]) {
const kueryFunction = functions[functionName];
@ -47,7 +48,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config?: KueryQueryOptions,
context?: Record<string, any>
context?: KqlContext
) {
const kueryFunction = functions[node.function as FunctionName];
return kueryFunction.toElasticsearchQuery(node, indexPattern, config, context);

View file

@ -47,3 +47,13 @@ export interface KueryQueryOptions {
*/
nestedIgnoreUnmapped?: boolean;
}
/** @public */
export interface KqlContext {
nested?: {
/**
* For nested queries, we pass along the path to the nested document so we can properly craft the query DSL.
*/
path: string;
};
}