Skip to content

Commit

Permalink
add JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Feb 21, 2024
1 parent 7c2342a commit c31895e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions x-pack/packages/ml/query_utils/src/default_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { isMatchAllQuery } from './match_all_query';
import { isSimpleQuery } from './simple_query';
import type { SearchQueryVariant } from './types';

/**
* Checks if the provided query is a default query. A default query is considered as one that matches all documents,
* either directly through a `match_all` query, a `SimpleQuery` with a wildcard query string, or a `FilterBasedSimpleQuery`
* that contains a filter with a wildcard query or a `match_all` condition.
*
* @param {SearchQueryVariant} query - The query to check.
* @returns {boolean} True if the query is a default query, false otherwise.
*/
export function isDefaultQuery(query: SearchQueryVariant): boolean {
return (
isMatchAllQuery(query) ||
Expand Down
15 changes: 14 additions & 1 deletion x-pack/packages/ml/query_utils/src/match_all_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@

import { isPopulatedObject } from '@kbn/ml-is-populated-object';

export const matchAllQuery = { match_all: {} };
/**
* Represents a query that matches all documents.
*/
export const matchAllQuery = {
/**
* 'match_all' property specifies a query that matches all documents.
*/
match_all: {},
};

/**
* Checks if an argument is a `match_all` query.
* @param {unknown} query - Argument to check.
* @returns {boolean} True if `query` is a `match_all` query, false otherwise.
*/
export function isMatchAllQuery(query: unknown): boolean {
return (
isPopulatedObject(query, ['match_all']) &&
Expand Down
8 changes: 8 additions & 0 deletions x-pack/packages/ml/query_utils/src/simple_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import { isPopulatedObject } from '@kbn/ml-is-populated-object';

import type { SimpleQuery } from './types';

/**
* Default instance of `SimpleQuery` with a wildcard query string.
*/
export const defaultSimpleQuery: SimpleQuery = { query_string: { query: '*' } };

/**
* Type guard verifying if an argument is a `SimpleQuery`.
* @param {unknown} arg - Argument to check.
* @returns {boolean} True if `arg` is a `SimpleQuery`, false otherwise.
*/
export function isSimpleQuery(arg: unknown): arg is SimpleQuery {
return isPopulatedObject(arg, ['query_string']);
}
28 changes: 28 additions & 0 deletions x-pack/packages/ml/query_utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,47 @@ export const SEARCH_QUERY_LANGUAGE = {
*/
export type SearchQueryLanguage = typeof SEARCH_QUERY_LANGUAGE[keyof typeof SEARCH_QUERY_LANGUAGE];

/**
* Placeholder for the structure for a saved search query.
*/
export type SavedSearchQuery = object;

/**
* Represents a simple query structure for searching documents.
*/
export interface SimpleQuery {
/**
* Defines the query string parameters for the search.
*/
query_string: {
/**
* The query text to search for within documents.
*/
query: string;

/**
* The default logical operator.
*/
default_operator?: estypes.QueryDslOperator;
};
}

/**
* Represents simple queries that are based on a boolean filter.
*/
export interface FilterBasedSimpleQuery {
/**
* The container for the boolean filter logic.
*/
bool: {
/**
* An array of `SimpleQuery` objects.
*/
filter: [SimpleQuery];
};
}

/**
* Represents a union of search queries that can be used to fetch documents.
*/
export type SearchQueryVariant = FilterBasedSimpleQuery | SimpleQuery | SavedSearchQuery;

0 comments on commit c31895e

Please sign in to comment.