Skip to content

Commit

Permalink
[data.search] Add Kibana request to search strategy dependencies (ela…
Browse files Browse the repository at this point in the history
…stic#98566)

* [data.search] Add Kibana request to search strategy dependencies

* Don't register internal strategy

* Update docs
  • Loading branch information
lukasolson authored and ecezalp committed May 26, 2021
1 parent de4d64b commit d5794b5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SearchStrategyDependencies
| Property | Type | Description |
| --- | --- | --- |
| [esClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.esclient.md) | <code>IScopedClusterClient</code> | |
| [request](./kibana-plugin-plugins-data-server.searchstrategydependencies.request.md) | <code>KibanaRequest</code> | |
| [savedObjectsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.savedobjectsclient.md) | <code>SavedObjectsClientContract</code> | |
| [searchSessionsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.searchsessionsclient.md) | <code>IScopedSearchSessionsClient</code> | |
| [uiSettingsClient](./kibana-plugin-plugins-data-server.searchstrategydependencies.uisettingsclient.md) | <code>IUiSettingsClient</code> | |
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
savedObjectsClient,
esClient: elasticsearch.client.asScoped(request),
uiSettingsClient: uiSettings.asScopedToClient(savedObjectsClient),
request,
};
return {
search: <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ import {
export const enhancedEsSearchStrategyProvider = (
legacyConfig$: Observable<SharedGlobalConfig>,
logger: Logger,
usage?: SearchUsage
usage?: SearchUsage,
useInternalUser: boolean = false
): ISearchStrategy => {
async function cancelAsyncSearch(id: string, esClient: IScopedClusterClient) {
try {
await esClient.asCurrentUser.asyncSearch.delete({ id });
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;
await client.asyncSearch.delete({ id });
} catch (e) {
throw getKbnServerError(e);
}
Expand All @@ -53,7 +55,7 @@ export const enhancedEsSearchStrategyProvider = (
options: IAsyncSearchOptions,
{ esClient, uiSettingsClient, searchSessionsClient }: SearchStrategyDependencies
) {
const client = esClient.asCurrentUser.asyncSearch;
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;

const search = async () => {
const params = id
Expand All @@ -66,7 +68,9 @@ export const enhancedEsSearchStrategyProvider = (
)),
...request.params,
};
const promise = id ? client.get({ ...params, id }) : client.submit(params);
const promise = id
? client.asyncSearch.get({ ...params, id })
: client.asyncSearch.submit(params);
const { body } = await shimAbortSignal(promise, options.abortSignal);
const response = shimHitsTotal(body.response, options);

Expand Down Expand Up @@ -96,6 +100,7 @@ export const enhancedEsSearchStrategyProvider = (
options: ISearchOptions,
{ esClient, uiSettingsClient }: SearchStrategyDependencies
): Promise<IEsSearchResponse> {
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;
const legacyConfig = await legacyConfig$.pipe(first()).toPromise();
const { body, index, ...params } = request.params!;
const method = 'POST';
Expand All @@ -108,7 +113,7 @@ export const enhancedEsSearchStrategyProvider = (
};

try {
const promise = esClient.asCurrentUser.transport.request({
const promise = client.transport.request({
method,
path,
body,
Expand Down Expand Up @@ -169,7 +174,11 @@ export const enhancedEsSearchStrategyProvider = (
extend: async (id, keepAlive, options, { esClient }) => {
logger.debug(`extend ${id} by ${keepAlive}`);
try {
await esClient.asCurrentUser.asyncSearch.get({ id, body: { keep_alive: keepAlive } });
const client = useInternalUser ? esClient.asInternalUser : esClient.asCurrentUser;
await client.asyncSearch.get({
id,
body: { keep_alive: keepAlive },
});
} catch (e) {
throw getKbnServerError(e);
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface SearchStrategyDependencies {
esClient: IScopedClusterClient;
uiSettingsClient: IUiSettingsClient;
searchSessionsClient: IScopedSearchSessionsClient;
request: KibanaRequest;
}

export interface ISearchSetup {
Expand Down
14 changes: 8 additions & 6 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import { ISearchOptions as ISearchOptions_2 } from 'src/plugins/data/public';
import { ISearchSource } from 'src/plugins/data/public';
import { IUiSettingsClient } from 'src/core/server';
import { IUiSettingsClient as IUiSettingsClient_3 } from 'kibana/server';
import { KibanaRequest } from 'kibana/server';
import { KibanaRequest as KibanaRequest_2 } from 'src/core/server';
import { KibanaRequest } from 'src/core/server';
import { KibanaRequest as KibanaRequest_2 } from 'kibana/server';
import { Logger } from 'src/core/server';
import { Logger as Logger_2 } from 'kibana/server';
import { LoggerFactory } from '@kbn/logging';
Expand Down Expand Up @@ -1040,7 +1040,7 @@ export interface ISearchOptions {
// @public (undocumented)
export interface ISearchSessionService<T = unknown> {
// (undocumented)
asScopedProvider: (core: CoreStart) => (request: KibanaRequest) => IScopedSearchSessionsClient<T>;
asScopedProvider: (core: CoreStart) => (request: KibanaRequest_2) => IScopedSearchSessionsClient<T>;
}

// Warning: (ae-missing-release-tag) "ISearchSetup" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
Expand Down Expand Up @@ -1068,11 +1068,11 @@ export interface ISearchStart<SearchStrategyRequest extends IKibanaSearchRequest
// (undocumented)
aggs: AggsStart;
// (undocumented)
asScoped: (request: KibanaRequest_2) => IScopedSearchClient;
asScoped: (request: KibanaRequest) => IScopedSearchClient;
getSearchStrategy: (name?: string) => ISearchStrategy<SearchStrategyRequest, SearchStrategyResponse>;
// (undocumented)
searchSource: {
asScoped: (request: KibanaRequest_2) => Promise<ISearchStartSearchSource>;
asScoped: (request: KibanaRequest) => Promise<ISearchStartSearchSource>;
};
}

Expand Down Expand Up @@ -1391,6 +1391,8 @@ export interface SearchStrategyDependencies {
// (undocumented)
esClient: IScopedClusterClient;
// (undocumented)
request: KibanaRequest;
// (undocumented)
savedObjectsClient: SavedObjectsClientContract;
// (undocumented)
searchSessionsClient: IScopedSearchSessionsClient;
Expand Down Expand Up @@ -1551,7 +1553,7 @@ export function usageProvider(core: CoreSetup_2): SearchUsage;
// src/plugins/data/server/index.ts:269:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/index.ts:270:1 - (ae-forgotten-export) The symbol "calcAutoIntervalLessThan" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/plugin.ts:81:74 - (ae-forgotten-export) The symbol "DataEnhancements" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/search/types.ts:114:5 - (ae-forgotten-export) The symbol "ISearchStartSearchSource" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/search/types.ts:115:5 - (ae-forgotten-export) The symbol "ISearchStartSearchSource" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { of, throwError } from 'rxjs';
import {
elasticsearchServiceMock,
httpServerMock,
savedObjectsClientMock,
uiSettingsServiceMock,
} from 'src/core/server/mocks';
Expand Down Expand Up @@ -327,6 +328,7 @@ const createSearchStrategyDependenciesMock = (): SearchStrategyDependencies => (
esClient: elasticsearchServiceMock.createScopedClusterClient(),
savedObjectsClient: savedObjectsClientMock.create(),
searchSessionsClient: createSearchSessionsClientMock(),
request: httpServerMock.createKibanaRequest(),
});

// using the official data mock from within x-pack doesn't type-check successfully,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { of, throwError } from 'rxjs';
import {
elasticsearchServiceMock,
httpServerMock,
savedObjectsClientMock,
uiSettingsServiceMock,
} from 'src/core/server/mocks';
Expand Down Expand Up @@ -282,6 +283,7 @@ const createSearchStrategyDependenciesMock = (): SearchStrategyDependencies => (
esClient: elasticsearchServiceMock.createScopedClusterClient(),
savedObjectsClient: savedObjectsClientMock.create(),
searchSessionsClient: createSearchSessionsClientMock(),
request: httpServerMock.createKibanaRequest(),
});

// using the official data mock from within x-pack doesn't type-check successfully,
Expand Down

0 comments on commit d5794b5

Please sign in to comment.