Skip to content
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

Explicit namespaces for esQuery and esKuery #57172

Merged
Merged
91 changes: 44 additions & 47 deletions src/core/server/saved_objects/service/lib/filter_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import { get, set } from 'lodash';
import { SavedObjectsErrorHelpers } from './errors';
import { IndexMapping } from '../../mappings';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { esKuery } from '../../../../../plugins/data/server';
import { esKuery, KueryNode } from '../../../../../plugins/data/server';

const astFunctionType = ['is', 'range', 'nested'];

export const validateConvertFilterToKueryNode = (
allowedTypes: string[],
filter: string,
indexMapping: IndexMapping
): esKuery.KueryNode | undefined => {
): KueryNode | undefined => {
if (filter && filter.length > 0 && indexMapping) {
const filterKueryNode = esKuery.fromKueryExpression(filter);

Expand Down Expand Up @@ -59,7 +59,7 @@ export const validateConvertFilterToKueryNode = (

validationFilterKuery.forEach(item => {
const path: string[] = item.astPath.length === 0 ? [] : item.astPath.split('.');
const existingKueryNode: esKuery.KueryNode =
const existingKueryNode: KueryNode =
path.length === 0 ? filterKueryNode : get(filterKueryNode, path);
if (item.isSavedObjectAttr) {
existingKueryNode.arguments[0].value = existingKueryNode.arguments[0].value.split('.')[1];
Expand Down Expand Up @@ -95,7 +95,7 @@ interface ValidateFilterKueryNode {
}

interface ValidateFilterKueryNodeParams {
astFilter: esKuery.KueryNode;
astFilter: KueryNode;
types: string[];
indexMapping: IndexMapping;
hasNestedKey?: boolean;
Expand All @@ -114,50 +114,47 @@ export const validateFilterKueryNode = ({
path = 'arguments',
}: ValidateFilterKueryNodeParams): ValidateFilterKueryNode[] => {
let localNestedKeys: string | undefined;
return astFilter.arguments.reduce(
(kueryNode: string[], ast: esKuery.KueryNode, index: number) => {
if (hasNestedKey && ast.type === 'literal' && ast.value != null) {
localNestedKeys = ast.value;
}
if (ast.arguments) {
const myPath = `${path}.${index}`;
return [
...kueryNode,
...validateFilterKueryNode({
astFilter: ast,
return astFilter.arguments.reduce((kueryNode: string[], ast: KueryNode, index: number) => {
if (hasNestedKey && ast.type === 'literal' && ast.value != null) {
localNestedKeys = ast.value;
}
if (ast.arguments) {
const myPath = `${path}.${index}`;
return [
...kueryNode,
...validateFilterKueryNode({
astFilter: ast,
types,
indexMapping,
storeValue: ast.type === 'function' && astFunctionType.includes(ast.function),
path: `${myPath}.arguments`,
hasNestedKey: ast.type === 'function' && ast.function === 'nested',
nestedKeys: localNestedKeys,
}),
];
}
if (storeValue && index === 0) {
const splitPath = path.split('.');
return [
...kueryNode,
{
astPath: splitPath.slice(0, splitPath.length - 1).join('.'),
error: hasFilterKeyError(
nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
types,
indexMapping,
storeValue: ast.type === 'function' && astFunctionType.includes(ast.function),
path: `${myPath}.arguments`,
hasNestedKey: ast.type === 'function' && ast.function === 'nested',
nestedKeys: localNestedKeys,
}),
];
}
if (storeValue && index === 0) {
const splitPath = path.split('.');
return [
...kueryNode,
{
astPath: splitPath.slice(0, splitPath.length - 1).join('.'),
error: hasFilterKeyError(
nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
types,
indexMapping
),
isSavedObjectAttr: isSavedObjectAttr(
nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
indexMapping
),
key: nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
type: getType(nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value),
},
];
}
return kueryNode;
},
[]
);
indexMapping
),
isSavedObjectAttr: isSavedObjectAttr(
nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
indexMapping
),
key: nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value,
type: getType(nestedKeys != null ? `${nestedKeys}.${ast.value}` : ast.value),
},
];
}
return kueryNode;
}, []);
};

const getType = (key: string | undefined | null) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { esKuery } from '../../../../../../plugins/data/server';
import { esKuery, KueryNode } from '../../../../../../plugins/data/server';

import { getRootPropertiesObjects, IndexMapping } from '../../../mappings';
import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry';
Expand Down Expand Up @@ -96,7 +96,7 @@ interface QueryParams {
searchFields?: string[];
defaultSearchOperator?: string;
hasReference?: HasReferenceQueryParams;
kueryNode?: esKuery.KueryNode;
kueryNode?: KueryNode;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IndexMapping } from '../../../mappings';
import { getQueryParams } from './query_params';
import { getSortingParams } from './sorting_params';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { esKuery } from '../../../../../../plugins/data/server';
import { KueryNode } from '../../../../../../plugins/data/server';
import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry';

interface GetSearchDslOptions {
Expand All @@ -38,7 +38,7 @@ interface GetSearchDslOptions {
type: string;
id: string;
};
kueryNode?: esKuery.KueryNode;
kueryNode?: KueryNode;
}

export function getSearchDsl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import { I18nStart, SavedObjectsStart, IUiSettingsClient, CoreStart } from 'src/core/public';
import { createGetterSetter } from '../../../../plugins/kibana_utils/public';
import { DataPublicPluginStart, FieldFormatsStart } from '../../../../plugins/data/public';
import { DataPublicPluginStart } from '../../../../plugins/data/public';

export const [getUISettings, setUISettings] = createGetterSetter<IUiSettingsClient>('UISettings');

export const [getFieldFormats, setFieldFormats] = createGetterSetter<FieldFormatsStart>(
'FieldFormats'
);
export const [getFieldFormats, setFieldFormats] = createGetterSetter<
DataPublicPluginStart['fieldFormats']
>('FieldFormats');

export const [getSavedObjectsClient, setSavedObjectsClient] = createGetterSetter<SavedObjectsStart>(
'SavedObjectsClient'
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/common/es_query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import * as esQuery from './es_query';
export * from './es_query';
import * as esFilters from './filters';
import * as esKuery from './kuery';
export * from './kuery';

export { esFilters, esQuery, esKuery };
export { esFilters };
3 changes: 2 additions & 1 deletion src/plugins/data/common/es_query/kuery/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

import { nodeTypes } from '../node_types/index';
import { KQLSyntaxError } from '../kuery_syntax_error';
import { KueryNode, JsonObject, DslQuery, KueryParseOptions } from '../types';
import { KueryNode, DslQuery, KueryParseOptions } from '../types';
import { IIndexPattern } from '../../../index_patterns/types';

// @ts-ignore
import { parse as parseKuery } from './_generated_/kuery';
import { JsonObject } from '../../../../../kibana_utils/public';

const fromExpression = (
expression: string | DslQuery,
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/data/common/es_query/kuery/kuery_syntax_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
import { repeat } from 'lodash';
import { i18n } from '@kbn/i18n';

const endOfInputText = i18n.translate('data.common.esQuery.kql.errors.endOfInputText', {
const endOfInputText = i18n.translate('data.common.kql.errors.endOfInputText', {
defaultMessage: 'end of input',
});

const grammarRuleTranslations: Record<string, string> = {
fieldName: i18n.translate('data.common.esQuery.kql.errors.fieldNameText', {
fieldName: i18n.translate('data.common.kql.errors.fieldNameText', {
defaultMessage: 'field name',
}),
value: i18n.translate('data.common.esQuery.kql.errors.valueText', {
value: i18n.translate('data.common.kql.errors.valueText', {
defaultMessage: 'value',
}),
literal: i18n.translate('data.common.esQuery.kql.errors.literalText', {
literal: i18n.translate('data.common.kql.errors.literalText', {
defaultMessage: 'literal',
}),
whitespace: i18n.translate('data.common.esQuery.kql.errors.whitespaceText', {
whitespace: i18n.translate('data.common.kql.errors.whitespaceText', {
defaultMessage: 'whitespace',
}),
};
Expand All @@ -61,7 +61,7 @@ export class KQLSyntaxError extends Error {

const translatedExpectationText = translatedExpectations.join(', ');

message = i18n.translate('data.common.esQuery.kql.errors.syntaxError', {
message = i18n.translate('data.common.kql.errors.syntaxError', {
defaultMessage: 'Expected {expectedList} but {foundInput} found.',
values: {
expectedList: translatedExpectationText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import _ from 'lodash';
import { functions } from '../functions';
import { IIndexPattern } from '../../..';
import { FunctionName, FunctionTypeBuildNode } from './types';
import { JsonValue } from '..';
import { JsonValue } from '../../../../../kibana_utils/public';

export function buildNode(functionName: FunctionName, ...args: any[]) {
const kueryFunction = functions[functionName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import _ from 'lodash';
import * as ast from '../ast';
import { nodeTypes } from '../node_types';
import { NamedArgTypeBuildNode } from './types';
import { JsonObject } from '../types';
import { JsonObject } from '../../../../../kibana_utils/public';

export function buildNode(name: string, value: any): NamedArgTypeBuildNode {
const argumentNode =
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/es_query/kuery/node_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

import { IIndexPattern } from '../../../index_patterns';
import { JsonValue, KueryNode } from '..';
import { JsonValue } from '../../../../../kibana_utils/public';
import { KueryNode } from '..';

export type FunctionName =
| 'is'
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/data/common/es_query/kuery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,3 @@ export interface KueryParseOptions {
}

export { nodeTypes } from './node_types';

export type JsonArray = JsonValue[];
export type JsonValue = null | boolean | number | string | JsonObject | JsonArray;

export interface JsonObject {
[key: string]: JsonValue;
}
37 changes: 34 additions & 3 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,44 @@

import { PluginInitializerContext } from '../../../core/public';

/*
* esQuery and esKuery helper namespaces:
*/

import {
doesKueryExpressionHaveLuceneSyntaxError,
fromKueryExpression,
toElasticsearchQuery,
nodeTypes,
buildEsQuery,
getEsQueryConfig,
buildQueryFromFilters,
luceneStringToDsl,
decorateQuery,
} from '../common';

export const esKuery = {
nodeTypes,
doesKueryExpressionHaveLuceneSyntaxError,
fromKueryExpression,
toElasticsearchQuery,
};

export const esQuery = {
buildEsQuery,
getEsQueryConfig,
buildQueryFromFilters,
luceneStringToDsl,
decorateQuery,
};

/*
* Field Formatters helper namespace:
*/

import {
FieldFormat,
FieldFormatsRegistry, // exported only for tests. Consider mock.
FieldFormatsRegistry,
DEFAULT_CONVERTER_COLOR,
HTML_CONTEXT_TYPE,
TEXT_CONTEXT_TYPE,
Expand Down Expand Up @@ -84,6 +115,7 @@ export function plugin(initializerContext: PluginInitializerContext) {
export { IRequestTypesMap, IResponseTypesMap } from './search';
export * from './types';
export {
EsQueryConfig,
// index patterns
IIndexPattern,
IFieldType,
Expand Down Expand Up @@ -122,8 +154,7 @@ export * from './ui';
export {
// es query
esFilters,
esKuery,
esQuery,
KueryNode,
// index patterns
isFilterable,
// kbn field types
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/data/public/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ import _ from 'lodash';
import { normalizeSortRequest } from './normalize_sort_request';
import { filterDocvalueFields } from './filter_docvalue_fields';
import { fieldWildcardFilter } from '../../../../kibana_utils/public';
import { esFilters, esQuery, SearchRequest } from '../..';

import { esFilters, SearchRequest } from '../..';

import { SearchSourceOptions, SearchSourceFields } from './types';
import { fetchSoon, FetchOptions, RequestFailure } from '../fetch';

import { getSearchService, getUiSettings, getInjectedMetadata } from '../../services';
import { getHighlightRequest } from '../../../common';
import { getEsQueryConfig, buildEsQuery } from '../../../common/es_query';
import { getHighlightRequest } from '../../../common/field_formats';

export type ISearchSource = Pick<SearchSource, keyof SearchSource>;

Expand Down Expand Up @@ -379,8 +382,8 @@ export class SearchSource {
_.set(body, '_source.includes', remainingFields);
}

const esQueryConfigs = esQuery.getEsQueryConfig(getUiSettings());
body.query = esQuery.buildEsQuery(index, query, filters, esQueryConfigs);
const esQueryConfigs = getEsQueryConfig(getUiSettings());
body.query = buildEsQuery(index, query, filters, esQueryConfigs);

if (highlightAll && body.query) {
body.highlight = getHighlightRequest(body.query, getUiSettings().get('doc_table:highlight'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import {
Query,
PersistedLog,
getQueryLog,
esKuery,
} from '../..';
import { useKibana, toMountPoint } from '../../../../kibana_react/public';
import { QueryStringInput } from './query_string_input';
import { doesKueryExpressionHaveLuceneSyntaxError } from '../../../common';

interface Props {
query?: Query;
Expand Down Expand Up @@ -298,7 +298,7 @@ function QueryBarTopRowUI(props: Props) {
language === 'kuery' &&
typeof query === 'string' &&
(!storage || !storage.get('kibana.luceneSyntaxWarningOptOut')) &&
esKuery.doesKueryExpressionHaveLuceneSyntaxError(query)
doesKueryExpressionHaveLuceneSyntaxError(query)
) {
const toast = notifications!.toasts.addWarning({
title: intl.formatMessage({
Expand Down
Loading