Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SAVED_OBJECTS] [AGGS] Step I to add aggregations in the find of saved objects #64002

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b699c70
step 1 to add aggs in the find function of saved object
XavierM Apr 14, 2020
3819cd5
setp 2 - add specific unit test to aggs + fix bug found during integr…
XavierM Apr 15, 2020
1c4bac9
step 3 - add security api_integration arounds aggs
XavierM Apr 20, 2020
6feca9b
fix types
XavierM Apr 20, 2020
8dda821
unit test added for aggs_utils
XavierM Apr 20, 2020
0904a0e
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Apr 20, 2020
44dcb25
add documentation
XavierM Apr 22, 2020
560b63b
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Apr 22, 2020
699d0a1
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Apr 27, 2020
0f57677
fix docs
XavierM Apr 27, 2020
d7cb38e
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Apr 27, 2020
ec2a69d
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Jul 6, 2020
817a3ef
review I
XavierM Jul 7, 2020
f7faa18
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Jul 7, 2020
a54f440
doc
XavierM Jul 7, 2020
2aef7fd
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Jul 7, 2020
e698b0e
try to fix test
XavierM Jul 7, 2020
09565d8
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Jul 7, 2020
c16e72b
add the new property to the saved object globaltype
XavierM Jul 7, 2020
5a12d44
fix types
XavierM Jul 7, 2020
c4eff53
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Jul 7, 2020
1eaafca
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Nov 9, 2020
f04ab64
delete old files
XavierM Nov 9, 2020
8f64144
fix types + test api integration
XavierM Nov 9, 2020
2209d12
type fix + test
XavierM Nov 10, 2020
4f667d4
Update src/core/server/saved_objects/types.ts
XavierM Nov 11, 2020
433a71c
review I
XavierM Nov 11, 2020
37ad64d
Merge branch 'saved-objects-aggs' of github.com:XavierM/kibana into s…
XavierM Nov 11, 2020
2fb9a2c
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Nov 11, 2020
48dcdda
Merge branch 'master' into saved-objects-aggs
kibanamachine Nov 11, 2020
2dc3ff8
Merge branch 'master' into saved-objects-aggs
kibanamachine Nov 12, 2020
f1f437b
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Nov 20, 2020
b23eac7
change our validation to match discussion with Pierre and Rudolph
XavierM Nov 23, 2020
462d87e
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Nov 23, 2020
4e319b3
Validate multiple items nested filter query through KueryNode
XavierM Nov 23, 2020
ba55542
Merge branch 'master' of github.com:elastic/kibana into saved-objects…
XavierM Nov 23, 2020
16de875
remove unused import
XavierM Nov 23, 2020
cb7ada3
review + put back test
XavierM Nov 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/api/saved-objects/find.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ experimental[] Retrieve a paginated set of {kib} saved objects by various condit

`filter`::
(Optional, string) The filter is a KQL string with the caveat that if you filter with an attribute from your type saved object.
It should look like that savedObjectType.attributes.title: "myTitle". However, If you used a direct attribute of a saved object like `updatedAt`,
It should look like that savedObjectType.attributes.title: "myTitle". However, If you used a direct attribute of a saved object like `updated_at`,
you will have to define your filter like that savedObjectType.updatedAt > 2018-12-22.

`aggs`::
(Optional, string) The aggs will support aggregation string with the caveat that your field from the aggregation will have the attribute from your type saved object,
it should look like this: savedObjectType.attributes.field. However, If you use a direct attribute of a saved object like updatedAt, you will have to define your filter like this: savedObjectType.updated_at.

NOTE: As objects change in {kib}, the results on each page of the response also
change. Use the find API for traditional paginated results, but avoid using it to export large amounts of data.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Search for objects
<b>Signature:</b>

```typescript
find: <T = unknown>(options: SavedObjectsFindOptions) => Promise<SavedObjectsFindResponsePublic<T>>;
find: <T = unknown, A = unknown>(options: SavedObjectsFindOptions) => Promise<SavedObjectsFindResponsePublic<T, unknown>>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The constructor for this class is marked as internal. Third-party code should no
| [bulkGet](./kibana-plugin-core-public.savedobjectsclient.bulkget.md) | | <code>(objects?: Array&lt;{</code><br/><code> id: string;</code><br/><code> type: string;</code><br/><code> }&gt;) =&gt; Promise&lt;SavedObjectsBatchResponse&lt;unknown&gt;&gt;</code> | Returns an array of objects by id |
| [create](./kibana-plugin-core-public.savedobjectsclient.create.md) | | <code>&lt;T = unknown&gt;(type: string, attributes: T, options?: SavedObjectsCreateOptions) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Persists an object |
| [delete](./kibana-plugin-core-public.savedobjectsclient.delete.md) | | <code>(type: string, id: string, options?: SavedObjectsDeleteOptions &#124; undefined) =&gt; ReturnType&lt;SavedObjectsApi['delete']&gt;</code> | Deletes an object |
| [find](./kibana-plugin-core-public.savedobjectsclient.find.md) | | <code>&lt;T = unknown&gt;(options: SavedObjectsFindOptions) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T&gt;&gt;</code> | Search for objects |
| [find](./kibana-plugin-core-public.savedobjectsclient.find.md) | | <code>&lt;T = unknown, A = unknown&gt;(options: SavedObjectsFindOptions) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T, unknown&gt;&gt;</code> | Search for objects |
| [get](./kibana-plugin-core-public.savedobjectsclient.get.md) | | <code>&lt;T = unknown&gt;(type: string, id: string) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Fetches a single object |

## Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

## SavedObjectsCreateOptions interface


<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [SavedObjectsFindResponsePublic](./kibana-plugin-core-public.savedobjectsfindresponsepublic.md) &gt; [aggregations](./kibana-plugin-core-public.savedobjectsfindresponsepublic.aggregations.md)

## SavedObjectsFindResponsePublic.aggregations property

<b>Signature:</b>

```typescript
aggregations?: A;
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Return type of the Saved Objects `find()` method.
<b>Signature:</b>

```typescript
export interface SavedObjectsFindResponsePublic<T = unknown> extends SavedObjectsBatchResponse<T>
export interface SavedObjectsFindResponsePublic<T = unknown, A = unknown> extends SavedObjectsBatchResponse<T>
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [aggregations](./kibana-plugin-core-public.savedobjectsfindresponsepublic.aggregations.md) | <code>A</code> | |
| [page](./kibana-plugin-core-public.savedobjectsfindresponsepublic.page.md) | <code>number</code> | |
| [perPage](./kibana-plugin-core-public.savedobjectsfindresponsepublic.perpage.md) | <code>number</code> | |
| [total](./kibana-plugin-core-public.savedobjectsfindresponsepublic.total.md) | <code>number</code> | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Find all SavedObjects matching the search query
<b>Signature:</b>

```typescript
find<T = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindResponse<T>>;
find<T = unknown, A = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindResponse<T, A>>;
```

## Parameters
Expand All @@ -20,5 +20,5 @@ find<T = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindRes

<b>Returns:</b>

`Promise<SavedObjectsFindResponse<T>>`
`Promise<SavedObjectsFindResponse<T, A>>`

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsFindResponse](./kibana-plugin-core-server.savedobjectsfindresponse.md) &gt; [aggregations](./kibana-plugin-core-server.savedobjectsfindresponse.aggregations.md)

## SavedObjectsFindResponse.aggregations property

<b>Signature:</b>

```typescript
aggregations?: A;
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Return type of the Saved Objects `find()` method.
<b>Signature:</b>

```typescript
export interface SavedObjectsFindResponse<T = unknown>
export interface SavedObjectsFindResponse<T = unknown, A = unknown>
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [aggregations](./kibana-plugin-core-server.savedobjectsfindresponse.aggregations.md) | <code>A</code> | |
| [page](./kibana-plugin-core-server.savedobjectsfindresponse.page.md) | <code>number</code> | |
| [per\_page](./kibana-plugin-core-server.savedobjectsfindresponse.per_page.md) | <code>number</code> | |
| [saved\_objects](./kibana-plugin-core-server.savedobjectsfindresponse.saved_objects.md) | <code>Array&lt;SavedObjectsFindResult&lt;T&gt;&gt;</code> | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
find<T = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindResponse<T>>;
find<T = unknown, A = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindResponse<T, A>>;
```

## Parameters
Expand All @@ -18,7 +18,7 @@ find<T = unknown>(options: SavedObjectsFindOptions): Promise<SavedObjectsFindRes

<b>Returns:</b>

`Promise<SavedObjectsFindResponse<T>>`
`Promise<SavedObjectsFindResponse<T, A>>`

{<!-- -->promise<!-- -->} - { saved\_objects: \[{ id, type, version, attributes }<!-- -->\], total, per\_page, page }

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ Creates an empty response for a find operation. This is only intended to be used
<b>Signature:</b>

```typescript
static createEmptyFindResponse: <T>({ page, perPage, }: SavedObjectsFindOptions) => SavedObjectsFindResponse<T>;
static createEmptyFindResponse: <T, A>({ page, perPage, }: SavedObjectsFindOptions) => SavedObjectsFindResponse<T, A>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export declare class SavedObjectsUtils

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [createEmptyFindResponse](./kibana-plugin-core-server.savedobjectsutils.createemptyfindresponse.md) | <code>static</code> | <code>&lt;T&gt;({ page, perPage, }: SavedObjectsFindOptions) =&gt; SavedObjectsFindResponse&lt;T&gt;</code> | Creates an empty response for a find operation. This is only intended to be used by saved objects client wrappers. |
| [createEmptyFindResponse](./kibana-plugin-core-server.savedobjectsutils.createemptyfindresponse.md) | <code>static</code> | <code>&lt;T, A&gt;({ page, perPage, }: SavedObjectsFindOptions) =&gt; SavedObjectsFindResponse&lt;T, A&gt;</code> | Creates an empty response for a find operation. This is only intended to be used by saved objects client wrappers. |
| [namespaceIdToString](./kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md) | <code>static</code> | <code>(namespace?: string &#124; undefined) =&gt; string</code> | Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the <code>undefined</code> namespace ID (which has a namespace string of <code>'default'</code>). |
| [namespaceStringToId](./kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md) | <code>static</code> | <code>(namespace: string) =&gt; string &#124; undefined</code> | Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the <code>'default'</code> namespace string (which has a namespace ID of <code>undefined</code>). |

10 changes: 8 additions & 2 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1088,14 +1088,16 @@ export class SavedObjectsClient {
// Warning: (ae-forgotten-export) The symbol "SavedObjectsClientContract" needs to be exported by the entry point index.d.ts
delete: (type: string, id: string, options?: SavedObjectsDeleteOptions | undefined) => ReturnType<SavedObjectsClientContract_2['delete']>;
// Warning: (ae-forgotten-export) The symbol "SavedObjectsFindOptions" needs to be exported by the entry point index.d.ts
find: <T = unknown>(options: SavedObjectsFindOptions_2) => Promise<SavedObjectsFindResponsePublic<T>>;
find: <T = unknown, A = unknown>(options: SavedObjectsFindOptions_2) => Promise<SavedObjectsFindResponsePublic<T, unknown>>;
get: <T = unknown>(type: string, id: string) => Promise<SimpleSavedObject<T>>;
update<T = unknown>(type: string, id: string, attributes: T, { version, migrationVersion, references }?: SavedObjectsUpdateOptions): Promise<SimpleSavedObject<T>>;
}

// @public
export type SavedObjectsClientContract = PublicMethodsOf<SavedObjectsClient>;

// Warning: (ae-missing-release-tag) "SavedObjectsCreateOptions" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export interface SavedObjectsCreateOptions {
id?: string;
Expand All @@ -1107,6 +1109,8 @@ export interface SavedObjectsCreateOptions {

// @public (undocumented)
export interface SavedObjectsFindOptions {
// @alpha
aggs?: Record<string, unknown>;
defaultSearchOperator?: 'AND' | 'OR';
fields?: string[];
// Warning: (ae-forgotten-export) The symbol "KueryNode" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -1143,7 +1147,9 @@ export interface SavedObjectsFindOptionsReference {
}

// @public
export interface SavedObjectsFindResponsePublic<T = unknown> extends SavedObjectsBatchResponse<T> {
export interface SavedObjectsFindResponsePublic<T = unknown, A = unknown> extends SavedObjectsBatchResponse<T> {
// (undocumented)
aggregations?: A;
// (undocumented)
page: number;
// (undocumented)
Expand Down
15 changes: 12 additions & 3 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type SavedObjectsFindOptions = Omit<

type PromiseType<T extends Promise<any>> = T extends Promise<infer U> ? U : never;

/** @public */
export interface SavedObjectsCreateOptions {
/**
* (Not recommended) Specify an id instead of having the saved objects service generate one for you.
Expand Down Expand Up @@ -110,7 +109,9 @@ export interface SavedObjectsDeleteOptions {
*
* @public
*/
export interface SavedObjectsFindResponsePublic<T = unknown> extends SavedObjectsBatchResponse<T> {
export interface SavedObjectsFindResponsePublic<T = unknown, A = unknown>
extends SavedObjectsBatchResponse<T> {
aggregations?: A;
total: number;
perPage: number;
page: number;
Expand Down Expand Up @@ -317,7 +318,7 @@ export class SavedObjectsClient {
* @property {object} [options.hasReference] - { type, id }
* @returns A find result with objects matching the specified search.
*/
public find = <T = unknown>(
public find = <T = unknown, A = unknown>(
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponsePublic<T>> => {
const path = this.getPath(['_find']);
Expand All @@ -333,6 +334,7 @@ export class SavedObjectsClient {
sortField: 'sort_field',
type: 'type',
filter: 'filter',
aggs: 'aggs',
namespaces: 'namespaces',
preference: 'preference',
};
Expand All @@ -349,6 +351,12 @@ export class SavedObjectsClient {
query.has_reference = JSON.stringify(query.has_reference);
}

// `aggs` is a structured object. we need to stringify it before sending it, as `fetch`
// is not doing it implicitly.
if (query.aggs) {
query.aggs = JSON.stringify(query.aggs);
}

const request: ReturnType<SavedObjectsApi['find']> = this.savedObjectsFetch(path, {
method: 'GET',
query,
Expand All @@ -359,6 +367,7 @@ export class SavedObjectsClient {
SavedObjectsFindResponsePublic
>(
{
aggregations: 'aggregations',
XavierM marked this conversation as resolved.
Show resolved Hide resolved
saved_objects: 'savedObjects',
total: 'total',
per_page: 'perPage',
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const registerFindRoute = (router: IRouter) => {
has_reference_operator: searchOperatorSchema,
fields: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])),
filter: schema.maybe(schema.string()),
aggs: schema.maybe(schema.string()),
namespaces: schema.maybe(
schema.oneOf([schema.string(), schema.arrayOf(schema.string())])
),
Expand All @@ -74,6 +75,7 @@ export const registerFindRoute = (router: IRouter) => {
hasReferenceOperator: query.has_reference_operator,
fields: typeof query.fields === 'string' ? [query.fields] : query.fields,
filter: query.filter,
aggs: query.aggs ? JSON.parse(query.aggs) : undefined,
pgayvallet marked this conversation as resolved.
Show resolved Hide resolved
namespaces,
});

Expand Down
Loading