From 37e9dced109c518375d922262b6a88540ab2b3ea Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 20 Apr 2020 15:16:24 -0700 Subject: [PATCH] [7.x] [SEARCH] Cleanup fetch soon (#63320) (#63983) * move old code to legacy folder * Use search service directly from search source * Move get search params to fetch folder * Delete search strategy folder * Doc update * Minor cleanups * Moved es client to legacy folder * Clean up some unused code (isViable, old search function) * Updated tests * strings update * Fix jest test * re-arrange runSearch function * re-arrange runSearch function * fix jest tests Co-authored-by: Elastic Machine Co-authored-by: Liza Katz Co-authored-by: Elastic Machine --- .../kibana-plugin-plugins-data-public.md | 1 - ...s-data-public.searchstrategyprovider.id.md | 11 -- ...-public.searchstrategyprovider.isviable.md | 11 -- ...gins-data-public.searchstrategyprovider.md | 20 ---- ...ta-public.searchstrategyprovider.search.md | 11 -- .../lib/get_indices.test.ts | 2 +- src/plugins/data/public/index.ts | 1 - src/plugins/data/public/public.api.md | 45 +++----- .../aggs/test_helpers/mock_data_services.ts | 11 +- .../search/fetch/get_search_params.test.ts | 70 ++++++++++++ .../get_search_params.ts | 14 +-- src/plugins/data/public/search/fetch/index.ts | 13 ++- .../fetch/{errors.ts => request_error.ts} | 0 .../search_error.ts | 0 src/plugins/data/public/search/fetch/types.ts | 3 + src/plugins/data/public/search/index.ts | 12 +- .../{fetch => legacy}/call_client.test.ts | 11 +- .../search/{fetch => legacy}/call_client.ts | 7 +- .../default_search_strategy.test.ts | 24 +--- .../default_search_strategy.ts | 38 +------ .../{ => legacy}/es_client/get_es_client.ts | 0 .../search/{ => legacy}/es_client/index.ts | 2 +- .../search/{ => legacy}/es_client/types.ts | 3 +- .../{fetch => legacy}/fetch_soon.test.ts | 6 +- .../search/{fetch => legacy}/fetch_soon.ts | 4 +- .../get_msearch_params.test.ts} | 45 +------- .../search/legacy/get_msearch_params.ts | 29 +++++ .../{search_strategy => legacy}/index.ts | 7 +- .../{search_strategy => legacy}/types.ts | 13 +-- .../data/public/search/search_service.ts | 6 +- .../search_source/search_source.test.ts | 55 ++++++++- .../search/search_source/search_source.ts | 53 +++++++-- .../search_strategy/no_op_search_strategy.ts | 55 --------- src/plugins/data/public/search/types.ts | 11 +- .../public/search/rollup_search_strategy.ts | 104 ------------------ .../translations/translations/ja-JP.json | 2 - .../translations/translations/zh-CN.json | 2 - 37 files changed, 276 insertions(+), 426 deletions(-) delete mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.id.md delete mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md delete mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.md delete mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.search.md create mode 100644 src/plugins/data/public/search/fetch/get_search_params.test.ts rename src/plugins/data/public/search/{search_strategy => fetch}/get_search_params.ts (81%) rename src/plugins/data/public/search/fetch/{errors.ts => request_error.ts} (100%) rename src/plugins/data/public/search/{search_strategy => fetch}/search_error.ts (100%) rename src/plugins/data/public/search/{fetch => legacy}/call_client.test.ts (91%) rename src/plugins/data/public/search/{fetch => legacy}/call_client.ts (89%) rename src/plugins/data/public/search/{search_strategy => legacy}/default_search_strategy.test.ts (83%) rename src/plugins/data/public/search/{search_strategy => legacy}/default_search_strategy.ts (64%) rename src/plugins/data/public/search/{ => legacy}/es_client/get_es_client.ts (100%) rename src/plugins/data/public/search/{ => legacy}/es_client/index.ts (91%) rename src/plugins/data/public/search/{ => legacy}/es_client/types.ts (94%) rename src/plugins/data/public/search/{fetch => legacy}/fetch_soon.test.ts (96%) rename src/plugins/data/public/search/{fetch => legacy}/fetch_soon.ts (95%) rename src/plugins/data/public/search/{search_strategy/get_search_params.test.ts => legacy/get_msearch_params.test.ts} (60%) create mode 100644 src/plugins/data/public/search/legacy/get_msearch_params.ts rename src/plugins/data/public/search/{search_strategy => legacy}/index.ts (78%) rename src/plugins/data/public/search/{search_strategy => legacy}/types.ts (89%) delete mode 100644 src/plugins/data/public/search/search_strategy/no_op_search_strategy.ts delete mode 100644 x-pack/plugins/rollup/public/search/rollup_search_strategy.ts diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index fc0dab94a0f656..bf29c883e4eb96 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -88,7 +88,6 @@ | [SavedQuery](./kibana-plugin-plugins-data-public.savedquery.md) | | | [SavedQueryService](./kibana-plugin-plugins-data-public.savedqueryservice.md) | | | [SearchSourceFields](./kibana-plugin-plugins-data-public.searchsourcefields.md) | | -| [SearchStrategyProvider](./kibana-plugin-plugins-data-public.searchstrategyprovider.md) | | | [TabbedAggColumn](./kibana-plugin-plugins-data-public.tabbedaggcolumn.md) | \* | | [TabbedTable](./kibana-plugin-plugins-data-public.tabbedtable.md) | \* | | [TimeRange](./kibana-plugin-plugins-data-public.timerange.md) | | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.id.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.id.md deleted file mode 100644 index d60ffba6a05ca8..00000000000000 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.id.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SearchStrategyProvider](./kibana-plugin-plugins-data-public.searchstrategyprovider.md) > [id](./kibana-plugin-plugins-data-public.searchstrategyprovider.id.md) - -## SearchStrategyProvider.id property - -Signature: - -```typescript -id: string; -``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md deleted file mode 100644 index aa8ed49051ee9a..00000000000000 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SearchStrategyProvider](./kibana-plugin-plugins-data-public.searchstrategyprovider.md) > [isViable](./kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md) - -## SearchStrategyProvider.isViable property - -Signature: - -```typescript -isViable: (indexPattern: IndexPattern) => boolean; -``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.md deleted file mode 100644 index b271a921906a71..00000000000000 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.md +++ /dev/null @@ -1,20 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SearchStrategyProvider](./kibana-plugin-plugins-data-public.searchstrategyprovider.md) - -## SearchStrategyProvider interface - -Signature: - -```typescript -export interface SearchStrategyProvider -``` - -## Properties - -| Property | Type | Description | -| --- | --- | --- | -| [id](./kibana-plugin-plugins-data-public.searchstrategyprovider.id.md) | string | | -| [isViable](./kibana-plugin-plugins-data-public.searchstrategyprovider.isviable.md) | (indexPattern: IndexPattern) => boolean | | -| [search](./kibana-plugin-plugins-data-public.searchstrategyprovider.search.md) | (params: SearchStrategySearchParams) => SearchStrategyResponse | | - diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.search.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.search.md deleted file mode 100644 index 6e2561c3b0ad00..00000000000000 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchstrategyprovider.search.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SearchStrategyProvider](./kibana-plugin-plugins-data-public.searchstrategyprovider.md) > [search](./kibana-plugin-plugins-data-public.searchstrategyprovider.search.md) - -## SearchStrategyProvider.search property - -Signature: - -```typescript -search: (params: SearchStrategySearchParams) => SearchStrategyResponse; -``` diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts index 40583af7177fee..b1500f8303b666 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/lib/get_indices.test.ts @@ -20,7 +20,7 @@ import { getIndices } from './get_indices'; import { IndexPatternCreationConfig } from '../../../../../../../../../plugins/index_pattern_management/public'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { LegacyApiCaller } from '../../../../../../../../../plugins/data/public/search'; +import { LegacyApiCaller } from '../../../../../../../../../plugins/data/public/search/legacy'; export const successfulResponse = { hits: { diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 06a46065baa847..b62b728beca35f 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -363,7 +363,6 @@ export { SearchRequest, SearchResponse, SearchError, - SearchStrategyProvider, ISearchSource, SearchSource, createSearchSource, diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 7d902c88616c51..78659720fe09a7 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1736,21 +1736,6 @@ export interface SearchSourceFields { version?: boolean; } -// Warning: (ae-missing-release-tag) "SearchStrategyProvider" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) -export interface SearchStrategyProvider { - // (undocumented) - id: string; - // (undocumented) - isViable: (indexPattern: IndexPattern) => boolean; - // Warning: (ae-forgotten-export) The symbol "SearchStrategySearchParams" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "SearchStrategyResponse" needs to be exported by the entry point index.d.ts - // - // (undocumented) - search: (params: SearchStrategySearchParams) => SearchStrategyResponse; -} - // Warning: (ae-missing-release-tag) "SortDirection" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) @@ -1901,21 +1886,21 @@ export type TSearchStrategyProvider = (context: ISearc // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "getRoutes" needs to be exported by the entry point index.d.ts // src/plugins/data/public/index.ts:234:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:383:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:391:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:400:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts -// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:382:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:387:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:388:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:399:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:400:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:404:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:408:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts +// src/plugins/data/public/index.ts:412:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:33:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts // src/plugins/data/public/query/state_sync/connect_to_query_state.ts:37:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts // src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromEvent" needs to be exported by the entry point index.d.ts diff --git a/src/plugins/data/public/search/aggs/test_helpers/mock_data_services.ts b/src/plugins/data/public/search/aggs/test_helpers/mock_data_services.ts index d1d591771743c7..e807e084c683a2 100644 --- a/src/plugins/data/public/search/aggs/test_helpers/mock_data_services.ts +++ b/src/plugins/data/public/search/aggs/test_helpers/mock_data_services.ts @@ -18,8 +18,8 @@ */ // eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { coreMock } from '../../../../../../../src/core/public/mocks'; -import { dataPluginMock } from '../../../../public/mocks'; +import { coreMock } from '../../../../../../core/public/mocks'; +import { dataPluginMock } from '../../../mocks'; import { setFieldFormats, setIndexPatterns, @@ -29,7 +29,7 @@ import { setQueryService, setSearchService, setUiSettings, -} from '../../../../public/services'; +} from '../../../services'; /** * Testing helper which calls all of the service setters used in the @@ -49,4 +49,9 @@ export function mockDataServices() { setQueryService(data.query); setSearchService(data.search); setUiSettings(core.uiSettings); + + return { + core, + data, + }; } diff --git a/src/plugins/data/public/search/fetch/get_search_params.test.ts b/src/plugins/data/public/search/fetch/get_search_params.test.ts new file mode 100644 index 00000000000000..edf18405e8ff71 --- /dev/null +++ b/src/plugins/data/public/search/fetch/get_search_params.test.ts @@ -0,0 +1,70 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getSearchParams } from './get_search_params'; +import { IUiSettingsClient } from 'kibana/public'; + +function getConfigStub(config: any = {}) { + return { + get: key => config[key], + } as IUiSettingsClient; +} + +describe('getSearchParams', () => { + test('includes rest_total_hits_as_int', () => { + const config = getConfigStub(); + const searchParams = getSearchParams(config); + expect(searchParams.rest_total_hits_as_int).toBe(true); + }); + + test('includes ignore_unavailable', () => { + const config = getConfigStub(); + const searchParams = getSearchParams(config); + expect(searchParams.ignore_unavailable).toBe(true); + }); + + test('includes ignore_throttled according to search:includeFrozen', () => { + let config = getConfigStub({ 'search:includeFrozen': true }); + let searchParams = getSearchParams(config); + expect(searchParams.ignore_throttled).toBe(false); + + config = getConfigStub({ 'search:includeFrozen': false }); + searchParams = getSearchParams(config); + expect(searchParams.ignore_throttled).toBe(true); + }); + + test('includes max_concurrent_shard_requests according to courier:maxConcurrentShardRequests', () => { + let config = getConfigStub({ 'courier:maxConcurrentShardRequests': 0 }); + let searchParams = getSearchParams(config); + expect(searchParams.max_concurrent_shard_requests).toBe(undefined); + + config = getConfigStub({ 'courier:maxConcurrentShardRequests': 5 }); + searchParams = getSearchParams(config); + expect(searchParams.max_concurrent_shard_requests).toBe(5); + }); + + test('includes timeout according to esShardTimeout if greater than 0', () => { + const config = getConfigStub(); + let searchParams = getSearchParams(config, 0); + expect(searchParams.timeout).toBe(undefined); + + searchParams = getSearchParams(config, 100); + expect(searchParams.timeout).toBe('100ms'); + }); +}); diff --git a/src/plugins/data/public/search/search_strategy/get_search_params.ts b/src/plugins/data/public/search/fetch/get_search_params.ts similarity index 81% rename from src/plugins/data/public/search/search_strategy/get_search_params.ts rename to src/plugins/data/public/search/fetch/get_search_params.ts index 9fb8f2c728c6f0..f0c43bd2e74cd1 100644 --- a/src/plugins/data/public/search/search_strategy/get_search_params.ts +++ b/src/plugins/data/public/search/fetch/get_search_params.ts @@ -17,18 +17,10 @@ * under the License. */ -import { IUiSettingsClient } from '../../../../../core/public'; +import { IUiSettingsClient } from 'kibana/public'; const sessionId = Date.now(); -export function getMSearchParams(config: IUiSettingsClient) { - return { - rest_total_hits_as_int: true, - ignore_throttled: getIgnoreThrottled(config), - max_concurrent_shard_requests: getMaxConcurrentShardRequests(config), - }; -} - export function getSearchParams(config: IUiSettingsClient, esShardTimeout: number = 0) { return { rest_total_hits_as_int: true, @@ -40,11 +32,11 @@ export function getSearchParams(config: IUiSettingsClient, esShardTimeout: numbe }; } -function getIgnoreThrottled(config: IUiSettingsClient) { +export function getIgnoreThrottled(config: IUiSettingsClient) { return !config.get('search:includeFrozen'); } -function getMaxConcurrentShardRequests(config: IUiSettingsClient) { +export function getMaxConcurrentShardRequests(config: IUiSettingsClient) { const maxConcurrentShardRequests = config.get('courier:maxConcurrentShardRequests'); return maxConcurrentShardRequests > 0 ? maxConcurrentShardRequests : undefined; } diff --git a/src/plugins/data/public/search/fetch/index.ts b/src/plugins/data/public/search/fetch/index.ts index 8a80b716add321..39845ec31bfaaa 100644 --- a/src/plugins/data/public/search/fetch/index.ts +++ b/src/plugins/data/public/search/fetch/index.ts @@ -18,5 +18,14 @@ */ export * from './types'; -export { fetchSoon } from './fetch_soon'; -export { RequestFailure } from './errors'; +export { + getSearchParams, + getPreference, + getTimeout, + getIgnoreThrottled, + getMaxConcurrentShardRequests, +} from './get_search_params'; + +export { SearchError, getSearchErrorType } from './search_error'; +export { RequestFailure } from './request_error'; +export { handleResponse } from './handle_response'; diff --git a/src/plugins/data/public/search/fetch/errors.ts b/src/plugins/data/public/search/fetch/request_error.ts similarity index 100% rename from src/plugins/data/public/search/fetch/errors.ts rename to src/plugins/data/public/search/fetch/request_error.ts diff --git a/src/plugins/data/public/search/search_strategy/search_error.ts b/src/plugins/data/public/search/fetch/search_error.ts similarity index 100% rename from src/plugins/data/public/search/search_strategy/search_error.ts rename to src/plugins/data/public/search/fetch/search_error.ts diff --git a/src/plugins/data/public/search/fetch/types.ts b/src/plugins/data/public/search/fetch/types.ts index e8de0576b8a72f..475b2abbc019f4 100644 --- a/src/plugins/data/public/search/fetch/types.ts +++ b/src/plugins/data/public/search/fetch/types.ts @@ -20,6 +20,9 @@ import { IUiSettingsClient } from '../../../../../core/public'; import { ISearchStart } from '../types'; +export type SearchRequest = any; +export type SearchResponse = any; + export interface FetchOptions { abortSignal?: AbortSignal; searchStrategyId?: string; diff --git a/src/plugins/data/public/search/index.ts b/src/plugins/data/public/search/index.ts index cce973d632f417..75c0eb8b6f0229 100644 --- a/src/plugins/data/public/search/index.ts +++ b/src/plugins/data/public/search/index.ts @@ -44,9 +44,13 @@ export { esSearchStrategyProvider, getEsPreference } from './es_search'; export { IKibanaSearchResponse, IKibanaSearchRequest } from '../../common/search'; -export { LegacyApiCaller, SearchRequest, SearchResponse } from './es_client'; - -export { SearchError, SearchStrategyProvider, getSearchErrorType } from './search_strategy'; +export { + SearchError, + FetchOptions, + SearchRequest, + SearchResponse, + getSearchErrorType, +} from './fetch'; export { ISearchSource, @@ -59,5 +63,3 @@ export { export { SearchInterceptor } from './search_interceptor'; export { RequestTimeoutError } from './request_timeout_error'; - -export { FetchOptions } from './fetch'; diff --git a/src/plugins/data/public/search/fetch/call_client.test.ts b/src/plugins/data/public/search/legacy/call_client.test.ts similarity index 91% rename from src/plugins/data/public/search/fetch/call_client.test.ts rename to src/plugins/data/public/search/legacy/call_client.test.ts index 7a99b7c064515b..f919187d46784d 100644 --- a/src/plugins/data/public/search/fetch/call_client.test.ts +++ b/src/plugins/data/public/search/legacy/call_client.test.ts @@ -18,16 +18,17 @@ */ import { callClient } from './call_client'; -import { handleResponse } from './handle_response'; -import { FetchHandlers } from './types'; -import { SearchStrategySearchParams, defaultSearchStrategy } from '../search_strategy'; +import { SearchStrategySearchParams } from './types'; +import { defaultSearchStrategy } from './default_search_strategy'; +import { FetchHandlers } from '../fetch'; +import { handleResponse } from '../fetch/handle_response'; const mockAbortFn = jest.fn(); -jest.mock('./handle_response', () => ({ +jest.mock('../fetch/handle_response', () => ({ handleResponse: jest.fn((request, response) => response), })); -jest.mock('../search_strategy', () => { +jest.mock('./default_search_strategy', () => { return { defaultSearchStrategy: { search: jest.fn(({ searchRequests }: SearchStrategySearchParams) => { diff --git a/src/plugins/data/public/search/fetch/call_client.ts b/src/plugins/data/public/search/legacy/call_client.ts similarity index 89% rename from src/plugins/data/public/search/fetch/call_client.ts rename to src/plugins/data/public/search/legacy/call_client.ts index b3c4c682fa60c0..c484c46aa4879f 100644 --- a/src/plugins/data/public/search/fetch/call_client.ts +++ b/src/plugins/data/public/search/legacy/call_client.ts @@ -17,10 +17,9 @@ * under the License. */ -import { handleResponse } from './handle_response'; -import { FetchOptions, FetchHandlers } from './types'; -import { defaultSearchStrategy } from '../search_strategy'; -import { SearchRequest } from '..'; +import { FetchOptions, FetchHandlers, handleResponse } from '../fetch'; +import { defaultSearchStrategy } from './default_search_strategy'; +import { SearchRequest } from '../index'; export function callClient( searchRequests: SearchRequest[], diff --git a/src/plugins/data/public/search/search_strategy/default_search_strategy.test.ts b/src/plugins/data/public/search/legacy/default_search_strategy.test.ts similarity index 83% rename from src/plugins/data/public/search/search_strategy/default_search_strategy.test.ts rename to src/plugins/data/public/search/legacy/default_search_strategy.test.ts index 210a0e5fd1ac78..835b02b3cd5c74 100644 --- a/src/plugins/data/public/search/search_strategy/default_search_strategy.test.ts +++ b/src/plugins/data/public/search/legacy/default_search_strategy.test.ts @@ -17,10 +17,10 @@ * under the License. */ -import { IUiSettingsClient } from '../../../../../core/public'; -import { SearchStrategySearchParams } from './types'; +import { IUiSettingsClient } from 'kibana/public'; import { defaultSearchStrategy } from './default_search_strategy'; import { searchStartMock } from '../mocks'; +import { SearchStrategySearchParams } from './types'; const { search } = defaultSearchStrategy; @@ -38,12 +38,6 @@ const searchMockResponse: any = Promise.resolve([]); searchMockResponse.abort = jest.fn(); const searchMock = jest.fn().mockReturnValue(searchMockResponse); -const newSearchMockResponse: any = Promise.resolve([]); -newSearchMockResponse.abort = jest.fn(); -const newSearchMock = jest.fn().mockReturnValue({ - toPromise: () => searchMockResponse, -}); - describe('defaultSearchStrategy', function() { describe('search', function() { let searchArgs: MockedKeys>; @@ -58,7 +52,6 @@ describe('defaultSearchStrategy', function() { const searchService = searchStartMock; searchService.aggs.calculateAutoTimeExpression = jest.fn().mockReturnValue('1d'); - searchService.search = newSearchMock; searchService.__LEGACY.esClient.search = searchMock; searchService.__LEGACY.esClient.msearch = msearchMock; @@ -112,18 +105,5 @@ describe('defaultSearchStrategy', function() { search({ ...searchArgs, config }).abort(); expect(msearchMockResponse.abort).toHaveBeenCalled(); }); - - test('should call new search service', () => { - const config = getConfigStub(); - search({ ...searchArgs, config }); - expect(newSearchMock).toHaveBeenCalledTimes(1); - }); - - test('should properly abort with new search service', async () => { - const abortSpy = jest.spyOn(AbortController.prototype, 'abort'); - const config = getConfigStub({}); - search({ ...searchArgs, config }).abort(); - expect(abortSpy).toHaveBeenCalled(); - }); }); }); diff --git a/src/plugins/data/public/search/search_strategy/default_search_strategy.ts b/src/plugins/data/public/search/legacy/default_search_strategy.ts similarity index 64% rename from src/plugins/data/public/search/search_strategy/default_search_strategy.ts rename to src/plugins/data/public/search/legacy/default_search_strategy.ts index 2bd88f51587a8a..1552410f9090ca 100644 --- a/src/plugins/data/public/search/search_strategy/default_search_strategy.ts +++ b/src/plugins/data/public/search/legacy/default_search_strategy.ts @@ -17,23 +17,19 @@ * under the License. */ +import { getPreference, getTimeout } from '../fetch'; +import { getMSearchParams } from './get_msearch_params'; import { SearchStrategyProvider, SearchStrategySearchParams } from './types'; -import { isDefault } from '../../index_patterns'; -import { getSearchParams, getMSearchParams, getPreference, getTimeout } from './get_search_params'; +// @deprecated export const defaultSearchStrategy: SearchStrategyProvider = { id: 'default', search: params => { - return params.config.get('courier:batchSearches') ? msearch(params) : search(params); - }, - - isViable: indexPattern => { - return indexPattern && isDefault(indexPattern); + return msearch(params); }, }; -// @deprecated function msearch({ searchRequests, searchService, @@ -65,29 +61,3 @@ function msearch({ abort: searching.abort, }; } - -function search({ - searchRequests, - searchService, - config, - esShardTimeout, -}: SearchStrategySearchParams) { - const abortController = new AbortController(); - const searchParams = getSearchParams(config, esShardTimeout); - const promises = searchRequests.map(({ index, indexType, body }) => { - const params = { - index: index.title || index, - body, - ...searchParams, - }; - const { signal } = abortController; - return searchService - .search({ params, indexType }, { signal }) - .toPromise() - .then(({ rawResponse }) => rawResponse); - }); - return { - searching: Promise.all(promises), - abort: () => abortController.abort(), - }; -} diff --git a/src/plugins/data/public/search/es_client/get_es_client.ts b/src/plugins/data/public/search/legacy/es_client/get_es_client.ts similarity index 100% rename from src/plugins/data/public/search/es_client/get_es_client.ts rename to src/plugins/data/public/search/legacy/es_client/get_es_client.ts diff --git a/src/plugins/data/public/search/es_client/index.ts b/src/plugins/data/public/search/legacy/es_client/index.ts similarity index 91% rename from src/plugins/data/public/search/es_client/index.ts rename to src/plugins/data/public/search/legacy/es_client/index.ts index b1e0ce31168242..78ac83af642d83 100644 --- a/src/plugins/data/public/search/es_client/index.ts +++ b/src/plugins/data/public/search/legacy/es_client/index.ts @@ -18,4 +18,4 @@ */ export { getEsClient } from './get_es_client'; -export { SearchRequest, SearchResponse, LegacyApiCaller } from './types'; +export { LegacyApiCaller } from './types'; diff --git a/src/plugins/data/public/search/es_client/types.ts b/src/plugins/data/public/search/legacy/es_client/types.ts similarity index 94% rename from src/plugins/data/public/search/es_client/types.ts rename to src/plugins/data/public/search/legacy/es_client/types.ts index 3ca0513a14238d..7a56b9b0cb00a8 100644 --- a/src/plugins/data/public/search/es_client/types.ts +++ b/src/plugins/data/public/search/legacy/es_client/types.ts @@ -17,8 +17,7 @@ * under the License. */ -export type SearchRequest = any; -export type SearchResponse = any; +import { SearchRequest, SearchResponse } from '../../fetch'; export interface LegacyApiCaller { search: (searchRequest: SearchRequest) => LegacyApiCallerResponse; diff --git a/src/plugins/data/public/search/fetch/fetch_soon.test.ts b/src/plugins/data/public/search/legacy/fetch_soon.test.ts similarity index 96% rename from src/plugins/data/public/search/fetch/fetch_soon.test.ts rename to src/plugins/data/public/search/legacy/fetch_soon.test.ts index a8d593c8501f68..b2e17798ccc9fa 100644 --- a/src/plugins/data/public/search/fetch/fetch_soon.test.ts +++ b/src/plugins/data/public/search/legacy/fetch_soon.test.ts @@ -19,9 +19,9 @@ import { fetchSoon } from './fetch_soon'; import { callClient } from './call_client'; -import { IUiSettingsClient } from '../../../../../core/public'; -import { FetchHandlers, FetchOptions } from './types'; -import { SearchRequest, SearchResponse } from '..'; +import { IUiSettingsClient } from 'kibana/public'; +import { FetchHandlers, FetchOptions } from '../fetch/types'; +import { SearchRequest, SearchResponse } from '../index'; function getConfigStub(config: any = {}) { return { diff --git a/src/plugins/data/public/search/fetch/fetch_soon.ts b/src/plugins/data/public/search/legacy/fetch_soon.ts similarity index 95% rename from src/plugins/data/public/search/fetch/fetch_soon.ts rename to src/plugins/data/public/search/legacy/fetch_soon.ts index b1405747426ee9..18fa410a5bef03 100644 --- a/src/plugins/data/public/search/fetch/fetch_soon.ts +++ b/src/plugins/data/public/search/legacy/fetch_soon.ts @@ -18,8 +18,8 @@ */ import { callClient } from './call_client'; -import { FetchHandlers, FetchOptions } from './types'; -import { SearchRequest, SearchResponse } from '..'; +import { FetchHandlers, FetchOptions } from '../fetch/types'; +import { SearchRequest, SearchResponse } from '../index'; /** * This function introduces a slight delay in the request process to allow multiple requests to queue diff --git a/src/plugins/data/public/search/search_strategy/get_search_params.test.ts b/src/plugins/data/public/search/legacy/get_msearch_params.test.ts similarity index 60% rename from src/plugins/data/public/search/search_strategy/get_search_params.test.ts rename to src/plugins/data/public/search/legacy/get_msearch_params.test.ts index 76f3105d7f942e..9f16d5b408178d 100644 --- a/src/plugins/data/public/search/search_strategy/get_search_params.test.ts +++ b/src/plugins/data/public/search/legacy/get_msearch_params.test.ts @@ -17,7 +17,7 @@ * under the License. */ -import { getMSearchParams, getSearchParams } from './get_search_params'; +import { getMSearchParams } from './get_msearch_params'; import { IUiSettingsClient } from '../../../../../core/public'; function getConfigStub(config: any = {}) { @@ -64,46 +64,3 @@ describe('getMSearchParams', () => { expect(msearchParams.hasOwnProperty('timeout')).toBe(false); }); }); - -describe('getSearchParams', () => { - test('includes rest_total_hits_as_int', () => { - const config = getConfigStub(); - const searchParams = getSearchParams(config); - expect(searchParams.rest_total_hits_as_int).toBe(true); - }); - - test('includes ignore_unavailable', () => { - const config = getConfigStub(); - const searchParams = getSearchParams(config); - expect(searchParams.ignore_unavailable).toBe(true); - }); - - test('includes ignore_throttled according to search:includeFrozen', () => { - let config = getConfigStub({ 'search:includeFrozen': true }); - let searchParams = getSearchParams(config); - expect(searchParams.ignore_throttled).toBe(false); - - config = getConfigStub({ 'search:includeFrozen': false }); - searchParams = getSearchParams(config); - expect(searchParams.ignore_throttled).toBe(true); - }); - - test('includes max_concurrent_shard_requests according to courier:maxConcurrentShardRequests', () => { - let config = getConfigStub({ 'courier:maxConcurrentShardRequests': 0 }); - let searchParams = getSearchParams(config); - expect(searchParams.max_concurrent_shard_requests).toBe(undefined); - - config = getConfigStub({ 'courier:maxConcurrentShardRequests': 5 }); - searchParams = getSearchParams(config); - expect(searchParams.max_concurrent_shard_requests).toBe(5); - }); - - test('includes timeout according to esShardTimeout if greater than 0', () => { - const config = getConfigStub(); - let searchParams = getSearchParams(config, 0); - expect(searchParams.timeout).toBe(undefined); - - searchParams = getSearchParams(config, 100); - expect(searchParams.timeout).toBe('100ms'); - }); -}); diff --git a/src/plugins/data/public/search/legacy/get_msearch_params.ts b/src/plugins/data/public/search/legacy/get_msearch_params.ts new file mode 100644 index 00000000000000..48d13903c972fe --- /dev/null +++ b/src/plugins/data/public/search/legacy/get_msearch_params.ts @@ -0,0 +1,29 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { IUiSettingsClient } from 'kibana/public'; +import { getIgnoreThrottled, getMaxConcurrentShardRequests } from '../fetch'; + +export function getMSearchParams(config: IUiSettingsClient) { + return { + rest_total_hits_as_int: true, + ignore_throttled: getIgnoreThrottled(config), + max_concurrent_shard_requests: getMaxConcurrentShardRequests(config), + }; +} diff --git a/src/plugins/data/public/search/search_strategy/index.ts b/src/plugins/data/public/search/legacy/index.ts similarity index 78% rename from src/plugins/data/public/search/search_strategy/index.ts rename to src/plugins/data/public/search/legacy/index.ts index e3de2ea46e3eca..e2ae72824f3f49 100644 --- a/src/plugins/data/public/search/search_strategy/index.ts +++ b/src/plugins/data/public/search/legacy/index.ts @@ -17,8 +17,5 @@ * under the License. */ -export { SearchError, getSearchErrorType } from './search_error'; - -export { SearchStrategyProvider, SearchStrategySearchParams } from './types'; - -export { defaultSearchStrategy } from './default_search_strategy'; +export { fetchSoon } from './fetch_soon'; +export { getEsClient, LegacyApiCaller } from './es_client'; diff --git a/src/plugins/data/public/search/search_strategy/types.ts b/src/plugins/data/public/search/legacy/types.ts similarity index 89% rename from src/plugins/data/public/search/search_strategy/types.ts rename to src/plugins/data/public/search/legacy/types.ts index 764370d8ff6497..3812cec7a2aa20 100644 --- a/src/plugins/data/public/search/search_strategy/types.ts +++ b/src/plugins/data/public/search/legacy/types.ts @@ -17,21 +17,20 @@ * under the License. */ -import { IndexPattern } from '../..'; -import { FetchHandlers } from '../fetch/types'; +import { FetchHandlers } from '../fetch'; import { SearchRequest, SearchResponse } from '..'; +export interface SearchStrategySearchParams extends FetchHandlers { + searchRequests: SearchRequest[]; +} + +// @deprecated export interface SearchStrategyProvider { id: string; search: (params: SearchStrategySearchParams) => SearchStrategyResponse; - isViable: (indexPattern: IndexPattern) => boolean; } export interface SearchStrategyResponse { searching: Promise; abort: () => void; } - -export interface SearchStrategySearchParams extends FetchHandlers { - searchRequests: SearchRequest[]; -} diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index a539736991adb4..916278a96659b4 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -22,12 +22,12 @@ import { Plugin, CoreSetup, CoreStart, PackageInfo } from '../../../../core/publ import { SYNC_SEARCH_STRATEGY, syncSearchStrategyProvider } from './sync_search_strategy'; import { ISearchSetup, ISearchStart, TSearchStrategyProvider, TSearchStrategiesMap } from './types'; import { TStrategyTypes } from './strategy_types'; -import { getEsClient, LegacyApiCaller } from './es_client'; +import { getEsClient, LegacyApiCaller } from './legacy'; import { ES_SEARCH_STRATEGY, DEFAULT_SEARCH_STRATEGY } from '../../common/search'; -import { esSearchStrategyProvider } from './es_search/es_search_strategy'; +import { esSearchStrategyProvider } from './es_search'; import { IndexPatternsContract } from '../index_patterns/index_patterns'; import { createSearchSource } from './search_source'; -import { QuerySetup } from '../query/query_service'; +import { QuerySetup } from '../query'; import { GetInternalStartServicesFn } from '../types'; import { SearchInterceptor } from './search_interceptor'; import { diff --git a/src/plugins/data/public/search/search_source/search_source.test.ts b/src/plugins/data/public/search/search_source/search_source.test.ts index 6bad093d31402b..6e878844664ad2 100644 --- a/src/plugins/data/public/search/search_source/search_source.test.ts +++ b/src/plugins/data/public/search/search_source/search_source.test.ts @@ -20,8 +20,26 @@ import { SearchSource } from './search_source'; import { IndexPattern, SortDirection } from '../..'; import { mockDataServices } from '../aggs/test_helpers'; +import { setSearchService } from '../../services'; +import { searchStartMock } from '../mocks'; +import { fetchSoon } from '../legacy'; +import { CoreStart } from 'kibana/public'; +import { Observable } from 'rxjs'; -jest.mock('../fetch', () => ({ +// Setup search service mock +searchStartMock.search = jest.fn(() => { + return new Observable(subscriber => { + setTimeout(() => { + subscriber.next({ + rawResponse: '', + }); + subscriber.complete(); + }, 100); + }); +}) as any; +setSearchService(searchStartMock); + +jest.mock('../legacy', () => ({ fetchSoon: jest.fn().mockResolvedValue({}), })); @@ -44,8 +62,11 @@ const indexPattern2 = ({ } as unknown) as IndexPattern; describe('SearchSource', function() { + let uiSettingsMock: jest.Mocked; beforeEach(() => { - mockDataServices(); + const { core } = mockDataServices(); + uiSettingsMock = core.uiSettings; + jest.clearAllMocks(); }); describe('#setField()', function() { @@ -151,6 +172,36 @@ describe('SearchSource', function() { }); }); + describe('#legacy fetch()', () => { + beforeEach(() => { + uiSettingsMock.get.mockImplementation(() => { + return true; // batchSearches = true + }); + }); + + afterEach(() => { + uiSettingsMock.get.mockImplementation(() => { + return false; // batchSearches = false + }); + }); + + it('should call msearch', async () => { + const searchSource = new SearchSource({ index: indexPattern }); + const options = {}; + await searchSource.fetch(options); + expect(fetchSoon).toBeCalledTimes(1); + }); + }); + + describe('#search service fetch()', () => { + it('should call msearch', async () => { + const searchSource = new SearchSource({ index: indexPattern }); + const options = {}; + await searchSource.fetch(options); + expect(searchStartMock.search).toBeCalledTimes(1); + }); + }); + describe('#serialize', function() { it('should reference index patterns', () => { const indexPattern123 = { id: '123' } as IndexPattern; diff --git a/src/plugins/data/public/search/search_source/search_source.ts b/src/plugins/data/public/search/search_source/search_source.ts index c70db7bb82ef7f..9d2bb889953cf2 100644 --- a/src/plugins/data/public/search/search_source/search_source.ts +++ b/src/plugins/data/public/search/search_source/search_source.ts @@ -70,17 +70,19 @@ */ import _ from 'lodash'; +import { map } from 'rxjs/operators'; import { SavedObjectReference } from 'kibana/public'; import { normalizeSortRequest } from './normalize_sort_request'; import { filterDocvalueFields } from './filter_docvalue_fields'; import { fieldWildcardFilter } from '../../../../kibana_utils/public'; import { IIndexPattern, SearchRequest } from '../..'; import { SearchSourceOptions, SearchSourceFields } from './types'; -import { fetchSoon, FetchOptions, RequestFailure } from '../fetch'; +import { FetchOptions, RequestFailure, getSearchParams, handleResponse } from '../fetch'; import { getSearchService, getUiSettings, getInjectedMetadata } from '../../services'; import { getEsQueryConfig, buildEsQuery, Filter } from '../../../common'; import { getHighlightRequest } from '../../../common/field_formats'; +import { fetchSoon } from '../legacy'; export type ISearchSource = Pick; @@ -185,18 +187,29 @@ export class SearchSource { } /** - * Fetch this source and reject the returned Promise on error - * - * @async + * Run a search using the search service + * @return {Observable>} */ - async fetch(options: FetchOptions = {}) { - await this.requestIsStarting(options); - - const searchRequest = await this.flatten(); - this.history = [searchRequest]; + private fetch$(searchRequest: SearchRequest, signal?: AbortSignal) { + const esShardTimeout = getInjectedMetadata().getInjectedVar('esShardTimeout') as number; + const searchParams = getSearchParams(getUiSettings(), esShardTimeout); + const params = { + index: searchRequest.index.title || searchRequest.index, + body: searchRequest.body, + ...searchParams, + }; + return getSearchService() + .search({ params, indexType: searchRequest.indexType }, { signal }) + .pipe(map(({ rawResponse }) => handleResponse(searchRequest, rawResponse))); + } + /** + * Run a search using the search service + * @return {Promise>} + */ + private async legacyFetch(searchRequest: SearchRequest, options: FetchOptions) { const esShardTimeout = getInjectedMetadata().getInjectedVar('esShardTimeout') as number; - const response = await fetchSoon( + return await fetchSoon( searchRequest, { ...(this.searchStrategyId && { searchStrategyId: this.searchStrategyId }), @@ -208,6 +221,24 @@ export class SearchSource { esShardTimeout, } ); + } + /** + * Fetch this source and reject the returned Promise on error + * + * @async + */ + async fetch(options: FetchOptions = {}) { + await this.requestIsStarting(options); + + const searchRequest = await this.flatten(); + this.history = [searchRequest]; + + let response; + if (getUiSettings().get('courier:batchSearches')) { + response = await this.legacyFetch(searchRequest, options); + } else { + response = this.fetch$(searchRequest, options.abortSignal).toPromise(); + } if (response.error) { throw new RequestFailure(null, response); @@ -246,7 +277,6 @@ export class SearchSource { /** * Called by requests of this search source when they are started - * @param {Courier.Request} request * @param options * @return {Promise} */ @@ -430,7 +460,6 @@ export class SearchSource { * and `kibanaSavedObjectMeta.searchSourceJSON.filter[].meta.index`. * * Using `createSearchSource`, the instance can be re-created. - * @param searchSource The search source to serialize * @public */ public serialize() { const references: SavedObjectReference[] = []; diff --git a/src/plugins/data/public/search/search_strategy/no_op_search_strategy.ts b/src/plugins/data/public/search/search_strategy/no_op_search_strategy.ts deleted file mode 100644 index dc7331e614a0e4..00000000000000 --- a/src/plugins/data/public/search/search_strategy/no_op_search_strategy.ts +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { i18n } from '@kbn/i18n'; -import { SearchError } from './search_error'; -import { SearchStrategyProvider } from './types'; - -export const noOpSearchStrategy: SearchStrategyProvider = { - id: 'noOp', - - search: () => { - const searchError = new SearchError({ - status: '418', // "I'm a teapot" error - title: i18n.translate( - 'data.search.searchSource.noSearchStrategyRegisteredErrorMessageTitle', - { - defaultMessage: 'No search strategy registered', - } - ), - message: i18n.translate( - 'data.search.searchSource.noSearchStrategyRegisteredErrorMessageDescription', - { - defaultMessage: `Couldn't find a search strategy for the search request`, - } - ), - type: 'NO_OP_SEARCH_STRATEGY', - path: '', - }); - - return { - searching: Promise.reject(searchError), - abort: () => {}, - }; - }, - - isViable: () => { - return true; - }, -}; diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index ba6e44f47b75eb..2122e4e82ec1d7 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -22,7 +22,7 @@ import { createSearchSource } from './search_source'; import { SearchAggsSetup, SearchAggsStart, SearchAggsStartLegacy } from './aggs'; import { ISearch, ISearchGeneric } from './i_search'; import { TStrategyTypes } from './strategy_types'; -import { LegacyApiCaller } from './es_client'; +import { LegacyApiCaller } from './legacy/es_client'; import { SearchInterceptor } from './search_interceptor'; export interface ISearchContext { @@ -38,15 +38,6 @@ export interface ISearchStrategy { search: ISearch; } -/** - * 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. - */ -export type TSearchStrategyProviderEnhanced = ( - search: ISearchGeneric -) => Promise>; - export type TSearchStrategiesMap = { [K in TStrategyTypes]?: TSearchStrategyProvider; }; diff --git a/x-pack/plugins/rollup/public/search/rollup_search_strategy.ts b/x-pack/plugins/rollup/public/search/rollup_search_strategy.ts deleted file mode 100644 index d1c3b9cba5f9de..00000000000000 --- a/x-pack/plugins/rollup/public/search/rollup_search_strategy.ts +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { HttpSetup } from 'kibana/public'; -import { - SearchError, - getSearchErrorType, - IIndexPattern, - SearchStrategyProvider, - SearchResponse, - SearchRequest, -} from '../../../../../src/plugins/data/public'; - -function serializeFetchParams(searchRequests: SearchRequest[]) { - return JSON.stringify( - searchRequests.map(searchRequestWithFetchParams => { - const indexPattern = - searchRequestWithFetchParams.index.title || searchRequestWithFetchParams.index; - const { - body: { size, aggs, query: _query }, - } = searchRequestWithFetchParams; - - const query = { - size, - aggregations: aggs, - query: _query, - }; - - return { index: indexPattern, query }; - }) - ); -} - -// Rollup search always returns 0 hits, but visualizations expect search responses -// to return hits > 0, otherwise they do not render. We fake the number of hits here -// by counting the number of aggregation buckets/values returned by rollup search. -function shimHitsInFetchResponse(response: SearchResponse[]) { - return response.map(result => { - const buckets = result.aggregations - ? Object.keys(result.aggregations).reduce((allBuckets, agg) => { - return allBuckets.concat( - result.aggregations[agg].buckets || [result.aggregations[agg].value] || [] - ); - }, []) - : []; - return buckets && buckets.length - ? { - ...result, - hits: { - ...result.hits, - total: buckets.length, - }, - } - : result; - }); -} - -export const getRollupSearchStrategy = (fetch: HttpSetup['fetch']): SearchStrategyProvider => ({ - id: 'rollup', - - search: ({ searchRequests }) => { - // Serialize the fetch params into a format suitable for the body of an ES query. - const serializedFetchParams = serializeFetchParams(searchRequests); - - const controller = new AbortController(); - const promise = fetch('../api/rollup/search', { - signal: controller.signal, - method: 'POST', - body: serializedFetchParams, - }); - - return { - searching: promise.then(shimHitsInFetchResponse).catch(error => { - const { - body: { statusCode, error: title, message }, - res: { url }, - } = error; - - // Format fetch error as a SearchError. - const searchError = new SearchError({ - status: statusCode, - title, - message: `Rollup search error: ${message}`, - path: url, - type: getSearchErrorType({ message }) || '', - }); - - return Promise.reject(searchError); - }), - abort: () => controller.abort(), - }; - }, - - isViable: (indexPattern: IIndexPattern) => { - if (!indexPattern) { - return false; - } - - return indexPattern.type === 'rollup'; - }, -}); diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index c00c576f31a369..92251c0c25f9ca 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -850,8 +850,6 @@ "data.search.searchSource.indexPatternIdDescription": "{kibanaIndexPattern} インデックス内の ID です。", "data.search.searchSource.indexPatternIdLabel": "インデックスパターン ID", "data.search.searchSource.indexPatternLabel": "インデックスパターン", - "data.search.searchSource.noSearchStrategyRegisteredErrorMessageDescription": "検索リクエストの検索方法が見つかりませんでした", - "data.search.searchSource.noSearchStrategyRegisteredErrorMessageTitle": "検索方法が登録されていません", "data.search.searchSource.queryTimeDescription": "クエリの処理の所要時間です。リクエストの送信やブラウザでのパースの時間は含まれません。", "data.search.searchSource.queryTimeLabel": "クエリ時間", "data.search.searchSource.queryTimeValue": "{queryTime}ms", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index b4b61b29151d70..3533cea4fce60b 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -851,8 +851,6 @@ "data.search.searchSource.indexPatternIdDescription": "{kibanaIndexPattern} 索引中的 ID。", "data.search.searchSource.indexPatternIdLabel": "索引模式 ID", "data.search.searchSource.indexPatternLabel": "索引模式", - "data.search.searchSource.noSearchStrategyRegisteredErrorMessageDescription": "无法为该搜索请求找到搜索策略", - "data.search.searchSource.noSearchStrategyRegisteredErrorMessageTitle": "未注册任何搜索策略", "data.search.searchSource.queryTimeDescription": "处理查询所花费的时间。不包括发送请求或在浏览器中解析它的时间。", "data.search.searchSource.queryTimeLabel": "查询时间", "data.search.searchSource.queryTimeValue": "{queryTime}ms",