Skip to content

Commit

Permalink
[data.search] Move SearchSource to common directory. (#77823)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers authored Sep 18, 2020
1 parent bf26dff commit 0f493fa
Show file tree
Hide file tree
Showing 43 changed files with 206 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
export * from './aggs';
export * from './es_search';
export * from './expressions';
export * from './search_source';
export * from './tabify';
export * from './types';
export * from './es_search';
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import { createSearchSource as createSearchSourceFactory } from './create_search_source';
import { SearchSourceDependencies } from './search_source';
import { IIndexPattern } from '../../../common/index_patterns';
import { IIndexPattern } from '../../index_patterns';
import { IndexPatternsContract } from '../../index_patterns/index_patterns';
import { Filter } from '../../../common/es_query/filters';
import { Filter } from '../../es_query/filters';
import { BehaviorSubject } from 'rxjs';

describe('createSearchSource', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { SavedObjectReference } from '../../../../../core/types';
import { Filter } from '../../../common/es_query/filters';
import { SavedObjectReference } from 'src/core/types';
import { Filter } from '../../es_query/filters';
import { SearchSourceFields } from './types';

export const extractReferences = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

import { UI_SETTINGS } from '../../../constants';
import { GetConfigFn } from '../../../types';
import { getSearchParams } from './get_search_params';
import { GetConfigFn, UI_SETTINGS } from '../../../common';

function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

import { UI_SETTINGS, ISearchRequestParams, GetConfigFn } from '../../../common';
import { UI_SETTINGS } from '../../../constants';
import { GetConfigFn } from '../../../types';
import { ISearchRequestParams } from '../../index';
import { SearchRequest } from './types';

const sessionId = Date.now();
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/data/common/search/search_source/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

export { getSearchParams, getSearchParamsFromRequest, getPreference } from './get_search_params';
export { RequestFailure } from './request_error';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { SearchResponse } from 'elasticsearch';
import { KbnError } from '../../../../kibana_utils/common';
import { KbnError } from '../../../../../kibana_utils/common';
import { SearchError } from './types';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

import { SearchResponse } from 'elasticsearch';
import { GetConfigFn } from '../../../common';
import { LegacyFetchHandlers } from '../legacy/types';
import { GetConfigFn } from '../../../types';

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ export { SortDirection, EsQuerySortValue, SearchSourceFields } from './types';
export { injectReferences } from './inject_references';
export { extractReferences } from './extract_references';
export { parseSearchSourceJSON } from './parse_json';
export * from './fetch';
export * from './legacy';
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { SavedObjectReference } from 'src/core/types';
import { SearchSourceFields } from './types';
import { SavedObjectReference } from '../../../../../core/types';

export const injectReferences = (
searchSourceFields: SearchSourceFields & { indexRefName: string },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/

import { SearchResponse } from 'elasticsearch';
import { ISearchOptions } from 'src/plugins/data/common';
import { FetchHandlers } from '../fetch';
import { FetchHandlers, SearchRequest } from '../fetch';
import { defaultSearchStrategy } from './default_search_strategy';
import { SearchRequest } from '../index';
import { ISearchOptions } from '../../index';

export function callClient(
searchRequests: SearchRequest[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,50 @@
* under the License.
*/

import { HttpStart } from 'src/core/public';
import { coreMock } from '../../../../../core/public/mocks';
import { getCallMsearch } from './call_msearch';
import { defaultSearchStrategy } from './default_search_strategy';
import { LegacyFetchHandlers, SearchStrategySearchParams } from './types';
import { BehaviorSubject } from 'rxjs';

const { search } = defaultSearchStrategy;

const msearchMock = jest.fn().mockResolvedValue({ body: { responses: [] } });

describe('defaultSearchStrategy', function () {
describe('search', function () {
describe('defaultSearchStrategy', () => {
describe('search', () => {
let searchArgs: MockedKeys<SearchStrategySearchParams>;
let http: jest.Mocked<HttpStart>;

beforeEach(() => {
msearchMock.mockClear();

http = coreMock.createStart().http;
http.post.mockResolvedValue(msearchMock);

searchArgs = {
searchRequests: [
{
index: { title: 'foo' },
body: {},
},
],
getConfig: jest.fn(),
onResponse: (req, res) => res,
legacy: {
callMsearch: getCallMsearch({ http }),
callMsearch: jest.fn().mockResolvedValue(undefined),
loadingCount$: new BehaviorSubject(0) as any,
} as jest.Mocked<LegacyFetchHandlers>,
};
});

test('calls http.post with the correct arguments', async () => {
test('calls callMsearch with the correct arguments', async () => {
await search({ ...searchArgs });
expect(http.post.mock.calls).toMatchInlineSnapshot(`
expect(searchArgs.legacy.callMsearch.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"/internal/_msearch",
Object {
"body": "{\\"searches\\":[{\\"header\\":{\\"index\\":\\"foo\\"}}]}",
"body": Object {
"searches": Array [
Object {
"body": Object {},
"header": Object {
"index": "foo",
"preference": undefined,
},
},
],
},
"signal": AbortSignal {},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
* under the License.
*/

import { fetchSoon } from './fetch_soon';
import { callClient } from './call_client';
import { FetchHandlers } from '../fetch/types';
import { SearchRequest } from '../index';
import { SearchResponse } from 'elasticsearch';
import { GetConfigFn, UI_SETTINGS, ISearchOptions } from '../../../common';
import { UI_SETTINGS } from '../../../constants';
import { GetConfigFn } from '../../../types';
import { FetchHandlers, SearchRequest } from '../fetch';
import { ISearchOptions } from '../../index';
import { callClient } from './call_client';
import { fetchSoon } from './fetch_soon';

function getConfigStub(config: any = {}): GetConfigFn {
return (key) => config[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/

import { SearchResponse } from 'elasticsearch';
import { UI_SETTINGS } from '../../../constants';
import { FetchHandlers, SearchRequest } from '../fetch';
import { ISearchOptions } from '../../index';
import { callClient } from './call_client';
import { FetchHandlers } from '../fetch/types';
import { SearchRequest } from '../index';
import { UI_SETTINGS, ISearchOptions } from '../../../common';

/**
* This function introduces a slight delay in the request process to allow multiple requests to queue
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/data/common/search/search_source/legacy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

export { fetchSoon } from './fetch_soon';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

import { BehaviorSubject } from 'rxjs';
import { SearchResponse } from 'elasticsearch';
import { FetchHandlers } from '../fetch';
import { SearchRequest } from '..';
import { FetchHandlers, SearchRequest } from '../fetch';

// @internal
export interface LegacyFetchHandlers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { has } from 'lodash';
import { Query } from 'src/plugins/data/public';
import { Query } from '../../query/types';

/**
* Creates a standardized query object from old queries that were either strings or pure ES query DSL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { normalizeSortRequest } from './normalize_sort_request';
import { SortDirection } from './types';
import { IIndexPattern } from '../..';
import { IIndexPattern } from '../../index_patterns';

describe('SearchSource#normalizeSortRequest', function () {
const scriptedField = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { IIndexPattern } from '../..';
import { IIndexPattern } from '../../index_patterns';
import { EsQuerySortValue, SortOptions } from './types';

export function normalizeSortRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/

import { Observable, BehaviorSubject } from 'rxjs';
import { GetConfigFn } from 'src/plugins/data/common';
import { SearchSource, SearchSourceDependencies } from './search_source';
import { IndexPattern, SortDirection } from '../..';
import { fetchSoon } from '../legacy';
import { IndexPattern } from '../../index_patterns';
import { GetConfigFn } from '../../types';
import { fetchSoon } from './legacy';
import { SearchSource, SearchSourceDependencies, SortDirection } from './';

jest.mock('../legacy', () => ({
jest.mock('./legacy', () => ({
fetchSoon: jest.fn().mockResolvedValue({}),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ import { map } from 'rxjs/operators';
import { normalizeSortRequest } from './normalize_sort_request';
import { filterDocvalueFields } from './filter_docvalue_fields';
import { fieldWildcardFilter } from '../../../../kibana_utils/common';
import { IIndexPattern, ISearchGeneric } from '../..';
import { IIndexPattern } from '../../index_patterns';
import { ISearchGeneric } from '../..';
import { SearchSourceOptions, SearchSourceFields } from './types';
import { FetchHandlers, RequestFailure, getSearchParamsFromRequest, SearchRequest } from '../fetch';
import { FetchHandlers, RequestFailure, getSearchParamsFromRequest, SearchRequest } from './fetch';

import {
getEsQueryConfig,
Expand All @@ -87,7 +88,7 @@ import {
ISearchOptions,
} from '../../../common';
import { getHighlightRequest } from '../../../common/field_formats';
import { fetchSoon } from '../legacy';
import { fetchSoon } from './legacy';
import { extractReferences } from './extract_references';

/** @internal */
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/data/common/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
* under the License.
*/

import { Observable } from 'rxjs';
import { IEsSearchRequest, IEsSearchResponse, ISearchOptions } from '../../common/search';

export type ISearch = (
request: IKibanaSearchRequest,
options?: ISearchOptions
) => Observable<IKibanaSearchResponse>;

export type ISearchGeneric = <
SearchStrategyRequest extends IEsSearchRequest = IEsSearchRequest,
SearchStrategyResponse extends IEsSearchResponse = IEsSearchResponse
>(
request: SearchStrategyRequest,
options?: ISearchOptions
) => Observable<SearchStrategyResponse>;

export interface IKibanaSearchResponse {
/**
* Some responses may contain a unique id to identify the request this response came from.
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { Required } from '@kbn/utility-types';
import * as Rx from 'rxjs';
import { SavedObject } from 'src/core/server';
import { SavedObject as SavedObject_3 } from 'src/core/public';
import { SavedObjectReference as SavedObjectReference_2 } from 'src/core/types';
import { SavedObjectsClientContract } from 'src/core/public';
import { Search } from '@elastic/elasticsearch/api/requestParams';
import { SearchResponse } from 'elasticsearch';
Expand Down Expand Up @@ -651,13 +652,12 @@ export type ExistsFilter = Filter & {
// @public (undocumented)
export const expandShorthand: (sh: Record<string, ShorthandFieldMapObject>) => MappingObject;

// Warning: (ae-forgotten-export) The symbol "SavedObjectReference" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "extractReferences" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export const extractSearchSourceReferences: (state: SearchSourceFields) => [SearchSourceFields & {
indexRefName?: string;
}, SavedObjectReference[]];
}, SavedObjectReference_2[]];

// Warning: (ae-missing-release-tag) "FieldFormat" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
Expand Down Expand Up @@ -1371,7 +1371,7 @@ export interface IndexPatternTypeMeta {
// @public (undocumented)
export const injectSearchSourceReferences: (searchSourceFields: SearchSourceFields & {
indexRefName: string;
}, references: SavedObjectReference[]) => SearchSourceFields;
}, references: SavedObjectReference_2[]) => SearchSourceFields;

// Warning: (ae-missing-release-tag) "InputTimeRange" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
getRequestInspectorStats,
getResponseInspectorStats,
IAggConfigs,
ISearchSource,
tabifyAggResponse,
} from '../../../common/search';

Expand All @@ -48,7 +49,6 @@ import {
getQueryService,
getSearchService,
} from '../../services';
import { ISearchSource } from '../search_source';
import { buildTabularInspectorData } from './build_tabular_inspector_data';
import { serializeAggConfig } from './utils';

Expand Down
Loading

0 comments on commit 0f493fa

Please sign in to comment.