-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SOM] use aggregation for type counts (#168918)
## Summary Use an aggregation for type counts in SO management instead of scrolling through all the documents. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
4113d25
commit 0c1f828
Showing
4 changed files
with
57 additions
and
17 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/plugins/saved_objects_management/server/lib/get_saved_objects_counts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { estypes } from '@elastic/elasticsearch'; | ||
import type { SavedObjectsFindOptions } from '@kbn/core-saved-objects-api-server'; | ||
import type { SavedObjectsClientContract } from '@kbn/core/server'; | ||
|
||
export const getSavedObjectCounts = async ({ | ||
types, | ||
options, | ||
client, | ||
}: { | ||
types: string[]; | ||
options: SavedObjectsFindOptions; | ||
client: SavedObjectsClientContract; | ||
}): Promise<Record<string, number>> => { | ||
const body = await client.find<void, { types: estypes.AggregationsStringTermsAggregate }>({ | ||
...options, | ||
type: types, | ||
perPage: 0, | ||
aggs: { | ||
types: { | ||
terms: { | ||
field: 'type', | ||
size: types.length, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const buckets = | ||
(body.aggregations?.types?.buckets as estypes.AggregationsStringTermsBucketKeys[]) || []; | ||
|
||
const counts = buckets.reduce((memo, bucket) => { | ||
memo[bucket.key] = bucket.doc_count; | ||
return memo; | ||
}, {} as Record<string, number>); | ||
|
||
return counts; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters