Skip to content

Commit

Permalink
[Search service] Refactor the way server-side search strategies are r…
Browse files Browse the repository at this point in the history
…egistered (#68452) (#69331)

* [search] Refactor the way search strategies are registered/retrieved on the server

* Fix types and tests and update docs

* Fix failing test

* Fix build of example plugin

* Fix functional test

* Make server strategies sync

Co-authored-by: Liza K <liza.katz@elastic.co>

Co-authored-by: Lukas Olson <olson.lukas@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 17, 2020
1 parent fea77cd commit 48278a7
Show file tree
Hide file tree
Showing 52 changed files with 552 additions and 741 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## IRequestTypesMap interface

The map of search strategy IDs to the corresponding request type definitions.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## IResponseTypesMap interface

The map of search strategy IDs to the corresponding response type definitions.

<b>Signature:</b>

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

```typescript
export declare type ISearch<T extends TStrategyTypes> = (request: IRequestTypesMap[T], options?: ISearchOptions) => Promise<IResponseTypesMap[T]>;
export declare type ISearch<T extends TStrategyTypes> = (context: RequestHandlerContext, request: IRequestTypesMap[T], options?: ISearchOptions) => Promise<IResponseTypesMap[T]>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
export declare type ISearchCancel<T extends TStrategyTypes> = (id: string) => Promise<void>;
export declare type ISearchCancel<T extends TStrategyTypes> = (context: RequestHandlerContext, id: string) => Promise<void>;
```

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export interface ISearchOptions

| Property | Type | Description |
| --- | --- | --- |
| [signal](./kibana-plugin-plugins-data-server.isearchoptions.signal.md) | <code>AbortSignal</code> | |
| [signal](./kibana-plugin-plugins-data-server.isearchoptions.signal.md) | <code>AbortSignal</code> | An <code>AbortSignal</code> that allows the caller of <code>search</code> to abort a search request. |

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## ISearchOptions.signal property

An `AbortSignal` that allows the caller of `search` to abort a search request.

<b>Signature:</b>

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

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchSetup](./kibana-plugin-plugins-data-server.isearchsetup.md)

## ISearchSetup interface

<b>Signature:</b>

```typescript
export interface ISearchSetup
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [registerSearchStrategy](./kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md) | <code>TRegisterSearchStrategy</code> | Extension point exposed for other plugins to register their own search strategies. |

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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchSetup](./kibana-plugin-plugins-data-server.isearchsetup.md) &gt; [registerSearchStrategy](./kibana-plugin-plugins-data-server.isearchsetup.registersearchstrategy.md)

## ISearchSetup.registerSearchStrategy property

Extension point exposed for other plugins to register their own search strategies.

<b>Signature:</b>

```typescript
registerSearchStrategy: TRegisterSearchStrategy;
```
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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md) &gt; [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md)

## ISearchStart.getSearchStrategy property

Get other registered search strategies. For example, if a new strategy needs to use the already-registered ES search strategy, it can use this function to accomplish that.

<b>Signature:</b>

```typescript
getSearchStrategy: TGetSearchStrategy;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md)

## ISearchStart interface

<b>Signature:</b>

```typescript
export interface ISearchStart
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md) | <code>TGetSearchStrategy</code> | Get other registered search strategies. For example, if a new strategy needs to use the already-registered ES search strategy, it can use this function to accomplish that. |

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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchStrategy](./kibana-plugin-plugins-data-server.isearchstrategy.md) &gt; [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md)

## ISearchStrategy.cancel property

<b>Signature:</b>

```typescript
cancel?: ISearchCancel<T>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchStrategy](./kibana-plugin-plugins-data-server.isearchstrategy.md)

## ISearchStrategy interface

Search strategy interface contains a search method that takes in a request and returns a promise that resolves to a response.

<b>Signature:</b>

```typescript
export interface ISearchStrategy<T extends TStrategyTypes>
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md) | <code>ISearchCancel&lt;T&gt;</code> | |
| [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md) | <code>ISearch&lt;T&gt;</code> | |

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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [ISearchStrategy](./kibana-plugin-plugins-data-server.isearchstrategy.md) &gt; [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md)

## ISearchStrategy.search property

<b>Signature:</b>

```typescript
search: ISearch<T>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
| [IIndexPattern](./kibana-plugin-plugins-data-server.iindexpattern.md) | |
| [IndexPatternAttributes](./kibana-plugin-plugins-data-server.indexpatternattributes.md) | Use data plugin interface instead |
| [IndexPatternFieldDescriptor](./kibana-plugin-plugins-data-server.indexpatternfielddescriptor.md) | |
| [IRequestTypesMap](./kibana-plugin-plugins-data-server.irequesttypesmap.md) | |
| [IResponseTypesMap](./kibana-plugin-plugins-data-server.iresponsetypesmap.md) | |
| [ISearchContext](./kibana-plugin-plugins-data-server.isearchcontext.md) | |
| [IRequestTypesMap](./kibana-plugin-plugins-data-server.irequesttypesmap.md) | The map of search strategy IDs to the corresponding request type definitions. |
| [IResponseTypesMap](./kibana-plugin-plugins-data-server.iresponsetypesmap.md) | The map of search strategy IDs to the corresponding response type definitions. |
| [ISearchOptions](./kibana-plugin-plugins-data-server.isearchoptions.md) | |
| [ISearchSetup](./kibana-plugin-plugins-data-server.isearchsetup.md) | |
| [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md) | |
| [ISearchStrategy](./kibana-plugin-plugins-data-server.isearchstrategy.md) | Search strategy interface contains a search method that takes in a request and returns a promise that resolves to a response. |
| [KueryNode](./kibana-plugin-plugins-data-server.kuerynode.md) | |
| [PluginSetup](./kibana-plugin-plugins-data-server.pluginsetup.md) | |
| [PluginStart](./kibana-plugin-plugins-data-server.pluginstart.md) | |
Expand Down Expand Up @@ -73,5 +75,5 @@
| [ISearch](./kibana-plugin-plugins-data-server.isearch.md) | |
| [ISearchCancel](./kibana-plugin-plugins-data-server.isearchcancel.md) | |
| [ParsedInterval](./kibana-plugin-plugins-data-server.parsedinterval.md) | |
| [TSearchStrategyProvider](./kibana-plugin-plugins-data-server.tsearchstrategyprovider.md) | Search strategy provider creates an instance of a search strategy with the request handler context bound to it. This way every search strategy can use whatever information they require from the request context. |
| [TStrategyTypes](./kibana-plugin-plugins-data-server.tstrategytypes.md) | Contains all known strategy type identifiers that will be used to map to request and response shapes. Plugins that wish to add their own custom search strategies should extend this type via:<!-- -->const MY\_STRATEGY = 'MY\_STRATEGY';<!-- -->declare module 'src/plugins/search/server' { export interface IRequestTypesMap { \[MY\_STRATEGY\]: IMySearchRequest; }<!-- -->export interface IResponseTypesMap { \[MY\_STRATEGY\]: IMySearchResponse } } |

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

```typescript
setup(core: CoreSetup, { usageCollection }: DataPluginSetupDependencies): {
setup(core: CoreSetup<object, DataPluginStart>, { usageCollection }: DataPluginSetupDependencies): {
search: ISearchSetup;
fieldFormats: {
register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number;
};
search: ISearchSetup;
};
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| core | <code>CoreSetup</code> | |
| core | <code>CoreSetup&lt;object, DataPluginStart&gt;</code> | |
| { usageCollection } | <code>DataPluginSetupDependencies</code> | |

<b>Returns:</b>

`{
search: ISearchSetup;
fieldFormats: {
register: (customFieldFormat: import("../public").FieldFormatInstanceType) => number;
};
search: ISearchSetup;
}`

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

```typescript
start(core: CoreStart): {
search: ISearchStart;
fieldFormats: {
fieldFormatServiceFactory: (uiSettings: import("kibana/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
fieldFormatServiceFactory: (uiSettings: import("../../../core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
};
```
Expand All @@ -23,8 +24,9 @@ start(core: CoreStart): {
<b>Returns:</b>

`{
search: ISearchStart;
fieldFormats: {
fieldFormatServiceFactory: (uiSettings: import("kibana/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
fieldFormatServiceFactory: (uiSettings: import("../../../core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
}`

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface DataPluginStart
| Property | Type | Description |
| --- | --- | --- |
| [fieldFormats](./kibana-plugin-plugins-data-server.pluginstart.fieldformats.md) | <code>FieldFormatsStart</code> | |
| [search](./kibana-plugin-plugins-data-server.pluginstart.search.md) | <code>ISearchStart</code> | |

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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [PluginStart](./kibana-plugin-plugins-data-server.pluginstart.md) &gt; [search](./kibana-plugin-plugins-data-server.pluginstart.search.md)

## PluginStart.search property

<b>Signature:</b>

```typescript
search: ISearchStart;
```

This file was deleted.

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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [TStrategyTypes](./kibana-plugin-plugins-data-server.tstrategytypes.md)

## TStrategyTypes type

Contains all known strategy type identifiers that will be used to map to request and response shapes. Plugins that wish to add their own custom search strategies should extend this type via:

const MY\_STRATEGY = 'MY\_STRATEGY';

declare module 'src/plugins/search/server' { export interface IRequestTypesMap { \[MY\_STRATEGY\]: IMySearchRequest; }

export interface IResponseTypesMap { \[MY\_STRATEGY\]: IMySearchResponse } }

<b>Signature:</b>

```typescript
export declare type TStrategyTypes = typeof ES_SEARCH_STRATEGY | string;
```
50 changes: 26 additions & 24 deletions examples/demo_search/server/async_demo_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,32 @@
* under the License.
*/

import { TSearchStrategyProvider } from '../../../src/plugins/data/server';
import { ASYNC_DEMO_SEARCH_STRATEGY } from '../common';

function getFibonacciSequence(n = 0) {
const beginning = [0, 1].slice(0, n);
return Array(Math.max(0, n))
.fill(null)
.reduce((sequence, value, i) => {
if (i < 2) return sequence;
return [...sequence, sequence[i - 1] + sequence[i - 2]];
}, beginning);
}

const generateId = (() => {
let id = 0;
return () => `${id++}`;
})();

const loadedMap = new Map<string, number>();
const totalMap = new Map<string, number>();

export const asyncDemoSearchStrategyProvider: TSearchStrategyProvider<typeof ASYNC_DEMO_SEARCH_STRATEGY> = () => {
import { ISearchStrategy } from '../../../src/plugins/data/server';
import { ASYNC_DEMO_SEARCH_STRATEGY, IAsyncDemoRequest } from '../common';

export const asyncDemoSearchStrategyProvider = (): ISearchStrategy<
typeof ASYNC_DEMO_SEARCH_STRATEGY
> => {
function getFibonacciSequence(n = 0) {
const beginning = [0, 1].slice(0, n);
return Array(Math.max(0, n))
.fill(null)
.reduce((sequence, value, i) => {
if (i < 2) return sequence;
return [...sequence, sequence[i - 1] + sequence[i - 2]];
}, beginning);
}

const generateId = (() => {
let id = 0;
return () => `${id++}`;
})();

const loadedMap = new Map<string, number>();
const totalMap = new Map<string, number>();

return {
search: async (request) => {
search: async (context, request: IAsyncDemoRequest) => {
const id = request.id ?? generateId();

const loaded = (loadedMap.get(id) ?? 0) + 1;
Expand All @@ -52,7 +54,7 @@ export const asyncDemoSearchStrategyProvider: TSearchStrategyProvider<typeof ASY
const fibonacciSequence = getFibonacciSequence(loaded);
return { id, total, loaded, fibonacciSequence };
},
cancel: async (id) => {
cancel: async (context, id) => {
loadedMap.delete(id);
totalMap.delete(id);
},
Expand Down
Loading

0 comments on commit 48278a7

Please sign in to comment.