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

[Data] Cleanup filter docs #107169

Merged
merged 20 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ esFilters: {
isPhraseFilter: (filter: any) => filter is import("@kbn/es-query").PhraseFilter;
isExistsFilter: (filter: any) => filter is import("@kbn/es-query").ExistsFilter;
isPhrasesFilter: (filter: any) => filter is import("@kbn/es-query").PhrasesFilter;
isRangeFilter: (filter: any) => filter is import("@kbn/es-query").RangeFilter;
isRangeFilter: (filter?: import("@kbn/es-query").ExistsFilter | import("@kbn/es-query").GeoPolygonFilter | import("@kbn/es-query").PhrasesFilter | import("@kbn/es-query").PhraseFilter | import("@kbn/es-query").MatchAllFilter | import("@kbn/es-query").MissingFilter | import("@kbn/es-query").RangeFilter | import("@kbn/es-query").GeoBoundingBoxFilter | undefined) => filter is import("@kbn/es-query").RangeFilter;
isMatchAllFilter: (filter: any) => filter is import("@kbn/es-query").MatchAllFilter;
isMissingFilter: (filter: any) => filter is import("@kbn/es-query").MissingFilter;
isQueryStringFilter: (filter: any) => filter is import("@kbn/es-query/target_types/filters/build_filters").QueryStringFilter;
Expand Down
8 changes: 7 additions & 1 deletion packages/kbn-es-query/src/es_query/build_es_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { buildQueryFromLucene } from './from_lucene';
import { Filter, Query } from '../filters';
import { IndexPatternBase } from './types';

/**
* Configurations to be used while constructing an ES query.
* @public
*/
export interface EsQueryConfig {
allowLeadingWildcards: boolean;
queryStringOptions: Record<string, any>;
Expand All @@ -33,6 +37,8 @@ function removeMatchAll<T>(filters: T[]) {
* @param config - an objects with query:allowLeadingWildcards and query:queryString:options UI
* settings in form of { allowLeadingWildcards, queryStringOptions }
* config contains dateformat:tz
*
* @public
*/
export function buildEsQuery(
indexPattern: IndexPatternBase | undefined,
Expand All @@ -47,7 +53,7 @@ export function buildEsQuery(
queries = Array.isArray(queries) ? queries : [queries];
filters = Array.isArray(filters) ? filters : [filters];

const validQueries = queries.filter((query) => has(query, 'query'));
const validQueries = queries.filter((query: any) => has(query, 'query'));
const queriesByLanguage = groupBy(validQueries, 'language');
const kueryQuery = buildQueryFromKuery(
indexPattern,
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-es-query/src/es_query/decorate_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { DslQuery, isEsQueryString } from './es_query_dsl';
* @param queryStringOptions query:queryString:options from UI settings
* @param dateFormatTZ dateFormat:tz from UI settings
* @returns {object}
*
* @public
*/

export function decorateQuery(
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-es-query/src/es_query/es_query_dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ export interface DslTermQuery {
term: Record<string, string>;
}

/**
* @public
*/
export type DslQuery =
| DslRangeQuery
| DslMatchQuery
| DslQueryStringQuery
| DslMatchAllQuery
| DslTermQuery;

/** @internal */
export const isEsQueryString = (query: any): query is DslQueryStringQuery =>
has(query, 'query_string.query');
2 changes: 2 additions & 0 deletions packages/kbn-es-query/src/es_query/filter_matches_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { IndexPatternBase } from '..';
* TODO: We should base this on something better than `filter.meta.key`. We should probably modify
* this to check if `filter.meta.index` matches `indexPattern.id` instead, but that's a breaking
* change.
*
* @internal
*/
export function filterMatchesIndex(filter: Filter, indexPattern?: IndexPatternBase | null) {
if (!filter.meta?.key || !indexPattern) {
Expand Down
8 changes: 8 additions & 0 deletions packages/kbn-es-query/src/es_query/from_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const translateToQuery = (filter: Filter) => {
return filter;
};

/**
* @param filters
* @param indexPattern
* @param ignoreFilterIfFieldNotInIndex by default filters that use fields that can't be found in the specified index pattern are not applied. Set this to true if you want to apply them any way.
* @returns An EQL query
*
* @public
*/
export const buildQueryFromFilters = (
filters: Filter[] = [],
indexPattern: IndexPatternBase | undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-es-query/src/es_query/from_kuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Query } from '../filters';
import { fromKueryExpression, toElasticsearchQuery, nodeTypes, KueryNode } from '../kuery';
import { IndexPatternBase } from './types';

/** @internal */
export function buildQueryFromKuery(
indexPattern: IndexPatternBase | undefined,
queries: Query[] = [],
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-es-query/src/es_query/from_lucene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Query } from '..';
import { decorateQuery } from './decorate_query';
import { luceneStringToDsl } from './lucene_string_to_dsl';

/** @internal */
export function buildQueryFromLucene(
queries: Query[],
queryStringOptions: Record<string, any>,
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-es-query/src/es_query/handle_nested_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { getFilterField, cleanFilter, Filter } from '../filters';
import { IndexPatternBase } from './types';

/** @internal */
export const handleNestedFilter = (filter: Filter, indexPattern?: IndexPatternBase) => {
if (!indexPattern) return filter;

Expand Down
7 changes: 7 additions & 0 deletions packages/kbn-es-query/src/es_query/lucene_string_to_dsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import { isString } from 'lodash';
import { DslQuery } from './es_query_dsl';

/**
*
* @param query
* @returns
*
* @public
*/
export function luceneStringToDsl(query: string | any): DslQuery {
if (isString(query)) {
if (query.trim() === '') {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-query/src/es_query/migrate_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getConvertedValueForField } from '../filters';
import { Filter } from '../filters';
import { IndexPatternBase } from './types';

export interface DeprecatedMatchPhraseFilter extends Filter {
interface DeprecatedMatchPhraseFilter extends Filter {
query: {
match: {
[field: string]: {
Expand All @@ -28,6 +28,7 @@ function isDeprecatedMatchPhraseFilter(filter: any): filter is DeprecatedMatchPh
return Boolean(fieldName && get(filter, ['query', 'match', fieldName, 'type']) === 'phrase');
}

/** @internal */
export function migrateFilter(filter: Filter, indexPattern?: IndexPatternBase) {
if (isDeprecatedMatchPhraseFilter(filter)) {
const fieldName = Object.keys(filter.query.match)[0];
Expand Down
13 changes: 13 additions & 0 deletions packages/kbn-es-query/src/es_query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

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

/**
* A field's sub type
* @public
*/
export interface IFieldSubType {
multi?: { parent: string };
nested?: { path: string };
}

/**
* A base interface for an index pattern field
* @public
*/
export interface IndexPatternFieldBase {
name: string;
/**
Expand All @@ -31,6 +40,10 @@ export interface IndexPatternFieldBase {
scripted?: boolean;
}

/**
* A base interface for an index pattern
* @public
*/
export interface IndexPatternBase {
fields: IndexPatternFieldBase[];
id?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { Filter, FilterMeta, FilterState, FilterStateStore } from './types';
import { Filter, FilterMeta, FilterStateStore } from './types';

export const buildEmptyFilter = (isPinned: boolean, index?: string): Filter => {
const meta: FilterMeta = {
Expand All @@ -15,7 +15,7 @@ export const buildEmptyFilter = (isPinned: boolean, index?: string): Filter => {
alias: null,
index,
};
const $state: FilterState = {
const $state: Filter['$state'] = {
store: isPinned ? FilterStateStore.GLOBAL_STATE : FilterStateStore.APP_STATE,
};

Expand Down
36 changes: 15 additions & 21 deletions packages/kbn-es-query/src/filters/build_filters/build_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@ import { buildRangeFilter } from './range_filter';
import { buildExistsFilter } from './exists_filter';

import type { IndexPatternFieldBase, IndexPatternBase } from '../../es_query';
import { FilterMeta, FilterStateStore } from './types';
import { FilterStateStore } from './types';

/**
*
* @param indexPattern
* @param field
* @param type
* @param negate whether the filter is negated (NOT filter)
* @param disabled whether the filter is disabled andwon't be applied to searches
* @param params
* @param alias a display name for the filter
* @param store whether the filter applies to the current application or should be applied to global context
* @returns
*
* @public
*/
export function buildFilter(
indexPattern: IndexPatternBase,
field: IndexPatternFieldBase,
Expand All @@ -36,26 +50,6 @@ export function buildFilter(
return filter;
}

export function buildCustomFilter(
indexPatternString: string,
queryDsl: any,
disabled: boolean,
negate: boolean,
alias: string | null,
store: FilterStateStore
): Filter {
const meta: FilterMeta = {
index: indexPatternString,
type: FILTERS.CUSTOM,
disabled,
negate,
alias,
};
const filter: Filter = { ...queryDsl, meta };
filter.$state = { store };
return filter;
}

function buildBaseFilter(
indexPattern: IndexPatternBase,
field: IndexPatternFieldBase,
Expand Down
35 changes: 34 additions & 1 deletion packages/kbn-es-query/src/filters/build_filters/custom_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,41 @@
* Side Public License, v 1.
*/

import type { Filter } from './types';
import { Filter, FilterMeta, FILTERS, FilterStateStore } from './types';

/** @public */
export type CustomFilter = Filter & {
query: any;
};

/**
*
* @param indexPatternString
* @param queryDsl
* @param disabled
* @param negate
* @param alias
* @param store
* @returns
*
* @public
*/
export function buildCustomFilter(
indexPatternString: string,
queryDsl: any,
disabled: boolean,
negate: boolean,
alias: string | null,
store: FilterStateStore
): Filter {
const meta: FilterMeta = {
index: indexPatternString,
type: FILTERS.CUSTOM,
disabled,
negate,
alias,
};
const filter: Filter = { ...queryDsl, meta };
filter.$state = { store };
return filter;
}
36 changes: 26 additions & 10 deletions packages/kbn-es-query/src/filters/build_filters/exists_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,42 @@
* Side Public License, v 1.
*/

import { get } from 'lodash';
import type { IndexPatternFieldBase, IndexPatternBase } from '../../es_query';
import type { Filter, FilterMeta } from './types';

export type ExistsFilterMeta = FilterMeta;

export interface FilterExistsProperty {
field: any;
}
import type { FieldFilter, Filter, FilterMeta } from './types';

/** @public */
export type ExistsFilter = Filter & {
meta: ExistsFilterMeta;
exists?: FilterExistsProperty;
meta: FilterMeta;
exists?: {
field: string;
};
};

export const isExistsFilter = (filter: any): filter is ExistsFilter => filter && filter.exists;
/**
* @param filter
* @returns `true` if a filter is an `ExistsFilter`
*
* @public
*/
export const isExistsFilter = (filter: FieldFilter): filter is ExistsFilter =>
get(filter, 'exists');
Copy link
Member

@tsullivan tsullivan Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't explicitly return a boolean, the return value of get is any.

Should this be:

Suggested change
get(filter, 'exists');
get(filter, 'exists') != null;

?

If it's valid for filter.exists to be 0, then this function seems it would return the wrong value. Perhaps this is not a big deal, and also the same issue existed in the previous code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it by replacing get with has which does exactly that


/**
* @internal
*/
export const getExistsFilterField = (filter: ExistsFilter) => {
return filter.exists && filter.exists.field;
};

/**
* Builds an `ExistsFilter`
* @param field field to validate the existence of
* @param indexPattern index pattern to look for the field in
* @returns An `ExistsFilter`
*
* @public
*/
export const buildExistsFilter = (field: IndexPatternFieldBase, indexPattern: IndexPatternBase) => {
return {
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import type { Filter, FilterMeta, LatLon } from './types';
import { get } from 'lodash';
import type { FieldFilter, Filter, FilterMeta, LatLon } from './types';

export type GeoBoundingBoxFilterMeta = FilterMeta & {
params: {
Expand All @@ -20,9 +21,18 @@ export type GeoBoundingBoxFilter = Filter & {
geo_bounding_box: any;
};

export const isGeoBoundingBoxFilter = (filter: any): filter is GeoBoundingBoxFilter =>
filter && filter.geo_bounding_box;
/**
* @param filter
* @returns `true` if a filter is an `GeoBoundingBoxFilter`
*
* @public
*/
export const isGeoBoundingBoxFilter = (filter: FieldFilter): filter is GeoBoundingBoxFilter =>
get(filter, 'geo_bounding_box');

/**
* @internal
*/
export const getGeoBoundingBoxFilterField = (filter: GeoBoundingBoxFilter) => {
return (
filter.geo_bounding_box &&
Expand Down
Loading