Skip to content

Commit

Permalink
FieldBase
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza K committed Jun 23, 2021
1 parent adc6234 commit f8e7e15
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* Side Public License, v 1.
*/

import { IFieldType } from '../../index_patterns';
import { Filter } from '../filters';
import { IndexPatternBase } from './types';
import { FieldBase, IndexPatternBase } from './types';

/*
* TODO: We should base this on something better than `filter.meta.key`. We should probably modify
Expand All @@ -26,5 +25,5 @@ export function filterMatchesIndex(filter: Filter, indexPattern?: IndexPatternBa
return filter.meta.index === indexPattern.id;
}

return indexPattern.fields.some((field: IFieldType) => field.name === filter.meta.key);
return indexPattern.fields.some((field) => field.name === filter.meta.key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import { handleNestedFilter } from './handle_nested_filter';
import { fields } from '../../index_patterns/mocks';
import { buildPhraseFilter, buildQueryFilter } from '../filters';
import { IndexPatternBase } from './types';
import { IFieldType } from '../../index_patterns';
import { IndexPatternBase, FieldBase } from './types';

describe('handleNestedFilter', function () {
const indexPattern: IndexPatternBase = {
Expand Down Expand Up @@ -46,7 +45,7 @@ describe('handleNestedFilter', function () {

it('should return filter untouched if it does not target a field from the given index pattern', () => {
const field = { ...getField('extension'), name: 'notarealfield' };
const filter = buildPhraseFilter(field as IFieldType, 'jpg', indexPattern);
const filter = buildPhraseFilter(field, 'jpg', indexPattern);
const result = handleNestedFilter(filter, indexPattern);
expect(result).toBe(filter);
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/es_query/es_query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export { buildQueryFromFilters } from './from_filters';
export { luceneStringToDsl } from './lucene_string_to_dsl';
export { decorateQuery } from './decorate_query';
export { getEsQueryConfig } from './get_es_query_config';
export { IndexPatternBase } from './types';
export { IndexPatternBase, FieldBase, IFieldSubType } from './types';
24 changes: 22 additions & 2 deletions src/plugins/data/common/es_query/es_query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
* Side Public License, v 1.
*/

import { IFieldType } from '../../index_patterns';
import type { estypes } from '@elastic/elasticsearch';

export interface IFieldSubType {
multi?: { parent: string };
nested?: { path: string };
}
export interface FieldBase {
name: string;
type: string;
subType?: IFieldSubType;
/**
* Scripted field painless script
*/
script?: string;
/**
* Scripted field langauge
* Painless is the only valid scripted field language
*/
lang?: estypes.ScriptLanguage;
scripted?: boolean;
}

export interface IndexPatternBase {
fields: IFieldType[];
fields: FieldBase[];
id?: string;
}
7 changes: 4 additions & 3 deletions src/plugins/data/common/es_query/filters/build_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import { IFieldType, IndexPatternBase } from '../..';
import { FieldBase, IndexPatternBase } from '../..';

import {
Filter,
FILTERS,
Expand All @@ -20,7 +21,7 @@ import {

export function buildFilter(
indexPattern: IndexPatternBase,
field: IFieldType,
field: FieldBase,
type: FILTERS,
negate: boolean,
disabled: boolean,
Expand Down Expand Up @@ -60,7 +61,7 @@ export function buildCustomFilter(

function buildBaseFilter(
indexPattern: IndexPatternBase,
field: IFieldType,
field: FieldBase,
type: FILTERS,
params: any
): Filter {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/es_query/filters/exists_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { Filter, FilterMeta } from './meta_filter';
import { IFieldType } from '../../index_patterns';
import { FieldBase } from '../../index_patterns';
import { IndexPatternBase } from '..';

export type ExistsFilterMeta = FilterMeta;
Expand All @@ -27,7 +27,7 @@ export const getExistsFilterField = (filter: ExistsFilter) => {
return filter.exists && filter.exists.field;
};

export const buildExistsFilter = (field: IFieldType, indexPattern: IndexPatternBase) => {
export const buildExistsFilter = (field: FieldBase, indexPattern: IndexPatternBase) => {
return {
meta: {
index: indexPattern.id,
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/common/es_query/filters/phrase_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import type { estypes } from '@elastic/elasticsearch';
import { get, isPlainObject } from 'lodash';
import { Filter, FilterMeta } from './meta_filter';
import { IFieldType } from '../../index_patterns';
import { FieldBase } from '../../index_patterns';
import { IndexPatternBase } from '..';

export type PhraseFilterMeta = FilterMeta & {
Expand Down Expand Up @@ -59,7 +59,7 @@ export const getPhraseFilterValue = (filter: PhraseFilter): PhraseFilterValue =>
};

export const buildPhraseFilter = (
field: IFieldType,
field: FieldBase,
value: any,
indexPattern: IndexPatternBase
): PhraseFilter => {
Expand All @@ -82,7 +82,7 @@ export const buildPhraseFilter = (
}
};

export const getPhraseScript = (field: IFieldType, value: string) => {
export const getPhraseScript = (field: FieldBase, value: string) => {
const convertedValue = getConvertedValueForField(field, value);
const script = buildInlineScriptForPhraseFilter(field);

Expand All @@ -106,7 +106,7 @@ export const getPhraseScript = (field: IFieldType, value: string) => {
* https://github.com/elastic/elasticsearch/issues/20941
* https://github.com/elastic/elasticsearch/pull/22201
**/
export const getConvertedValueForField = (field: IFieldType, value: any) => {
export const getConvertedValueForField = (field: FieldBase, value: any) => {
if (typeof value !== 'boolean' && field.type === 'boolean') {
if ([1, 'true'].includes(value)) {
return true;
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/data/common/es_query/filters/phrases_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import { Filter, FilterMeta } from './meta_filter';
import { getPhraseScript } from './phrase_filter';
import { FILTERS } from './index';
import { IFieldType } from '../../index_patterns';
import { IndexPatternBase } from '../es_query';
import { FieldBase, IndexPatternBase } from '../es_query';

export type PhrasesFilterMeta = FilterMeta & {
params: string[]; // The unformatted values
Expand All @@ -33,7 +32,7 @@ export const getPhrasesFilterField = (filter: PhrasesFilter) => {
// Creates a filter where the given field matches one or more of the given values
// params should be an array of values
export const buildPhrasesFilter = (
field: IFieldType,
field: FieldBase,
params: any[],
indexPattern: IndexPatternBase
) => {
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/data/common/es_query/filters/range_filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import { each } from 'lodash';
import { buildRangeFilter, getRangeFilterField, RangeFilter } from './range_filter';
import { fields, getField } from '../../index_patterns/mocks';
import { IIndexPattern, IFieldType } from '../../index_patterns';
import { IndexPatternBase, FieldBase } from '../es_query';

describe('Range filter builder', () => {
let indexPattern: IIndexPattern;
let indexPattern: IndexPatternBase;

beforeEach(() => {
indexPattern = {
id: 'id',
} as IIndexPattern;
} as IndexPatternBase;
});

it('should be a function', () => {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('Range filter builder', () => {
});

describe('when given params where one side is infinite', () => {
let field: IFieldType;
let field: FieldBase;
let filter: RangeFilter;

beforeEach(() => {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('Range filter builder', () => {
});

describe('when given params where both sides are infinite', () => {
let field: IFieldType;
let field: FieldBase;
let filter: RangeFilter;

beforeEach(() => {
Expand All @@ -186,9 +186,9 @@ describe('Range filter builder', () => {
});

describe('getRangeFilterField', function () {
const indexPattern: IIndexPattern = ({
const indexPattern: IndexPatternBase = ({
fields,
} as unknown) as IIndexPattern;
} as unknown) as IndexPatternBase;

test('should return the name of the field a range query is targeting', () => {
const field = indexPattern.fields.find((patternField) => patternField.name === 'bytes');
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/data/common/es_query/filters/range_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import type { estypes } from '@elastic/elasticsearch';
import { map, reduce, mapValues, get, keys, pickBy } from 'lodash';
import { Filter, FilterMeta } from './meta_filter';
import { IFieldType } from '../../index_patterns';
import { IndexPatternBase } from '..';
import { IndexPatternBase, FieldBase } from '..';

const OPERANDS_IN_RANGE = 2;

Expand Down Expand Up @@ -83,13 +82,13 @@ export const getRangeFilterField = (filter: RangeFilter) => {
return filter.range && Object.keys(filter.range)[0];
};

const formatValue = (field: IFieldType, params: any[]) =>
const formatValue = (field: FieldBase, params: any[]) =>
map(params, (val: any, key: string) => get(operators, key) + val).join(' ');

// Creates a filter where the value for the given field is in the given range
// params should be an object containing `lt`, `lte`, `gt`, and/or `gte`
export const buildRangeFilter = (
field: IFieldType,
field: FieldBase,
params: RangeFilterParams,
indexPattern: IndexPatternBase,
formattedValue?: string
Expand Down Expand Up @@ -135,7 +134,7 @@ export const buildRangeFilter = (
return filter as RangeFilter;
};

export const getRangeScript = (field: IFieldType, params: RangeFilterParams) => {
export const getRangeScript = (field: FieldBase, params: RangeFilterParams) => {
const knownParams = mapValues(
pickBy(params, (val, key: any) => key in operators),
(value) => (field.type === 'number' && typeof value === 'string' ? parseFloat(value) : value)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/common/es_query/kuery/functions/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { get } from 'lodash';
import * as literal from '../node_types/literal';
import { KueryNode, IFieldType, IndexPatternBase } from '../../..';
import { KueryNode, FieldBase, IndexPatternBase } from '../../..';

export function buildNodeParams(fieldName: string) {
return {
Expand All @@ -30,9 +30,9 @@ export function toElasticsearchQuery(
value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value,
};
const fieldName = literal.toElasticsearchQuery(fullFieldNameArg);
const field = get(indexPattern, 'fields', []).find((fld: IFieldType) => fld.name === fieldName);
const field = indexPattern?.fields?.find((fld: FieldBase) => fld.name === fieldName);

if (field && (field as IFieldType).scripted) {
if (field?.scripted) {
throw new Error(`Exists query does not support scripted fields`);
}
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import _ from 'lodash';
import { nodeTypes } from '../node_types';
import * as ast from '../ast';
import { IndexPatternBase, KueryNode, IFieldType, LatLon } from '../../..';
import { IndexPatternBase, KueryNode, FieldBase, LatLon } from '../../..';

export function buildNodeParams(fieldName: string, params: any) {
params = _.pick(params, 'topLeft', 'bottomRight');
Expand All @@ -36,8 +36,8 @@ export function toElasticsearchQuery(
value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value,
};
const fieldName = nodeTypes.literal.toElasticsearchQuery(fullFieldNameArg) as string;
const fieldList: IFieldType[] = indexPattern?.fields ?? [];
const field = fieldList.find((fld: IFieldType) => fld.name === fieldName);
const fieldList = indexPattern?.fields ?? [];
const field = fieldList.find((fld) => fld.name === fieldName);

const queryParams = args.reduce((acc: any, arg: any) => {
const snakeArgName = _.snakeCase(arg.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { nodeTypes } from '../node_types';
import * as ast from '../ast';
import { IndexPatternBase, KueryNode, IFieldType, LatLon } from '../../..';
import { IndexPatternBase, KueryNode, FieldBase, LatLon } from '../../..';
import { LiteralTypeBuildNode } from '../node_types/types';

export function buildNodeParams(fieldName: string, points: LatLon[]) {
Expand All @@ -35,8 +35,8 @@ export function toElasticsearchQuery(
value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value,
};
const fieldName = nodeTypes.literal.toElasticsearchQuery(fullFieldNameArg) as string;
const fieldList: IFieldType[] = indexPattern?.fields ?? [];
const field = fieldList.find((fld: IFieldType) => fld.name === fieldName);
const fieldList = indexPattern?.fields ?? [];
const field = fieldList.find((fld) => fld.name === fieldName);
const queryParams = {
points: points.map((point: LiteralTypeBuildNode) => {
return ast.toElasticsearchQuery(point, indexPattern, config, context);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/es_query/kuery/functions/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getPhraseScript } from '../../filters';
import { getFields } from './utils/get_fields';
import { getTimeZoneFromSettings } from '../../utils';
import { getFullFieldNameNode } from './utils/get_full_field_name_node';
import { IndexPatternBase, KueryNode, IFieldType } from '../../..';
import { IndexPatternBase, KueryNode, FieldBase } from '../../..';

import * as ast from '../ast';

Expand Down Expand Up @@ -100,7 +100,7 @@ export function toElasticsearchQuery(
return { match_all: {} };
}

const queries = fields!.reduce((accumulator: any, field: IFieldType) => {
const queries = fields!.reduce((accumulator: any, field: FieldBase) => {
const wrapWithNestedQuery = (query: any) => {
// Wildcards can easily include nested and non-nested fields. There isn't a good way to let
// users handle this themselves so we automatically add nested queries in this scenario.
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/es_query/kuery/functions/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getRangeScript, RangeFilterParams } from '../../filters';
import { getFields } from './utils/get_fields';
import { getTimeZoneFromSettings } from '../../utils';
import { getFullFieldNameNode } from './utils/get_full_field_name_node';
import { IndexPatternBase, KueryNode, IFieldType } from '../../..';
import { IndexPatternBase, KueryNode, FieldBase } from '../../..';

export function buildNodeParams(fieldName: string, params: RangeFilterParams) {
const paramsToMap = _.pick(params, 'gt', 'lt', 'gte', 'lte', 'format');
Expand Down Expand Up @@ -62,7 +62,7 @@ export function toElasticsearchQuery(
});
}

const queries = fields!.map((field: IFieldType) => {
const queries = fields!.map((field) => {
const wrapWithNestedQuery = (query: any) => {
// Wildcards can easily include nested and non-nested fields. There isn't a good way to let
// users handle this themselves so we automatically add nested queries in this scenario.
Expand Down
Loading

0 comments on commit f8e7e15

Please sign in to comment.