diff --git a/.changeset/famous-tips-raise.md b/.changeset/famous-tips-raise.md index 89fc8a6563..1a44e19608 100644 --- a/.changeset/famous-tips-raise.md +++ b/.changeset/famous-tips-raise.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend': patch --- -Fix for duplicate results in queryEntities when using an orderField +Fix for duplicate results in `queryEntities` when providing an `orderField` parameter diff --git a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts index a7b8fdc6c9..8e71e61bc5 100644 --- a/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/service/DefaultEntitiesCatalog.ts @@ -273,6 +273,24 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog { const sortField = cursor.orderFields.at(0); + if (sortField) { + const duplicateCheck = await this.database('search') + .select('entity_id', 'key', 'value') + .count('* as count') + .where('key', '=', sortField.field) + .groupBy('entity_id', 'key', 'value') + .having(this.database.raw('count(*) > 1')) + .limit(5); + + if (duplicateCheck.length > 0) { + this.logger.warn( + `Found duplicate search entries for field ${ + sortField.field + }: ${JSON.stringify(duplicateCheck)}`, + ); + } + } + // The first part of the query builder is a subquery that applies all of the // filtering. const dbQuery = this.database.with(