Skip to content

Commit

Permalink
move serialize into search source
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Mar 30, 2020
1 parent 3360b12 commit 40aeded
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
| [getSearchErrorType({ message })](./kibana-plugin-plugins-data-public.getsearcherrortype.md) | |
| [getTime(indexPattern, timeRange, forceNow)](./kibana-plugin-plugins-data-public.gettime.md) | |
| [plugin(initializerContext)](./kibana-plugin-plugins-data-public.plugin.md) | |
| [serializeSearchSource(searchSource)](./kibana-plugin-plugins-data-public.serializesearchsource.md) | Serializes a <code>SearchSource</code> instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.<!-- -->The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named <code>kibanaSavedObjectMeta.searchSourceJSON.index</code> and <code>kibanaSavedObjectMeta.searchSourceJSON.filter[&lt;number&gt;].meta.index</code>.<!-- -->Using <code>parseSearchSource</code>, the instance can be re-created. |

## Interfaces

Expand Down Expand Up @@ -111,7 +110,7 @@
| [FilterBar](./kibana-plugin-plugins-data-public.filterbar.md) | |
| [getKbnTypeNames](./kibana-plugin-plugins-data-public.getkbntypenames.md) | Get the esTypes known by all kbnFieldTypes {<!-- -->Array<string>} |
| [indexPatterns](./kibana-plugin-plugins-data-public.indexpatterns.md) | |
| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a <code>SearchSource</code> instance. Use this method to re-create the search source serialized using <code>serializeSearchSource</code> |
| [parseSearchSource](./kibana-plugin-plugins-data-public.parsesearchsource.md) | Deserializes a json string and a set of referenced objects to a <code>SearchSource</code> instance. Use this method to re-create the search source serialized using <code>searchSource.serialize</code>.<!-- -->This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service |
| [QueryStringInput](./kibana-plugin-plugins-data-public.querystringinput.md) | |
| [search](./kibana-plugin-plugins-data-public.search.md) | |
| [SearchBar](./kibana-plugin-plugins-data-public.searchbar.md) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## parseSearchSource variable

Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `serializeSearchSource`
Deserializes a json string and a set of referenced objects to a `SearchSource` instance. Use this method to re-create the search source serialized using `searchSource.serialize`<!-- -->.

This function is a factory function that returns the actual utility when calling it with the required service dependency (index patterns contract). A pre-wired version is also exposed in the start contract of the data plugin as part of the search service

<b>Signature:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export declare class SearchSource
| [getParent()](./kibana-plugin-plugins-data-public.searchsource.getparent.md) | | Get the parent of this SearchSource {<!-- -->undefined\|searchSource<!-- -->} |
| [getSearchRequestBody()](./kibana-plugin-plugins-data-public.searchsource.getsearchrequestbody.md) | | |
| [onRequestStart(handler)](./kibana-plugin-plugins-data-public.searchsource.onrequeststart.md) | | Add a handler that will be notified whenever requests start |
| [serialize()](./kibana-plugin-plugins-data-public.searchsource.serialize.md) | | Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.<!-- -->The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named <code>kibanaSavedObjectMeta.searchSourceJSON.index</code> and <code>kibanaSavedObjectMeta.searchSourceJSON.filter[&lt;number&gt;].meta.index</code>.<!-- -->Using <code>parseSearchSource</code>, the instance can be re-created. |
| [setField(field, value)](./kibana-plugin-plugins-data-public.searchsource.setfield.md) | | |
| [setFields(newFields)](./kibana-plugin-plugins-data-public.searchsource.setfields.md) | | |
| [setParent(parent, options)](./kibana-plugin-plugins-data-public.searchsource.setparent.md) | | Set a searchSource that this source should inherit from |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [SearchSource](./kibana-plugin-plugins-data-public.searchsource.md) &gt; [serialize](./kibana-plugin-plugins-data-public.searchsource.serialize.md)

## SearchSource.serialize() method

Serializes the instance to a JSON string and a set of referenced objects. Use this method to get a representation of the search source which can be stored in a saved object.

The references returned by this function can be mixed with other references in the same object, however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index` and `kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index`<!-- -->.

Using `parseSearchSource`<!-- -->, the instance can be re-created.

<b>Signature:</b>

```typescript
serialize(): {
searchSourceJSON: string;
references: SavedObjectReference[];
};
```
<b>Returns:</b>

`{
searchSourceJSON: string;
references: SavedObjectReference[];
}`

This file was deleted.

1 change: 0 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ export {
ISearchSource,
SearchSource,
parseSearchSource,
serializeSearchSource,
SearchSourceFields,
EsQuerySortValue,
SortDirection,
Expand Down
40 changes: 19 additions & 21 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,10 @@ export class SearchSource {
// (undocumented)
history: SearchRequest[];
onRequestStart(handler: (searchSource: ISearchSource, options?: FetchOptions) => Promise<unknown>): void;
serialize(): {
searchSourceJSON: string;
references: SavedObjectReference[];
};
// (undocumented)
setField<K extends keyof SearchSourceFields>(field: K, value: SearchSourceFields[K]): this;
// (undocumented)
Expand Down Expand Up @@ -1735,12 +1739,6 @@ export interface SearchStrategyProvider {
search: (params: SearchStrategySearchParams) => SearchStrategyResponse;
}

// @public
export function serializeSearchSource(searchSource: ISearchSource): {
searchSourceJSON: string;
references: SavedObjectReference[];
};

// 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)
Expand Down Expand Up @@ -1891,21 +1889,21 @@ export type TSearchStrategyProvider<T extends TStrategyTypes> = (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:384:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:384:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:389:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:390:1 - (ae-forgotten-export) The symbol "convertDateRangeToString" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:392:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:401:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:402:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:407:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:410:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:411:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:414:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" 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/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
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export {
EsQuerySortValue,
SortDirection,
parseSearchSource,
serializeSearchSource,
} from './search_source';

export { SearchInterceptor } from './search_interceptor';
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/search/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const searchStartMock: jest.Mocked<ISearchStart> = {
setInterceptor: jest.fn(),
search: jest.fn(),
parseSearchSource: jest.fn(),
serializeSearchSource: jest.fn(),
__LEGACY: {
AggConfig: jest.fn() as any,
AggType: jest.fn(),
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { getEsClient, LegacyApiCaller } from './es_client';
import { ES_SEARCH_STRATEGY, DEFAULT_SEARCH_STRATEGY } from '../../common/search';
import { esSearchStrategyProvider } from './es_search/es_search_strategy';
import { IndexPatternsContract } from '../index_patterns/index_patterns';
import { parseSearchSource, serializeSearchSource } from './search_source';
import { parseSearchSource } from './search_source';
import { QuerySetup } from '../query/query_service';
import { SearchInterceptor } from './search_interceptor';
import {
Expand Down Expand Up @@ -145,7 +145,6 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
// TODO: should an intercepror have a destroy method?
this.searchInterceptor = searchInterceptor;
},
serializeSearchSource,
parseSearchSource: parseSearchSource(indexPatterns),
__LEGACY: {
esClient: this.esClient!,
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/search/search_source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@

export * from './search_source';
export { parseSearchSource } from './parse_search_source';
export { serializeSearchSource } from './serialize_search_source';
export { SortDirection, EsQuerySortValue, SearchSourceFields } from './types';
1 change: 1 addition & 0 deletions src/plugins/data/public/search/search_source/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export const searchSourceMock: MockedKeys<ISearchSource> = {
getSearchRequestBody: jest.fn(),
destroy: jest.fn(),
history: [],
serialize: jest.fn(),
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { SearchSourceFields } from './types';

/**
* Deserializes a json string and a set of referenced objects to a `SearchSource` instance.
* Use this method to re-create the search source serialized using `serializeSearchSource`.
* Use this method to re-create the search source serialized using `searchSource.serialize`.
*
* This function is a factory function that returns the actual utility when calling it with the
* required service dependency (index patterns contract). A pre-wired version is also exposed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { SearchSource } from './search_source';
import { IndexPattern } from '../..';
import { IndexPattern, SortDirection } from '../..';
import { mockDataServices } from '../aggs/test_helpers';

jest.mock('../fetch', () => ({
Expand Down Expand Up @@ -150,4 +150,77 @@ describe('SearchSource', function() {
expect(parentFn).toBeCalledWith(searchSource, options);
});
});

describe('#serialize', function() {
it('should reference index patterns', () => {
const indexPattern123 = { id: '123' } as IndexPattern;
const searchSource = new SearchSource();
searchSource.setField('index', indexPattern123);
const { searchSourceJSON, references } = searchSource.serialize();
expect(references[0].id).toEqual('123');
expect(references[0].type).toEqual('index-pattern');
expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name);
});

it('should add other fields', () => {
const searchSource = new SearchSource();
searchSource.setField('highlightAll', true);
searchSource.setField('from', 123456);
const { searchSourceJSON } = searchSource.serialize();
expect(JSON.parse(searchSourceJSON).highlightAll).toEqual(true);
expect(JSON.parse(searchSourceJSON).from).toEqual(123456);
});

it('should omit sort and size', () => {
const searchSource = new SearchSource();
searchSource.setField('highlightAll', true);
searchSource.setField('from', 123456);
searchSource.setField('sort', { field: SortDirection.asc });
searchSource.setField('size', 200);
const { searchSourceJSON } = searchSource.serialize();
expect(Object.keys(JSON.parse(searchSourceJSON))).toEqual(['highlightAll', 'from']);
});

it('should serialize filters', () => {
const searchSource = new SearchSource();
const filter = [
{
query: 'query',
meta: {
alias: 'alias',
disabled: false,
negate: false,
},
},
];
searchSource.setField('filter', filter);
const { searchSourceJSON } = searchSource.serialize();
expect(JSON.parse(searchSourceJSON).filter).toEqual(filter);
});

it('should reference index patterns in filters separately from index field', () => {
const searchSource = new SearchSource();
const indexPattern123 = { id: '123' } as IndexPattern;
searchSource.setField('index', indexPattern123);
const filter = [
{
query: 'query',
meta: {
alias: 'alias',
disabled: false,
negate: false,
index: '456',
},
},
];
searchSource.setField('filter', filter);
const { searchSourceJSON, references } = searchSource.serialize();
expect(references[0].id).toEqual('123');
expect(references[0].type).toEqual('index-pattern');
expect(JSON.parse(searchSourceJSON).indexRefName).toEqual(references[0].name);
expect(references[1].id).toEqual('456');
expect(references[1].type).toEqual('index-pattern');
expect(JSON.parse(searchSourceJSON).filter[0].meta.indexRefName).toEqual(references[1].name);
});
});
});
Loading

0 comments on commit 40aeded

Please sign in to comment.