Skip to content

Commit

Permalink
TS AggregationsResult improvements #9857 (#10000)
Browse files Browse the repository at this point in the history
  • Loading branch information
edloidas authored Feb 1, 2023
1 parent a56e9e9 commit 6598ec6
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 117 deletions.
62 changes: 44 additions & 18 deletions modules/lib/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,23 @@ export type SortDsl = FieldSortDsl | GeoDistanceSortDsl;
//
// START AGGREGATIONS, FILTERS
//
export interface Bucket {
[subAggregationName: string]: AggregationsResult | string | number | undefined;

export type Bucket<SubAggregations extends Aggregations = Record<never, never>> = AggregationsToAggregationResults<SubAggregations> & {
key: string;
docCount: number;
}
};

export interface NumericBucket
extends Bucket {
export type NumericBucket<SubAggregations extends Aggregations = Record<never, never>> = Bucket<SubAggregations> & {
from?: number;
to?: number;
}
};

export interface DateBucket
extends Bucket {
export type DateBucket<SubAggregations extends Aggregations = Record<never, never>> = Bucket<SubAggregations> & {
from?: string;
to?: string;
}
};

export interface BucketsAggregationResult {
buckets: (DateBucket | NumericBucket)[];
export interface BucketsAggregationResult<SubAggregations extends Aggregations = Record<never, never>> {
buckets: (DateBucket<SubAggregations> | NumericBucket<SubAggregations>)[];
}

export interface StatsAggregationResult {
Expand All @@ -318,7 +314,35 @@ export interface SingleValueMetricAggregationResult {
value: number;
}

export type AggregationsResult = BucketsAggregationResult | StatsAggregationResult | SingleValueMetricAggregationResult;
export type AggregationsResult<SubAggregations extends Aggregations = Record<never, never>> =
| BucketsAggregationResult<SubAggregations>
| StatsAggregationResult
| SingleValueMetricAggregationResult;

export type AggregationsToAggregationResults<AggregationInput extends Aggregations = never> = {
[Key in keyof AggregationInput]: AggregationToAggregationResult<AggregationInput[Key]>;
};

export type AggregationToAggregationResult<Type extends Aggregation> = Type extends BucketsAggregationsUnion
? BucketsAggregationResult<Type['aggregations']>
: Type extends SingleValueMetricAggregationsUnion
? SingleValueMetricAggregationResult
: Type extends StatsAggregation
? StatsAggregationResult
: never;

export type BucketsAggregationsUnion =
| TermsAggregation
| GeoDistanceAggregation
| NumericRangeAggregation
| DateRangeAggregation
| HistogramAggregation
| DateHistogramAggregation;

export type SingleValueMetricAggregationsUnion =
| MinAggregation
| MaxAggregation
| ValueCountAggregation;

export type Aggregation =
| TermsAggregation
Expand All @@ -332,14 +356,16 @@ export type Aggregation =
| MaxAggregation
| ValueCountAggregation;

export type Aggregations = Record<string, Aggregation>;

export interface TermsAggregation {
terms: {
field: string;
order?: string;
size?: number;
minDocCount?: number;
};
aggregations?: Record<string, Aggregation>;
aggregations?: Aggregations;
}

export interface HistogramAggregation {
Expand All @@ -351,7 +377,7 @@ export interface HistogramAggregation {
extendedBoundMax?: number;
minDocCount?: number;
};
aggregations?: Record<string, Aggregation>;
aggregations?: Aggregations;
}

export interface DateHistogramAggregation {
Expand All @@ -361,7 +387,7 @@ export interface DateHistogramAggregation {
minDocCount?: number;
format: string;
};
aggregations?: Record<string, Aggregation>;
aggregations?: Aggregations;
}

export interface NumericRange {
Expand All @@ -375,7 +401,7 @@ export interface NumericRangeAggregation {
field: string;
ranges?: NumericRange[];
};
aggregations?: Record<string, Aggregation>;
aggregations?: Aggregations;
}

export interface DateRange {
Expand All @@ -390,7 +416,7 @@ export interface DateRangeAggregation {
format: string;
ranges: DateRange[];
};
aggregations?: Record<string, Aggregation>;
aggregations?: Aggregations;
}

export interface StatsAggregation {
Expand Down
126 changes: 76 additions & 50 deletions modules/lib/lib-content/src/main/resources/lib/xp/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ declare global {

import type {
Aggregation,
Aggregations,
AggregationsResult,
AggregationsToAggregationResults,
BucketsAggregationResult,
ByteSource,
Content,
Filter,
Expand All @@ -27,6 +30,7 @@ import type {
HighlightResult,
PublishInfo,
QueryDsl,
SingleValueMetricAggregationResult,
SortDsl,
} from '@enonic-types/core';

Expand Down Expand Up @@ -54,56 +58,59 @@ function checkOptionalNumber<T extends object>(obj: T, name: keyof T): void {
}

export type {
Aggregation,
Aggregations,
AggregationsResult,
Attachment,
BooleanDslExpression,
BooleanFilter,
BucketsAggregationResult,
BucketsAggregationsUnion,
ByteSource,
PublishInfo,
Content,
Component,
Region,
QueryDsl,
InDslExpression,
ExistsDslExpression,
FulltextDslExpression,
LikeDslExpression,
StemmedDslExpression,
TermDslExpression,
RangeDslExpression,
NgramDslExpression,
BooleanDslExpression,
MatchAllDslExpression,
PathMatchDslExpression,
Content,
DateBucket,
BucketsAggregationResult,
StatsAggregationResult,
SingleValueMetricAggregationResult,
AggregationsResult,
Aggregation,
TermsAggregation,
HistogramAggregation,
DateHistogramAggregation,
NumericRange,
NumericRangeAggregation,
DateRange,
DateRangeAggregation,
StatsAggregation,
GeoDistanceAggregation,
MinAggregation,
MaxAggregation,
ValueCountAggregation,
Highlight,
HighlightResult,
ExistsDslExpression,
ExistsFilter,
NotExistsFilter,
HasValueFilter,
IdsFilter,
BooleanFilter,
Filter,
FormItemSet,
FormItemLayout,
FormItem,
FormItemInlineMixin,
FormItemInput,
FormItemLayout,
FormItemOptionSet,
FormItemInlineMixin,
FormItem,
FormItemSet,
FulltextDslExpression,
GeoDistanceAggregation,
HasValueFilter,
Highlight,
HighlightResult,
HistogramAggregation,
IdsFilter,
InDslExpression,
LikeDslExpression,
MatchAllDslExpression,
MaxAggregation,
MinAggregation,
NgramDslExpression,
NotExistsFilter,
NumericRange,
NumericRangeAggregation,
PathMatchDslExpression,
PublishInfo,
QueryDsl,
RangeDslExpression,
Region,
SingleValueMetricAggregationResult,
SingleValueMetricAggregationsUnion,
StatsAggregation,
StatsAggregationResult,
StemmedDslExpression,
TermDslExpression,
TermsAggregation,
ValueCountAggregation,
} from '@enonic-types/core';

type Attachments = Content['attachments'];
Expand Down Expand Up @@ -412,11 +419,14 @@ export {
_delete as delete,
};

export interface ContentsResult<Hit extends Content<unknown>> {
export interface ContentsResult<
Hit extends Content<unknown>,
AggregationOutput extends Record<string, AggregationsResult>
> {
total: number;
count: number;
hits: Hit[];
aggregations?: Record<string, AggregationsResult>;
aggregations: AggregationOutput;
highlight?: Record<string, HighlightResult>;
}

Expand All @@ -436,7 +446,10 @@ interface GetChildContentHandler {

setSort(value?: string | null): void;

execute<Hit extends Content<unknown>>(): ContentsResult<Hit>;
execute<
Hit extends Content<unknown>,
AggregationOutput extends Record<string, AggregationsResult>
>(): ContentsResult<Hit, AggregationOutput>;
}

/**
Expand All @@ -452,7 +465,10 @@ interface GetChildContentHandler {
*
* @returns {Object} Result (of content) fetched from the repository.
*/
export function getChildren<Hit extends Content<unknown> = Content>(params: GetChildContentParams): ContentsResult<Hit> {
export function getChildren<
Hit extends Content<unknown> = Content,
AggregationOutput extends Record<string, AggregationsResult> = never
>(params: GetChildContentParams): ContentsResult<Hit, AggregationOutput> {
checkRequired(params, 'key');

const {
Expand All @@ -467,7 +483,7 @@ export function getChildren<Hit extends Content<unknown> = Content>(params: GetC
bean.setStart(start);
bean.setCount(count);
bean.setSort(__.nullOrValue(sort));
return __.toNativeObject(bean.execute<Hit>());
return __.toNativeObject(bean.execute<Hit, AggregationOutput>());
}

export type IdGeneratorSupplier = (value: string) => string;
Expand Down Expand Up @@ -542,7 +558,10 @@ interface CreateContentHandler {
*
* @returns {object} Content created as JSON.
*/
export function create<Data = Record<string, unknown>, Type extends string = string>(params: CreateContentParams<Data, Type>): Content<Data, Type> {
export function create<
Data = Record<string, unknown>,
Type extends string = string
>(params: CreateContentParams<Data, Type>): Content<Data, Type> {
const {
name,
parentPath,
Expand Down Expand Up @@ -578,13 +597,13 @@ export function create<Data = Record<string, unknown>, Type extends string = str
return __.toNativeObject(bean.execute<Data, Type>());
}

export interface QueryContentParams {
export interface QueryContentParams<AggregationInput extends Aggregations> {
start?: number;
count?: number;
query?: QueryDsl | string;
sort?: string | SortDsl | SortDsl[];
filters?: Filter | Filter[];
aggregations?: Record<string, Aggregation>;
aggregations?: AggregationInput;
contentTypes?: string[];
highlight?: Highlight;
}
Expand All @@ -606,7 +625,10 @@ interface QueryContentHandler {

setHighlight(value: ScriptValue): void;

execute<Hit extends Content<unknown>>(): ContentsResult<Hit>;
execute<
Hit extends Content<unknown>,
AggregationInput extends Aggregations = never
>(): ContentsResult<Hit, AggregationsToAggregationResults<AggregationInput>>;
}

/**
Expand All @@ -625,7 +647,11 @@ interface QueryContentHandler {
*
* @returns {object} Result of query.
*/
export function query<Hit extends Content<unknown> = Content>(params: QueryContentParams): ContentsResult<Hit> {

export function query<
Hit extends Content<unknown> = Content,
AggregationInput extends Aggregations = never
>(params: QueryContentParams<AggregationInput>): ContentsResult<Hit, AggregationsToAggregationResults<AggregationInput>> {
const bean = __.newBean<QueryContentHandler>('com.enonic.xp.lib.content.QueryContentHandler');

bean.setStart(params.start);
Expand All @@ -637,7 +663,7 @@ export function query<Hit extends Content<unknown> = Content>(params: QueryConte
bean.setFilters(__.toScriptValue(params.filters));
bean.setHighlight(__.toScriptValue(params.highlight));

return __.toNativeObject(bean.execute<Hit>());
return __.toNativeObject(bean.execute<Hit, AggregationInput>());
}

export interface ModifyContentParams<Data, Type extends string> {
Expand Down
Loading

0 comments on commit 6598ec6

Please sign in to comment.