Skip to content

Commit 67a9cf2

Browse files
committed
Add missed changes
1 parent 8e1995d commit 67a9cf2

File tree

2 files changed

+73
-29
lines changed

2 files changed

+73
-29
lines changed

src/collections/aggregate/index.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ export type AggregateBaseOptions<T, M> = {
1616
returnMetrics?: M;
1717
};
1818

19-
export type AggregateGroupByOptions<T, M> = AggregateOptions<T, M> & {
20-
groupBy: (keyof T & string) | GroupByAggregate<T>;
19+
export type AggregateGroupByOptions<T, M> = AggregateBaseOptions<T, M> & {
20+
groupBy: T extends undefined ? string : (keyof T & string) | GroupByAggregate<T>;
2121
};
2222

2323
export type GroupByAggregate<T> = {
24-
property: keyof T & string;
24+
property: T extends undefined ? string : keyof T & string;
2525
limit?: number;
2626
};
2727

28-
export type AggregateOptions<T, M> = AggregateBaseOptions<T, M>;
29-
30-
export type AggregateBaseOverAllOptions<T, M> = AggregateBaseOptions<T, M>;
28+
export type AggregateOverAllOptions<T, M> = AggregateBaseOptions<T, M>;
3129

3230
export type AggregateNearOptions<T, M> = AggregateBaseOptions<T, M> & {
3331
certainty?: number;
@@ -46,11 +44,11 @@ export type AggregateHybridOptions<T, M> = AggregateBaseOptions<T, M> & {
4644
};
4745

4846
export type AggregateGroupByHybridOptions<T, M> = AggregateHybridOptions<T, M> & {
49-
groupBy: (keyof T & string) | GroupByAggregate<T>;
47+
groupBy: T extends undefined ? string : (keyof T & string) | GroupByAggregate<T>;
5048
};
5149

5250
export type AggregateGroupByNearOptions<T, M> = AggregateNearOptions<T, M> & {
53-
groupBy: (keyof T & string) | GroupByAggregate<T>;
51+
groupBy: T extends undefined ? string : (keyof T & string) | GroupByAggregate<T>;
5452
};
5553

5654
export type AggregateBoolean = {
@@ -453,7 +451,7 @@ class AggregateManager<T> implements Aggregate<T> {
453451
base(
454452
metrics?: PropertiesMetrics<T>,
455453
filters?: FilterValue,
456-
groupBy?: (keyof T & string) | GroupByAggregate<T>
454+
groupBy?: (keyof T & string) | GroupByAggregate<T> | string
457455
) {
458456
let fields = 'meta { count }';
459457
let builder = this.query().withClassName(this.name);
@@ -602,7 +600,9 @@ class AggregateManager<T> implements Aggregate<T> {
602600
return this.do(builder);
603601
}
604602

605-
overAll<M extends PropertiesMetrics<T>>(opts?: AggregateOptions<T, M>): Promise<AggregateResult<T, M>> {
603+
overAll<M extends PropertiesMetrics<T>>(
604+
opts?: AggregateOverAllOptions<T, M>
605+
): Promise<AggregateResult<T, M>> {
606606
const builder = this.base(opts?.returnMetrics, opts?.filters);
607607
return this.do(builder);
608608
}
@@ -637,7 +637,7 @@ class AggregateManager<T> implements Aggregate<T> {
637637
prop: groupedBy.path[0],
638638
value: groupedBy.value,
639639
},
640-
properties: rest.length > 0 ? rest : undefined,
640+
properties: rest,
641641
totalCount: meta?.count,
642642
};
643643
})
@@ -730,7 +730,9 @@ export interface Aggregate<T> {
730730
* @param {AggregateOptions<T, M>} [opts] The options for the request.
731731
* @returns {Promise<AggregateResult<T, M>[]>} The aggregated metrics for the objects in the collection.
732732
*/
733-
overAll<M extends PropertiesMetrics<T>>(opts?: AggregateOptions<T, M>): Promise<AggregateResult<T, M>>;
733+
overAll<M extends PropertiesMetrics<T>>(
734+
opts?: AggregateOverAllOptions<T, M>
735+
): Promise<AggregateResult<T, M>>;
734736
}
735737

736738
export interface AggregateGroupBy<T> {

src/collections/aggregate/integration.test.ts

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('Testing of the collection.aggregate methods', () => {
140140
expect(result[0].totalCount).toEqual(100);
141141
expect(result[0].groupedBy.prop).toEqual('text');
142142
expect(result[0].groupedBy.value).toEqual('test');
143-
expect(result[0].properties).toBeUndefined();
143+
expect(result[0].properties).toEqual({});
144144
});
145145

146146
it('should aggregate grouped by data with a near text search and no property metrics', async () => {
@@ -152,7 +152,7 @@ describe('Testing of the collection.aggregate methods', () => {
152152
expect(result[0].totalCount).toEqual(100);
153153
expect(result[0].groupedBy.prop).toEqual('text');
154154
expect(result[0].groupedBy.value).toEqual('test');
155-
expect(result[0].properties).toBeUndefined();
155+
expect(result[0].properties).toEqual({});
156156
});
157157

158158
it('should aggregate data without a search and one generic property metric', async () => {
@@ -419,7 +419,7 @@ describe('Testing of collection.aggregate search methods', () => {
419419
],
420420
vectorizers: weaviate.configure.vectorizer.text2VecContextionary(),
421421
})
422-
.then(async () => {
422+
.then(() => {
423423
const data: Array<any> = [];
424424
for (let i = 0; i < 100; i++) {
425425
data.push({
@@ -428,9 +428,10 @@ describe('Testing of collection.aggregate search methods', () => {
428428
},
429429
});
430430
}
431-
await collection.data.insertMany(data).then((res) => {
432-
uuid = res.uuids[0];
433-
});
431+
return collection.data.insertMany(data);
432+
})
433+
.then((res) => {
434+
uuid = res.uuids[0];
434435
});
435436
});
436437

@@ -441,35 +442,76 @@ describe('Testing of collection.aggregate search methods', () => {
441442
queryProperties: ['text'],
442443
returnMetrics: collection.metrics.aggregate('text').text(['count']),
443444
});
444-
expect(result.totalCount).toEqual(100);
445-
expect(result.properties.text.count).toEqual(100);
445+
expect(result.totalCount).toBeGreaterThan(0);
446+
});
447+
448+
it('should return a grouped aggregation on a hybrid search', async () => {
449+
const result = await collection.aggregate.groupBy.hybrid('test', {
450+
objectLimit: 1000,
451+
groupBy: 'text',
452+
returnMetrics: collection.metrics.aggregate('text').text(['count']),
453+
});
454+
expect(result.length).toEqual(1);
455+
expect(result[0].groupedBy.prop).toEqual('text');
456+
expect(result[0].groupedBy.value).toEqual('test');
446457
});
447458

448459
it('should return an aggregation on a nearText search', async () => {
449460
const result = await collection.aggregate.nearText('test', {
450-
objectLimit: 100,
461+
objectLimit: 1000,
451462
returnMetrics: collection.metrics.aggregate('text').text(['count']),
452463
});
453-
expect(result.totalCount).toEqual(100);
454-
expect(result.properties.text.count).toEqual(100);
464+
expect(result.totalCount).toBeGreaterThan(0);
465+
});
466+
467+
it('should return a grouped aggregation on a nearText search', async () => {
468+
const result = await collection.aggregate.groupBy.nearText('test', {
469+
objectLimit: 1000,
470+
groupBy: 'text',
471+
returnMetrics: collection.metrics.aggregate('text').text(['count']),
472+
});
473+
expect(result.length).toEqual(1);
474+
expect(result[0].groupedBy.prop).toEqual('text');
475+
expect(result[0].groupedBy.value).toEqual('test');
455476
});
456477

457478
it('should return an aggregation on a nearVector search', async () => {
458479
const obj = await collection.query.fetchObjectById(uuid, { includeVector: true });
459480
const result = await collection.aggregate.nearVector(obj?.vectors.default!, {
460-
objectLimit: 100,
481+
objectLimit: 1000,
461482
returnMetrics: collection.metrics.aggregate('text').text(['count']),
462483
});
463-
expect(result.totalCount).toEqual(100);
464-
expect(result.properties.text.count).toEqual(100);
484+
expect(result.totalCount).toBeGreaterThan(0);
485+
});
486+
487+
it('should return a grouped aggregation on a nearVector search', async () => {
488+
const obj = await collection.query.fetchObjectById(uuid, { includeVector: true });
489+
const result = await collection.aggregate.groupBy.nearVector(obj?.vectors.default!, {
490+
objectLimit: 1000,
491+
groupBy: 'text',
492+
returnMetrics: collection.metrics.aggregate('text').text(['count']),
493+
});
494+
expect(result.length).toEqual(1);
495+
expect(result[0].groupedBy.prop).toEqual('text');
496+
expect(result[0].groupedBy.value).toEqual('test');
465497
});
466498

467499
it('should return an aggregation on a nearObject search', async () => {
468500
const result = await collection.aggregate.nearObject(uuid, {
469-
objectLimit: 100,
501+
objectLimit: 1000,
470502
returnMetrics: collection.metrics.aggregate('text').text(['count']),
471503
});
472-
expect(result.totalCount).toEqual(100);
473-
expect(result.properties.text.count).toEqual(100);
504+
expect(result.totalCount).toBeGreaterThan(0);
505+
});
506+
507+
it('should return a grouped aggregation on a nearText search', async () => {
508+
const result = await collection.aggregate.groupBy.nearObject(uuid, {
509+
objectLimit: 1000,
510+
groupBy: 'text',
511+
returnMetrics: collection.metrics.aggregate('text').text(['count']),
512+
});
513+
expect(result.length).toEqual(1);
514+
expect(result[0].groupedBy.prop).toEqual('text');
515+
expect(result[0].groupedBy.value).toEqual('test');
474516
});
475517
});

0 commit comments

Comments
 (0)