fix: Counting distinct results for entity facets in search table

Signed-off-by: Stuart Said <stuartman9@hotmail.co.uk>
This commit is contained in:
Stuart Said
2025-09-18 12:26:04 +01:00
parent 52227d382a
commit 2aaf01a0fa
3 changed files with 52 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Fix for duplicate search results in entity-facets API call
@@ -2359,6 +2359,52 @@ describe('DefaultEntitiesCatalog', () => {
});
},
);
it.each(databases.eachSupportedId())(
'works when the entity is duplicated in search results, %p',
async databaseId => {
await createDatabase(databaseId);
await addEntityToSearch({
apiVersion: 'a',
kind: 'k',
metadata: {
name: 'one',
uid: 'uid-a',
},
spec: {},
});
// Manually insert a duplicate search entry, this shouldn't happen but does in reality
await knex<DbSearchRow>('search').insert([
{
entity_id: 'uid-a',
key: 'metadata.name',
value: 'one',
original_value: 'one',
},
]);
const catalog = new DefaultEntitiesCatalog({
database: knex,
logger: mockServices.logger.mock(),
stitcher,
});
await expect(
catalog.facets({
facets: ['metadata.name'],
credentials: mockCredentials.none(),
}),
).resolves.toEqual({
facets: {
'metadata.name': expect.arrayContaining([
{ value: 'one', count: 1 },
]),
},
});
},
);
});
});
@@ -683,7 +683,7 @@ export class DefaultEntitiesCatalog implements EntitiesCatalog {
.select({
facet: 'search.key',
value: 'search.original_value',
count: this.database.raw('count(*)'),
count: this.database.raw('count(DISTINCT search.entity_id)'),
})
.groupBy(['search.key', 'search.original_value']);