Skip to content

Commit

Permalink
[KQL] Define KqlContext type
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Jun 27, 2022
1 parent 4c8ff10 commit 7612f03
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/kbn-es-query/src/kuery/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '../..';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 || [];
Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,7 +21,7 @@ export function toElasticsearchQuery(
node: KueryNode,
indexPattern?: DataViewBase,
config: KueryQueryOptions = {},
context: Record<string, any> = {}
context: KqlContext = {}
): estypes.QueryDslQueryContainer {
const {
arguments: [fieldNameArg],
Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-es-query/src/kuery/functions/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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],
Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/nested.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/not.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-es-query/src/kuery/functions/or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 || [];

Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-query/src/kuery/functions/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-es-query/src/kuery/node_types/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions packages/kbn-es-query/src/kuery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

0 comments on commit 7612f03

Please sign in to comment.