Skip to content

Commit 80c042e

Browse files
committed
Fix bug when aggregating with multi-tenancy
1 parent 55e2e11 commit 80c042e

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

src/collections/aggregate/index.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DbVersionSupport } from '../../utils/dbVersion.js';
55

66
import { FilterValue } from '../filters/index.js';
77

8+
import { WeaviateQueryError } from '../../errors.js';
89
import { Aggregator } from '../../graphql/index.js';
910
import { Serialize } from '../serialize/index.js';
1011

@@ -444,6 +445,9 @@ class AggregateManager<T> implements Aggregate<T> {
444445
if (filters) {
445446
builder = builder.withWhere(Serialize.filtersREST(filters));
446447
}
448+
if (this.tenant) {
449+
builder = builder.withTenant(this.tenant);
450+
}
447451
return builder;
448452
}
449453

@@ -556,31 +560,41 @@ class AggregateManager<T> implements Aggregate<T> {
556560
do = <M extends PropertiesMetrics<T> | undefined = undefined>(
557561
query: Aggregator
558562
): Promise<AggregateResult<T, M>> => {
559-
return query.do().then(({ data }: any) => {
560-
const { meta, ...rest } = data.Aggregate[this.name][0];
561-
return {
562-
properties: rest,
563-
totalCount: meta?.count,
564-
};
565-
});
563+
return query
564+
.do()
565+
.then(({ data }: any) => {
566+
const { meta, ...rest } = data.Aggregate[this.name][0];
567+
return {
568+
properties: rest,
569+
totalCount: meta?.count,
570+
};
571+
})
572+
.catch((err: Error) => {
573+
throw new WeaviateQueryError(err.message, 'GraphQL');
574+
});
566575
};
567576

568577
doGroupBy = <M extends PropertiesMetrics<T> | undefined = undefined>(
569578
query: Aggregator
570579
): Promise<AggregateGroupByResult<T, M>[]> => {
571-
return query.do().then(({ data }: any) =>
572-
data.Aggregate[this.name].map((item: any) => {
573-
const { groupedBy, meta, ...rest } = item;
574-
return {
575-
groupedBy: {
576-
prop: groupedBy.path[0],
577-
value: groupedBy.value,
578-
},
579-
properties: rest.length > 0 ? rest : undefined,
580-
totalCount: meta?.count,
581-
};
582-
})
583-
);
580+
return query
581+
.do()
582+
.then(({ data }: any) =>
583+
data.Aggregate[this.name].map((item: any) => {
584+
const { groupedBy, meta, ...rest } = item;
585+
return {
586+
groupedBy: {
587+
prop: groupedBy.path[0],
588+
value: groupedBy.value,
589+
},
590+
properties: rest.length > 0 ? rest : undefined,
591+
totalCount: meta?.count,
592+
};
593+
})
594+
)
595+
.catch((err: Error) => {
596+
throw new WeaviateQueryError(err.message, 'GraphQL');
597+
});
584598
};
585599
}
586600

src/collections/aggregate/integration.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
3-
import { WeaviateUnsupportedFeatureError } from '../../errors.js';
3+
import { WeaviateQueryError, WeaviateUnsupportedFeatureError } from '../../errors.js';
44
import weaviate, { AggregateText, WeaviateClient } from '../../index.js';
55
import { Collection } from '../collection/index.js';
66
import { CrossReference } from '../references/index.js';
@@ -331,3 +331,52 @@ describe('Testing of the collection.aggregate methods with named vectors', () =>
331331
expect(result.totalCount).toEqual(0);
332332
});
333333
});
334+
335+
describe('Testing of collection.aggregate.overAll with a multi-tenancy collection', () => {
336+
let client: WeaviateClient;
337+
let collection: Collection;
338+
const collectionName = 'TestCollectionAggregate';
339+
340+
afterAll(async () => {
341+
return (await client).collections.delete(collectionName).catch((err) => {
342+
console.error(err);
343+
throw err;
344+
});
345+
});
346+
347+
beforeAll(async () => {
348+
client = await weaviate.connectToLocal();
349+
return client.collections
350+
.create({
351+
name: collectionName,
352+
properties: [
353+
{
354+
name: 'text',
355+
dataType: 'text',
356+
},
357+
],
358+
multiTenancy: { enabled: true },
359+
})
360+
.then(async (created) => {
361+
const tenants = await created.tenants.create({ name: 'test' });
362+
collection = created.withTenant(tenants[0].name);
363+
const data: Array<any> = [];
364+
for (let i = 0; i < 100; i++) {
365+
data.push({
366+
properties: {
367+
text: 'test',
368+
},
369+
});
370+
}
371+
await collection.data.insertMany(data);
372+
});
373+
});
374+
375+
it('should aggregate data without a search and no property metrics over the tenant', () =>
376+
collection.aggregate.overAll().then((result) => expect(result.totalCount).toEqual(100)));
377+
378+
it('should throw an error for a non-existant tenant', () =>
379+
expect(collection.withTenant('non-existing-tenant').aggregate.overAll()).rejects.toThrow(
380+
WeaviateQueryError
381+
));
382+
});

0 commit comments

Comments
 (0)