Skip to content

Commit

Permalink
savedObjects: add score to repository.find results (#68894) (#69486)
Browse files Browse the repository at this point in the history
* add `score` to repository.find results

* update generated doc

* fix FTR result set

* remove score from exports

* fix FTR for find API

* fix label

* fix tsdoc
  • Loading branch information
pgayvallet authored Jun 18, 2020
1 parent bd4b72f commit 543319f
Show file tree
Hide file tree
Showing 26 changed files with 123 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsExportResultDetails](./kibana-plugin-core-server.savedobjectsexportresultdetails.md) | Structure of the export result details entry |
| [SavedObjectsFindOptions](./kibana-plugin-core-server.savedobjectsfindoptions.md) | |
| [SavedObjectsFindResponse](./kibana-plugin-core-server.savedobjectsfindresponse.md) | Return type of the Saved Objects <code>find()</code> method.<!-- -->\*Note\*: this type is different between the Public and Server Saved Objects clients. |
| [SavedObjectsFindResult](./kibana-plugin-core-server.savedobjectsfindresult.md) | |
| [SavedObjectsImportConflictError](./kibana-plugin-core-server.savedobjectsimportconflicterror.md) | Represents a failure to import due to a conflict. |
| [SavedObjectsImportError](./kibana-plugin-core-server.savedobjectsimporterror.md) | Represents a failure to import. |
| [SavedObjectsImportMissingReferencesError](./kibana-plugin-core-server.savedobjectsimportmissingreferenceserror.md) | Represents a failure to import due to missing references. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export interface SavedObjectsFindResponse<T = unknown>
| --- | --- | --- |
| [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;SavedObject&lt;T&gt;&gt;</code> | |
| [saved\_objects](./kibana-plugin-core-server.savedobjectsfindresponse.saved_objects.md) | <code>Array&lt;SavedObjectsFindResult&lt;T&gt;&gt;</code> | |
| [total](./kibana-plugin-core-server.savedobjectsfindresponse.total.md) | <code>number</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
saved_objects: Array<SavedObject<T>>;
saved_objects: Array<SavedObjectsFindResult<T>>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- 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; [SavedObjectsFindResult](./kibana-plugin-core-server.savedobjectsfindresult.md)

## SavedObjectsFindResult interface


<b>Signature:</b>

```typescript
export interface SavedObjectsFindResult<T = unknown> extends SavedObject<T>
```
## Properties
| Property | Type | Description |
| --- | --- | --- |
| [score](./kibana-plugin-core-server.savedobjectsfindresult.score.md) | <code>number</code> | The Elasticsearch <code>_score</code> of this result. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- 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; [SavedObjectsFindResult](./kibana-plugin-core-server.savedobjectsfindresult.md) &gt; [score](./kibana-plugin-core-server.savedobjectsfindresult.score.md)

## SavedObjectsFindResult.score property

The Elasticsearch `_score` of this result.

<b>Signature:</b>

```typescript
score: number;
```
6 changes: 4 additions & 2 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ export class SavedObjectsClient {
query,
});
return request.then((resp) => {
resp.saved_objects = resp.saved_objects.map((d) => this.createSavedObject(d));
return renameKeys<
PromiseType<ReturnType<SavedObjectsApi['find']>>,
SavedObjectsFindResponsePublic
Expand All @@ -314,7 +313,10 @@ export class SavedObjectsClient {
per_page: 'perPage',
page: 'page',
},
resp
{
...resp,
saved_objects: resp.saved_objects.map((d) => this.createSavedObject(d)),
}
) as SavedObjectsFindResponsePublic<T>;
});
};
Expand Down
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export {
SavedObjectsErrorHelpers,
SavedObjectsExportOptions,
SavedObjectsExportResultDetails,
SavedObjectsFindResult,
SavedObjectsFindResponse,
SavedObjectsImportConflictError,
SavedObjectsImportError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('getSortedObjectsForExport()', () => {
id: '2',
type: 'search',
attributes: {},
score: 1,
references: [
{
name: 'name',
Expand All @@ -59,6 +60,7 @@ describe('getSortedObjectsForExport()', () => {
id: '1',
type: 'index-pattern',
attributes: {},
score: 1,
references: [],
},
],
Expand Down Expand Up @@ -133,6 +135,7 @@ describe('getSortedObjectsForExport()', () => {
id: '2',
type: 'search',
attributes: {},
score: 1,
references: [
{
name: 'name',
Expand All @@ -145,6 +148,7 @@ describe('getSortedObjectsForExport()', () => {
id: '1',
type: 'index-pattern',
attributes: {},
score: 1,
references: [],
},
],
Expand Down Expand Up @@ -192,6 +196,7 @@ describe('getSortedObjectsForExport()', () => {
id: '2',
type: 'search',
attributes: {},
score: 1,
references: [
{
name: 'name',
Expand All @@ -204,6 +209,7 @@ describe('getSortedObjectsForExport()', () => {
id: '1',
type: 'index-pattern',
attributes: {},
score: 1,
references: [],
},
],
Expand Down Expand Up @@ -279,6 +285,7 @@ describe('getSortedObjectsForExport()', () => {
id: '2',
type: 'search',
attributes: {},
score: 1,
references: [
{
name: 'name',
Expand All @@ -291,6 +298,7 @@ describe('getSortedObjectsForExport()', () => {
id: '1',
type: 'index-pattern',
attributes: {},
score: 1,
references: [],
},
],
Expand Down Expand Up @@ -366,6 +374,7 @@ describe('getSortedObjectsForExport()', () => {
id: '2',
type: 'search',
attributes: {},
score: 1,
references: [
{
type: 'index-pattern',
Expand All @@ -378,6 +387,7 @@ describe('getSortedObjectsForExport()', () => {
id: '1',
type: 'index-pattern',
attributes: {},
score: 1,
references: [],
},
],
Expand Down Expand Up @@ -405,6 +415,7 @@ describe('getSortedObjectsForExport()', () => {
attributes: {
name: 'baz',
},
score: 1,
references: [],
},
{
Expand All @@ -413,6 +424,7 @@ describe('getSortedObjectsForExport()', () => {
attributes: {
name: 'foo',
},
score: 1,
references: [],
},
{
Expand All @@ -421,6 +433,7 @@ describe('getSortedObjectsForExport()', () => {
attributes: {
name: 'bar',
},
score: 1,
references: [],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ async function fetchObjectsToExport({
}

// sorts server-side by _id, since it's only available in fielddata
return findResponse.saved_objects.sort((a: SavedObject, b: SavedObject) =>
a.id > b.id ? 1 : -1
return (
findResponse.saved_objects
// exclude the find-specific `score` property from the exported objects
.map(({ score, ...obj }) => obj)
.sort((a: SavedObject, b: SavedObject) => (a.id > b.id ? 1 : -1))
);
} else {
throw Boom.badRequest('Either `type` or `objects` are required.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('GET /api/saved_objects/_find', () => {
timeFieldName: '@timestamp',
notExpandable: true,
attributes: {},
score: 1,
references: [],
},
{
Expand All @@ -88,6 +89,7 @@ describe('GET /api/saved_objects/_find', () => {
timeFieldName: '@timestamp',
notExpandable: true,
attributes: {},
score: 1,
references: [],
},
],
Expand Down
8 changes: 5 additions & 3 deletions src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ describe('SavedObjectsRepository', () => {
{
_index: '.kibana',
_id: `${namespace ? `${namespace}:` : ''}config:6.0.0-alpha1`,
_score: 1,
_score: 2,
...mockVersionProps,
_source: {
namespace,
Expand All @@ -1954,7 +1954,7 @@ describe('SavedObjectsRepository', () => {
{
_index: '.kibana',
_id: `${namespace ? `${namespace}:` : ''}index-pattern:stocks-*`,
_score: 1,
_score: 3,
...mockVersionProps,
_source: {
namespace,
Expand All @@ -1970,7 +1970,7 @@ describe('SavedObjectsRepository', () => {
{
_index: '.kibana',
_id: `${NAMESPACE_AGNOSTIC_TYPE}:something`,
_score: 1,
_score: 4,
...mockVersionProps,
_source: {
type: NAMESPACE_AGNOSTIC_TYPE,
Expand Down Expand Up @@ -2131,6 +2131,7 @@ describe('SavedObjectsRepository', () => {
type: doc._source.type,
...mockTimestampFields,
version: mockVersion,
score: doc._score,
attributes: doc._source[doc._source.type],
references: [],
});
Expand All @@ -2153,6 +2154,7 @@ describe('SavedObjectsRepository', () => {
type: doc._source.type,
...mockTimestampFields,
version: mockVersion,
score: doc._score,
attributes: doc._source[doc._source.type],
references: [],
});
Expand Down
8 changes: 6 additions & 2 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
SavedObjectsBulkUpdateResponse,
SavedObjectsCreateOptions,
SavedObjectsFindResponse,
SavedObjectsFindResult,
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
SavedObjectsBulkUpdateObject,
Expand Down Expand Up @@ -673,8 +674,11 @@ export class SavedObjectsRepository {
page,
per_page: perPage,
total: response.hits.total,
saved_objects: response.hits.hits.map((hit: SavedObjectsRawDoc) =>
this._rawToSavedObject(hit)
saved_objects: response.hits.hits.map(
(hit: SavedObjectsRawDoc): SavedObjectsFindResult => ({
...this._rawToSavedObject(hit),
score: (hit as any)._score,
})
),
};
}
Expand Down
13 changes: 12 additions & 1 deletion src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export interface SavedObjectsBulkResponse<T = unknown> {
saved_objects: Array<SavedObject<T>>;
}

/**
*
* @public
*/
export interface SavedObjectsFindResult<T = unknown> extends SavedObject<T> {
/**
* The Elasticsearch `_score` of this result.
*/
score: number;
}

/**
* Return type of the Saved Objects `find()` method.
*
Expand All @@ -88,7 +99,7 @@ export interface SavedObjectsBulkResponse<T = unknown> {
* @public
*/
export interface SavedObjectsFindResponse<T = unknown> {
saved_objects: Array<SavedObject<T>>;
saved_objects: Array<SavedObjectsFindResult<T>>;
total: number;
per_page: number;
page: number;
Expand Down
7 changes: 6 additions & 1 deletion src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2039,11 +2039,16 @@ export interface SavedObjectsFindResponse<T = unknown> {
// (undocumented)
per_page: number;
// (undocumented)
saved_objects: Array<SavedObject<T>>;
saved_objects: Array<SavedObjectsFindResult<T>>;
// (undocumented)
total: number;
}

// @public (undocumented)
export interface SavedObjectsFindResult<T = unknown> extends SavedObject<T> {
score: number;
}

// @public
export interface SavedObjectsImportConflictError {
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
*/

import { times } from 'lodash';
import { SavedObjectsFindOptions, SavedObject } from 'src/core/server';
import { SavedObjectsFindOptions, SavedObjectsFindResult } from 'src/core/server';
import { savedObjectsClientMock } from '../../../../core/server/mocks';
import { findAll } from './find_all';

describe('findAll', () => {
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;

const createObj = (id: number): SavedObject => ({
const createObj = (id: number): SavedObjectsFindResult => ({
type: 'type',
id: `id-${id}`,
attributes: {},
score: 1,
references: [],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('findRelationships', () => {
type: 'parent-type',
id: 'parent-id',
attributes: {},
score: 1,
references: [],
},
],
Expand Down
2 changes: 2 additions & 0 deletions test/api_integration/apis/saved_objects/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function ({ getService }) {
attributes: {
title: 'Count of requests',
},
score: 0,
migrationVersion: resp.body.saved_objects[0].migrationVersion,
references: [
{
Expand Down Expand Up @@ -134,6 +135,7 @@ export default function ({ getService }) {
.searchSourceJSON,
},
},
score: 0,
references: [
{
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
Expand Down
1 change: 1 addition & 0 deletions test/api_integration/apis/saved_objects_management/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default function ({ getService }: FtrProviderContext) {
type: 'index-pattern',
},
],
score: 0,
updated_at: '2017-09-21T18:51:23.794Z',
meta: {
editUrl:
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ describe('getAll()', () => {
foo: 'bar',
},
},
score: 1,
references: [],
},
],
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerts/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,7 @@ describe('find()', () => {
},
],
},
score: 1,
references: [
{
name: 'action_0',
Expand Down
Loading

0 comments on commit 543319f

Please sign in to comment.