Skip to content

Commit

Permalink
[data.search] Add request handler context and asScoped pattern (#80775)
Browse files Browse the repository at this point in the history
* [Search] Add request context and asScoped pattern

* Update docs

* Unify interface for getting search client

* Update examples/search_examples/server/my_strategy.ts

Co-authored-by: Anton Dosov <[email protected]>

* Review feedback

* Fix checks

* Fix CI

* Fix security search

* Fix test

* Fix test for reals

* Fix types

Co-authored-by: Anton Dosov <[email protected]>
  • Loading branch information
lukasolson and Dosant authored Nov 4, 2020
1 parent 4b9570f commit 44368b0
Show file tree
Hide file tree
Showing 57 changed files with 386 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export interface IKibanaSearchResponse<RawResponse = any>
| [isPartial](./kibana-plugin-plugins-data-public.ikibanasearchresponse.ispartial.md) | <code>boolean</code> | Indicates whether the results returned are complete or partial |
| [isRunning](./kibana-plugin-plugins-data-public.ikibanasearchresponse.isrunning.md) | <code>boolean</code> | Indicates whether search is still in flight |
| [loaded](./kibana-plugin-plugins-data-public.ikibanasearchresponse.loaded.md) | <code>number</code> | If relevant to the search strategy, return a loaded number that represents how progress is indicated. |
| [rawResponse](./kibana-plugin-plugins-data-public.ikibanasearchresponse.rawresponse.md) | <code>RawResponse</code> | |
| [rawResponse](./kibana-plugin-plugins-data-public.ikibanasearchresponse.rawresponse.md) | <code>RawResponse</code> | The raw response returned by the internal search method (usually the raw ES response) |
| [total](./kibana-plugin-plugins-data-public.ikibanasearchresponse.total.md) | <code>number</code> | If relevant to the search strategy, return a total number that represents how progress is indicated. |

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

## IKibanaSearchResponse.rawResponse property

The raw response returned by the internal search method (usually the raw ES response)

<b>Signature:</b>

```typescript
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
| [IndexPatternsContract](./kibana-plugin-plugins-data-public.indexpatternscontract.md) | |
| [IndexPatternSelectProps](./kibana-plugin-plugins-data-public.indexpatternselectprops.md) | |
| [InputTimeRange](./kibana-plugin-plugins-data-public.inputtimerange.md) | |
| [ISearch](./kibana-plugin-plugins-data-public.isearch.md) | |
| [ISearchGeneric](./kibana-plugin-plugins-data-public.isearchgeneric.md) | |
| [ISearchSource](./kibana-plugin-plugins-data-public.isearchsource.md) | search source interface |
| [MatchAllFilter](./kibana-plugin-plugins-data-public.matchallfilter.md) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ search(request: IKibanaSearchRequest, options?: ISearchOptions): Observable<IKib

`Observable<IKibanaSearchResponse>`

`Observalbe` emitting the search response or an error.
`Observable` emitting the search response or an error.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export declare class SearchSource
| [getSerializedFields()](./kibana-plugin-plugins-data-public.searchsource.getserializedfields.md) | | serializes search source fields (which can later be passed to [ISearchStartSearchSource](./kibana-plugin-plugins-data-public.isearchstartsearchsource.md)<!-- -->) |
| [onRequestStart(handler)](./kibana-plugin-plugins-data-public.searchsource.onrequeststart.md) | | Add a handler that will be notified whenever requests start |
| [serialize()](./kibana-plugin-plugins-data-public.searchsource.serialize.md) | | Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.<!-- -->The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named <code>kibanaSavedObjectMeta.searchSourceJSON.index</code> and <code>kibanaSavedObjectMeta.searchSourceJSON.filter[&lt;number&gt;].meta.index</code>.<!-- -->Using <code>createSearchSource</code>, the instance can be re-created. |
| [setField(field, value)](./kibana-plugin-plugins-data-public.searchsource.setfield.md) | | sets value to a single search source feild |
| [setField(field, value)](./kibana-plugin-plugins-data-public.searchsource.setfield.md) | | sets value to a single search source field |
| [setFields(newFields)](./kibana-plugin-plugins-data-public.searchsource.setfields.md) | | Internal, do not use. Overrides all search source fields with the new field array. |
| [setParent(parent, options)](./kibana-plugin-plugins-data-public.searchsource.setparent.md) | | Set a searchSource that this source should inherit from |
| [setPreferredSearchStrategyId(searchStrategyId)](./kibana-plugin-plugins-data-public.searchsource.setpreferredsearchstrategyid.md) | | internal, dont use |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Using `createSearchSource`<!-- -->, the instance can be re-created.
```typescript
serialize(): {
searchSourceJSON: string;
references: import("../../../../../core/types").SavedObjectReference[];
references: import("src/core/server").SavedObjectReference[];
};
```
<b>Returns:</b>

`{
searchSourceJSON: string;
references: import("../../../../../core/types").SavedObjectReference[];
references: import("src/core/server").SavedObjectReference[];
}`

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

## SearchSource.setField() method

sets value to a single search source feild
sets value to a single search source field

<b>Signature:</b>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +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; [ISearchStart](./kibana-plugin-plugins-data-server.isearchstart.md) &gt; [search](./kibana-plugin-plugins-data-server.isearchstart.search.md)
[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; [asScoped](./kibana-plugin-plugins-data-server.isearchstart.asscoped.md)

## ISearchStart.search property
## ISearchStart.asScoped property

<b>Signature:</b>

```typescript
search: ISearchStrategy['search'];
asScoped: (request: KibanaRequest) => ISearchClient;
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

## 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.
Get other registered search strategies by name (or, by default, the Elasticsearch strategy). 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: (name: string) => ISearchStrategy<SearchStrategyRequest, SearchStrategyResponse>;
getSearchStrategy: (name?: string) => ISearchStrategy<SearchStrategyRequest, SearchStrategyResponse>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ISearchStart<SearchStrategyRequest extends IKibanaSearchRequest
| Property | Type | Description |
| --- | --- | --- |
| [aggs](./kibana-plugin-plugins-data-server.isearchstart.aggs.md) | <code>AggsStart</code> | |
| [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md) | <code>(name: string) =&gt; ISearchStrategy&lt;SearchStrategyRequest, SearchStrategyResponse&gt;</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. |
| [search](./kibana-plugin-plugins-data-server.isearchstart.search.md) | <code>ISearchStrategy['search']</code> | |
| [asScoped](./kibana-plugin-plugins-data-server.isearchstart.asscoped.md) | <code>(request: KibanaRequest) =&gt; ISearchClient</code> | |
| [getSearchStrategy](./kibana-plugin-plugins-data-server.isearchstart.getsearchstrategy.md) | <code>(name?: string) =&gt; ISearchStrategy&lt;SearchStrategyRequest, SearchStrategyResponse&gt;</code> | Get other registered search strategies by name (or, by default, the Elasticsearch strategy). For example, if a new strategy needs to use the already-registered ES search strategy, it can use this function to accomplish that. |
| [searchSource](./kibana-plugin-plugins-data-server.isearchstart.searchsource.md) | <code>{</code><br/><code> asScoped: (request: KibanaRequest) =&gt; Promise&lt;ISearchStartSearchSource&gt;;</code><br/><code> }</code> | |

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

```typescript
cancel?: (context: RequestHandlerContext, id: string) => Promise<void>;
cancel?: (id: string, options: ISearchOptions, deps: SearchStrategyDependencies) => Promise<void>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export interface ISearchStrategy<SearchStrategyRequest extends IKibanaSearchRequ

| Property | Type | Description |
| --- | --- | --- |
| [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md) | <code>(context: RequestHandlerContext, id: string) =&gt; Promise&lt;void&gt;</code> | |
| [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md) | <code>(request: SearchStrategyRequest, options: ISearchOptions, context: RequestHandlerContext) =&gt; Observable&lt;SearchStrategyResponse&gt;</code> | |
| [cancel](./kibana-plugin-plugins-data-server.isearchstrategy.cancel.md) | <code>(id: string, options: ISearchOptions, deps: SearchStrategyDependencies) =&gt; Promise&lt;void&gt;</code> | |
| [search](./kibana-plugin-plugins-data-server.isearchstrategy.search.md) | <code>(request: SearchStrategyRequest, options: ISearchOptions, deps: SearchStrategyDependencies) =&gt; Observable&lt;SearchStrategyResponse&gt;</code> | |

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

```typescript
search: (request: SearchStrategyRequest, options: ISearchOptions, context: RequestHandlerContext) => Observable<SearchStrategyResponse>;
search: (request: SearchStrategyRequest, options: ISearchOptions, deps: SearchStrategyDependencies) => Observable<SearchStrategyResponse>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
| [PluginSetup](./kibana-plugin-plugins-data-server.pluginsetup.md) | |
| [PluginStart](./kibana-plugin-plugins-data-server.pluginstart.md) | |
| [RefreshInterval](./kibana-plugin-plugins-data-server.refreshinterval.md) | |
| [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md) | |
| [SearchUsage](./kibana-plugin-plugins-data-server.searchusage.md) | |
| [TabbedAggColumn](./kibana-plugin-plugins-data-server.tabbedaggcolumn.md) | \* |
| [TabbedTable](./kibana-plugin-plugins-data-server.tabbedtable.md) | \* |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
```typescript
start(core: CoreStart): {
fieldFormats: {
fieldFormatServiceFactory: (uiSettings: import("../../../core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
fieldFormatServiceFactory: (uiSettings: import("src/core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
indexPatterns: {
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("../../../core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../public").IndexPatternsService>;
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../public").IndexPatternsService>;
};
search: ISearchStart<import("./search").IEsSearchRequest, import("./search").IEsSearchResponse<any>>;
};
Expand All @@ -28,10 +28,10 @@ start(core: CoreStart): {

`{
fieldFormats: {
fieldFormatServiceFactory: (uiSettings: import("../../../core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
fieldFormatServiceFactory: (uiSettings: import("src/core/server").IUiSettingsClient) => Promise<import("../common").FieldFormatsRegistry>;
};
indexPatterns: {
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("../../../core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../public").IndexPatternsService>;
indexPatternsServiceFactory: (savedObjectsClient: Pick<import("src/core/server").SavedObjectsClient, "update" | "find" | "get" | "delete" | "errors" | "create" | "bulkCreate" | "checkConflicts" | "bulkGet" | "addToNamespaces" | "deleteFromNamespaces" | "bulkUpdate" | "removeReferencesTo">) => Promise<import("../public").IndexPatternsService>;
};
search: ISearchStart<import("./search").IEsSearchRequest, import("./search").IEsSearchResponse<any>>;
}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ search: {
utils: {
doSearch: <SearchResponse = any>(searchMethod: () => Promise<SearchResponse>, abortSignal?: AbortSignal | undefined) => import("rxjs").Observable<SearchResponse>;
shimAbortSignal: <T extends import("../common").TransportRequestPromise<unknown>>(promise: T, signal: AbortSignal | undefined) => T;
trackSearchStatus: <KibanaResponse extends import("../common").IKibanaSearchResponse<any> = import("./search").IEsSearchResponse<import("../../../core/server").SearchResponse<unknown>>>(logger: import("@kbn/logging/target/logger").Logger, usage?: import("./search").SearchUsage | undefined) => import("rxjs").UnaryFunction<import("rxjs").Observable<KibanaResponse>, import("rxjs").Observable<KibanaResponse>>;
trackSearchStatus: <KibanaResponse extends import("../common").IKibanaSearchResponse<any> = import("./search").IEsSearchResponse<import("src/core/server").SearchResponse<unknown>>>(logger: import("src/core/server").Logger, usage?: import("./search").SearchUsage | undefined) => import("rxjs").UnaryFunction<import("rxjs").Observable<KibanaResponse>, import("rxjs").Observable<KibanaResponse>>;
includeTotalLoaded: () => import("rxjs").OperatorFunction<import("../common").IKibanaSearchResponse<import("elasticsearch").SearchResponse<unknown>>, {
total: number;
loaded: number;
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-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md) &gt; [esClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.esclient.md)

## SearchStrategyDependencies.esClient property

<b>Signature:</b>

```typescript
esClient: IScopedClusterClient;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- 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; [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md)

## SearchStrategyDependencies interface

<b>Signature:</b>

```typescript
export interface SearchStrategyDependencies
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [esClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.esclient.md) | <code>IScopedClusterClient</code> | |
| [savedObjectsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.savedobjectsclient.md) | <code>SavedObjectsClientContract</code> | |
| [uiSettingsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.uisettingsclient.md) | <code>IUiSettingsClient</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; [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md) &gt; [savedObjectsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.savedobjectsclient.md)

## SearchStrategyDependencies.savedObjectsClient property

<b>Signature:</b>

```typescript
savedObjectsClient: SavedObjectsClientContract;
```
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; [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md) &gt; [uiSettingsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.uisettingsclient.md)

## SearchStrategyDependencies.uiSettingsClient property

<b>Signature:</b>

```typescript
uiSettingsClient: IUiSettingsClient;
```
10 changes: 5 additions & 5 deletions examples/search_examples/server/my_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import { IMyStrategyResponse, IMyStrategyRequest } from '../common';
export const mySearchStrategyProvider = (
data: PluginStart
): ISearchStrategy<IMyStrategyRequest, IMyStrategyResponse> => {
const es = data.search.getSearchStrategy('es');
const es = data.search.getSearchStrategy();
return {
search: (request, options, context) =>
es.search(request, options, context).pipe(
search: (request, options, deps) =>
es.search(request, options, deps).pipe(
map((esSearchRes) => ({
...esSearchRes,
cool: request.get_cool ? 'YES' : 'NOPE',
}))
),
cancel: async (context, id) => {
cancel: async (id, options, deps) => {
if (es.cancel) {
es.cancel(context, id);
await es.cancel(id, options, deps);
}
},
};
Expand Down
7 changes: 3 additions & 4 deletions examples/search_examples/server/routes/server_search_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function registerServerSearchRoute(router: IRouter, data: DataPluginStart
// Run a synchronous search server side, by enforcing a high keepalive and waiting for completion.
// If you wish to run the search with polling (in basic+), you'd have to poll on the search API.
// Please reach out to the @app-arch-team if you need this to be implemented.
const res = await data.search
.search(
const res = await context
.search!.search(
{
params: {
index,
Expand All @@ -57,8 +57,7 @@ export function registerServerSearchRoute(router: IRouter, data: DataPluginStart
keepAlive: '5m',
},
} as IEsSearchRequest,
{},
context
{}
)
.toPromise();

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/common/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { Assign, Ensure } from '@kbn/utility-types';

import { ISearchSource } from 'src/plugins/data/public';
import { ISearchOptions, ISearchSource } from 'src/plugins/data/public';
import {
ExpressionAstFunction,
ExpressionAstArgument,
SerializedFieldFormat,
} from 'src/plugins/expressions/common';
import { ISearchOptions } from '../es_search';

import { IAggType } from './agg_type';
import { writeParams } from './agg_params';
Expand Down
16 changes: 0 additions & 16 deletions src/plugins/data/common/search/es_search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ import { IKibanaSearchRequest, IKibanaSearchResponse } from '../types';

export const ES_SEARCH_STRATEGY = 'es';

export interface ISearchOptions {
/**
* An `AbortSignal` that allows the caller of `search` to abort a search request.
*/
abortSignal?: AbortSignal;
/**
* Use this option to force using a specific server side search strategy. Leave empty to use the default strategy.
*/
strategy?: string;

/**
* A session ID, grouping multiple search requests into a single session.
*/
sessionId?: string;
}

export type ISearchRequestParams<T = Record<string, any>> = {
trackTotalHits?: boolean;
} & Search<T>;
Expand Down
Loading

0 comments on commit 44368b0

Please sign in to comment.