From 2ef7f3bd0cee5f44d71e0528409bc3f8576be333 Mon Sep 17 00:00:00 2001 From: Liza Katz Date: Wed, 17 Mar 2021 17:16:01 +0200 Subject: [PATCH] use index patterns and search services for autocomplete (#92861) * use index patterns and search services for autocomplete * imports * extend DataRequestHandlerContext * Catch error if index patterns service can't be created. * doc Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- ...ata-server.indexpatternsserviceprovider.md | 2 +- ...rver.indexpatternsserviceprovider.setup.md | 4 +-- .../kibana-plugin-plugins-data-server.md | 1 - ...data-server.searchrequesthandlercontext.md | 11 ------ .../data/server/autocomplete/routes.ts | 3 +- .../autocomplete/value_suggestions_route.ts | 36 +++++++++++++------ src/plugins/data/server/index.ts | 4 +-- .../data/server/index_patterns/index.ts | 4 +++ .../index_patterns/index_patterns_service.ts | 20 ++++++++++- src/plugins/data/server/mocks.ts | 2 +- src/plugins/data/server/plugin.ts | 5 ++- .../data/server/search/routes/msearch.ts | 2 +- .../data/server/search/routes/search.ts | 2 +- .../data/server/search/search_service.ts | 2 +- src/plugins/data/server/search/types.ts | 11 ------ src/plugins/data/server/server.api.md | 15 ++++---- src/plugins/data/server/types.ts | 22 ++++++++++++ x-pack/plugins/infra/server/types.ts | 6 ++-- 18 files changed, 96 insertions(+), 56 deletions(-) delete mode 100644 docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchrequesthandlercontext.md create mode 100644 src/plugins/data/server/types.ts diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsserviceprovider.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsserviceprovider.md index d408f00e33c9e..698b4bc7f2043 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsserviceprovider.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsserviceprovider.md @@ -14,6 +14,6 @@ export declare class IndexPatternsServiceProvider implements PluginSignature: ```typescript -setup(core: CoreSetup, { expressions }: IndexPatternsServiceSetupDeps): void; +setup(core: CoreSetup, { logger, expressions }: IndexPatternsServiceSetupDeps): void; ``` ## Parameters @@ -15,7 +15,7 @@ setup(core: CoreSetup, { expressio | Parameter | Type | Description | | --- | --- | --- | | core | CoreSetup<DataPluginStartDependencies, DataPluginStart> | | -| { expressions } | IndexPatternsServiceSetupDeps | | +| { logger, expressions } | IndexPatternsServiceSetupDeps | | Returns: diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md index e0734bc017f4f..16d9ce457603e 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.md @@ -109,6 +109,5 @@ | [KibanaContext](./kibana-plugin-plugins-data-server.kibanacontext.md) | | | [ParsedInterval](./kibana-plugin-plugins-data-server.parsedinterval.md) | | | [Query](./kibana-plugin-plugins-data-server.query.md) | | -| [SearchRequestHandlerContext](./kibana-plugin-plugins-data-server.searchrequesthandlercontext.md) | | | [TimeRange](./kibana-plugin-plugins-data-server.timerange.md) | | diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchrequesthandlercontext.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchrequesthandlercontext.md deleted file mode 100644 index f031ddfbd09af..0000000000000 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchrequesthandlercontext.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [SearchRequestHandlerContext](./kibana-plugin-plugins-data-server.searchrequesthandlercontext.md) - -## SearchRequestHandlerContext type - -Signature: - -```typescript -export declare type SearchRequestHandlerContext = IScopedSearchClient; -``` diff --git a/src/plugins/data/server/autocomplete/routes.ts b/src/plugins/data/server/autocomplete/routes.ts index c453094ff6874..fc6bb0b69c102 100644 --- a/src/plugins/data/server/autocomplete/routes.ts +++ b/src/plugins/data/server/autocomplete/routes.ts @@ -9,9 +9,10 @@ import { Observable } from 'rxjs'; import { CoreSetup, SharedGlobalConfig } from 'kibana/server'; import { registerValueSuggestionsRoute } from './value_suggestions_route'; +import { DataRequestHandlerContext } from '../types'; export function registerRoutes({ http }: CoreSetup, config$: Observable): void { - const router = http.createRouter(); + const router = http.createRouter(); registerValueSuggestionsRoute(router, config$); } diff --git a/src/plugins/data/server/autocomplete/value_suggestions_route.ts b/src/plugins/data/server/autocomplete/value_suggestions_route.ts index 489a23eb83897..8e6d3afa18ed5 100644 --- a/src/plugins/data/server/autocomplete/value_suggestions_route.ts +++ b/src/plugins/data/server/autocomplete/value_suggestions_route.ts @@ -12,12 +12,12 @@ import { IRouter, SharedGlobalConfig } from 'kibana/server'; import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; -import { IFieldType, Filter } from '../index'; -import { findIndexPatternById, getFieldByName } from '../index_patterns'; +import { IFieldType, Filter, ES_SEARCH_STRATEGY, IEsSearchRequest } from '../index'; import { getRequestAbortedSignal } from '../lib'; +import { DataRequestHandlerContext } from '../types'; export function registerValueSuggestionsRoute( - router: IRouter, + router: IRouter, config$: Observable ) { router.post( @@ -44,24 +44,40 @@ export function registerValueSuggestionsRoute( const config = await config$.pipe(first()).toPromise(); const { field: fieldName, query, filters } = request.body; const { index } = request.params; - const { client } = context.core.elasticsearch.legacy; const signal = getRequestAbortedSignal(request.events.aborted$); + if (!context.indexPatterns) { + return response.badRequest(); + } + const autocompleteSearchOptions = { timeout: `${config.kibana.autocompleteTimeout.asMilliseconds()}ms`, terminate_after: config.kibana.autocompleteTerminateAfter.asMilliseconds(), }; - const indexPattern = await findIndexPatternById(context.core.savedObjects.client, index); - - const field = indexPattern && getFieldByName(fieldName, indexPattern); + const indexPatterns = await context.indexPatterns.find(index, 1); + if (!indexPatterns || indexPatterns.length === 0) { + return response.notFound(); + } + const field = indexPatterns[0].getFieldByName(fieldName); const body = await getBody(autocompleteSearchOptions, field || fieldName, query, filters); - const result = await client.callAsCurrentUser('search', { index, body }, { signal }); + const searchRequest: IEsSearchRequest = { + params: { + index, + body, + }, + }; + const { rawResponse } = await context.search + .search(searchRequest, { + strategy: ES_SEARCH_STRATEGY, + abortSignal: signal, + }) + .toPromise(); const buckets: any[] = - get(result, 'aggregations.suggestions.buckets') || - get(result, 'aggregations.nestedSuggestions.suggestions.buckets'); + get(rawResponse, 'aggregations.suggestions.buckets') || + get(rawResponse, 'aggregations.nestedSuggestions.suggestions.buckets'); return response.ok({ body: map(buckets || [], 'key') }); } diff --git a/src/plugins/data/server/index.ts b/src/plugins/data/server/index.ts index cbf09ef57d96a..c153c0efa8892 100644 --- a/src/plugins/data/server/index.ts +++ b/src/plugins/data/server/index.ts @@ -236,10 +236,10 @@ export { SearchUsage, SearchSessionService, ISearchSessionService, - SearchRequestHandlerContext, - DataRequestHandlerContext, } from './search'; +export { DataRequestHandlerContext } from './types'; + // Search namespace export const search = { aggs: { diff --git a/src/plugins/data/server/index_patterns/index.ts b/src/plugins/data/server/index_patterns/index.ts index 7226d6f015cf8..85610cd85a3ce 100644 --- a/src/plugins/data/server/index_patterns/index.ts +++ b/src/plugins/data/server/index_patterns/index.ts @@ -6,6 +6,8 @@ * Side Public License, v 1. */ +import { IndexPatternsService } from '../../common/index_patterns'; + export * from './utils'; export { IndexPatternsFetcher, @@ -15,3 +17,5 @@ export { getCapabilitiesForRollupIndices, } from './fetcher'; export { IndexPatternsServiceProvider, IndexPatternsServiceStart } from './index_patterns_service'; + +export type IndexPatternsHandlerContext = IndexPatternsService; diff --git a/src/plugins/data/server/index_patterns/index_patterns_service.ts b/src/plugins/data/server/index_patterns/index_patterns_service.ts index 5d703021b94da..b489c29bc3b70 100644 --- a/src/plugins/data/server/index_patterns/index_patterns_service.ts +++ b/src/plugins/data/server/index_patterns/index_patterns_service.ts @@ -25,6 +25,7 @@ import { getIndexPatternLoad } from './expressions'; import { UiSettingsServerToCommon } from './ui_settings_wrapper'; import { IndexPatternsApiServer } from './index_patterns_api_client'; import { SavedObjectsClientServerToCommon } from './saved_objects_client_wrapper'; +import { DataRequestHandlerContext } from '../types'; export interface IndexPatternsServiceStart { indexPatternsServiceFactory: ( @@ -35,6 +36,7 @@ export interface IndexPatternsServiceStart { export interface IndexPatternsServiceSetupDeps { expressions: ExpressionsServerSetup; + logger: Logger; } export interface IndexPatternsServiceStartDeps { @@ -45,11 +47,27 @@ export interface IndexPatternsServiceStartDeps { export class IndexPatternsServiceProvider implements Plugin { public setup( core: CoreSetup, - { expressions }: IndexPatternsServiceSetupDeps + { logger, expressions }: IndexPatternsServiceSetupDeps ) { core.savedObjects.registerType(indexPatternSavedObjectType); core.capabilities.registerProvider(capabilitiesProvider); + core.http.registerRouteHandlerContext( + 'indexPatterns', + async (context, request) => { + const [coreStart, , dataStart] = await core.getStartServices(); + try { + return await dataStart.indexPatterns.indexPatternsServiceFactory( + coreStart.savedObjects.getScopedClient(request), + coreStart.elasticsearch.client.asScoped(request).asCurrentUser + ); + } catch (e) { + logger.error(e); + return undefined; + } + } + ); + registerRoutes(core.http, core.getStartServices); expressions.registerFunction(getIndexPatternLoad({ getStartServices: core.getStartServices })); diff --git a/src/plugins/data/server/mocks.ts b/src/plugins/data/server/mocks.ts index 786dd30dbabd0..c82db7a141403 100644 --- a/src/plugins/data/server/mocks.ts +++ b/src/plugins/data/server/mocks.ts @@ -13,7 +13,7 @@ import { } from './search/mocks'; import { createFieldFormatsSetupMock, createFieldFormatsStartMock } from './field_formats/mocks'; import { createIndexPatternsStartMock } from './index_patterns/mocks'; -import { DataRequestHandlerContext } from './search'; +import { DataRequestHandlerContext } from './types'; function createSetupContract() { return { diff --git a/src/plugins/data/server/plugin.ts b/src/plugins/data/server/plugin.ts index a7a7663d6981c..3408c39cbb8e2 100644 --- a/src/plugins/data/server/plugin.ts +++ b/src/plugins/data/server/plugin.ts @@ -82,7 +82,10 @@ export class DataServerPlugin this.queryService.setup(core); this.autocompleteService.setup(core); this.kqlTelemetryService.setup(core, { usageCollection }); - this.indexPatterns.setup(core, { expressions }); + this.indexPatterns.setup(core, { + expressions, + logger: this.logger.get('indexPatterns'), + }); core.uiSettings.register(getUiSettings()); diff --git a/src/plugins/data/server/search/routes/msearch.ts b/src/plugins/data/server/search/routes/msearch.ts index b578805d8c2df..b5f06c4b343e7 100644 --- a/src/plugins/data/server/search/routes/msearch.ts +++ b/src/plugins/data/server/search/routes/msearch.ts @@ -12,7 +12,7 @@ import { SearchRouteDependencies } from '../search_service'; import { getCallMsearch } from './call_msearch'; import { reportServerError } from '../../../../kibana_utils/server'; -import type { DataPluginRouter } from '../types'; +import type { DataPluginRouter } from '../../types'; /** * The msearch route takes in an array of searches, each consisting of header * and body json, and reformts them into a single request for the _msearch API. diff --git a/src/plugins/data/server/search/routes/search.ts b/src/plugins/data/server/search/routes/search.ts index 1680a9c4a7237..6690e2b81f3e4 100644 --- a/src/plugins/data/server/search/routes/search.ts +++ b/src/plugins/data/server/search/routes/search.ts @@ -10,7 +10,7 @@ import { first } from 'rxjs/operators'; import { schema } from '@kbn/config-schema'; import { getRequestAbortedSignal } from '../../lib'; import { reportServerError } from '../../../../kibana_utils/server'; -import type { DataPluginRouter } from '../types'; +import type { DataPluginRouter } from '../../types'; export function registerSearchRoute(router: DataPluginRouter): void { router.post( diff --git a/src/plugins/data/server/search/search_service.ts b/src/plugins/data/server/search/search_service.ts index 6ece8ff945468..be826a943efd9 100644 --- a/src/plugins/data/server/search/search_service.ts +++ b/src/plugins/data/server/search/search_service.ts @@ -29,7 +29,6 @@ import type { ISearchStrategy, SearchEnhancements, SearchStrategyDependencies, - DataRequestHandlerContext, } from './types'; import { AggsService } from './aggs'; @@ -66,6 +65,7 @@ import { ConfigSchema } from '../../config'; import { ISearchSessionService, SearchSessionService } from './session'; import { KbnServerError } from '../../../kibana_utils/server'; import { registerBsearchRoute } from './routes/bsearch'; +import { DataRequestHandlerContext } from '../types'; type StrategyMap = Record>; diff --git a/src/plugins/data/server/search/types.ts b/src/plugins/data/server/search/types.ts index e8548257c0167..d7aadcc348c87 100644 --- a/src/plugins/data/server/search/types.ts +++ b/src/plugins/data/server/search/types.ts @@ -8,12 +8,10 @@ import { Observable } from 'rxjs'; import type { - IRouter, IScopedClusterClient, IUiSettingsClient, SavedObjectsClientContract, KibanaRequest, - RequestHandlerContext, } from 'src/core/server'; import { ISearchOptions, @@ -116,12 +114,3 @@ export interface ISearchStart< } export type SearchRequestHandlerContext = IScopedSearchClient; - -/** - * @internal - */ -export interface DataRequestHandlerContext extends RequestHandlerContext { - search: SearchRequestHandlerContext; -} - -export type DataPluginRouter = IRouter; diff --git a/src/plugins/data/server/server.api.md b/src/plugins/data/server/server.api.md index 0118906c181cc..83f7c67eba057 100644 --- a/src/plugins/data/server/server.api.md +++ b/src/plugins/data/server/server.api.md @@ -312,6 +312,12 @@ export const config: PluginConfigDescriptor; // @internal (undocumented) export interface DataRequestHandlerContext extends RequestHandlerContext { + // Warning: (ae-forgotten-export) The symbol "IndexPatternsHandlerContext" needs to be exported by the entry point index.d.ts + // + // (undocumented) + indexPatterns?: IndexPatternsHandlerContext; + // Warning: (ae-forgotten-export) The symbol "SearchRequestHandlerContext" needs to be exported by the entry point index.d.ts + // // (undocumented) search: SearchRequestHandlerContext; } @@ -954,7 +960,7 @@ export class IndexPatternsServiceProvider implements Plugin_3, { expressions }: IndexPatternsServiceSetupDeps): void; + setup(core: CoreSetup_2, { logger, expressions }: IndexPatternsServiceSetupDeps): void; // Warning: (ae-forgotten-export) The symbol "IndexPatternsServiceStartDeps" needs to be exported by the entry point index.d.ts // // (undocumented) @@ -1319,11 +1325,6 @@ export const search: { tabifyGetColumns: typeof tabifyGetColumns; }; -// Warning: (ae-missing-release-tag) "SearchRequestHandlerContext" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) -export type SearchRequestHandlerContext = IScopedSearchClient; - // @internal export class SearchSessionService implements ISearchSessionService { constructor(); @@ -1515,7 +1516,7 @@ export function usageProvider(core: CoreSetup_2): SearchUsage; // src/plugins/data/server/index.ts:270:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // src/plugins/data/server/index.ts:271:1 - (ae-forgotten-export) The symbol "calcAutoIntervalLessThan" needs to be exported by the entry point index.d.ts // src/plugins/data/server/plugin.ts:79: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:112:5 - (ae-forgotten-export) The symbol "ISearchStartSearchSource" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/src/plugins/data/server/types.ts b/src/plugins/data/server/types.ts new file mode 100644 index 0000000000000..ea0fa49058d37 --- /dev/null +++ b/src/plugins/data/server/types.ts @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import type { IRouter, RequestHandlerContext } from 'src/core/server'; + +import { SearchRequestHandlerContext } from './search'; +import { IndexPatternsHandlerContext } from './index_patterns'; + +/** + * @internal + */ +export interface DataRequestHandlerContext extends RequestHandlerContext { + search: SearchRequestHandlerContext; + indexPatterns?: IndexPatternsHandlerContext; +} + +export type DataPluginRouter = IRouter; diff --git a/x-pack/plugins/infra/server/types.ts b/x-pack/plugins/infra/server/types.ts index 5cae015861946..1c51a5549cb41 100644 --- a/x-pack/plugins/infra/server/types.ts +++ b/x-pack/plugins/infra/server/types.ts @@ -5,8 +5,7 @@ * 2.0. */ -import type { RequestHandlerContext } from 'src/core/server'; -import type { SearchRequestHandlerContext } from '../../../../src/plugins/data/server'; +import type { DataRequestHandlerContext } from '../../../../src/plugins/data/server'; import { MlPluginSetup } from '../../ml/server'; export type MlSystem = ReturnType; @@ -27,7 +26,6 @@ export type InfraRequestHandlerContext = InfraMlRequestHandlerContext & /** * @internal */ -export interface InfraPluginRequestHandlerContext extends RequestHandlerContext { +export interface InfraPluginRequestHandlerContext extends DataRequestHandlerContext { infra: InfraRequestHandlerContext; - search: SearchRequestHandlerContext; }