Skip to content

Commit

Permalink
[Vega] Supports runtime fields (elastic#98707) (elastic#99342)
Browse files Browse the repository at this point in the history
* [Vega] Support of Runtime Fields

* Refactor es_query_parser

* Refactor es_query_parser

* Remove unnecessary object creation

* Revert tests and move changes related to runtime fields into search_api

* Remove unnecessary code
  • Loading branch information
DianaDerevyankina authored May 5, 2021
1 parent d800252 commit 9f9c417
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/plugins/vis_type_vega/public/data_model/search_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* Side Public License, v 1.
*/

import { combineLatest } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { combineLatest, from } from 'rxjs';
import { map, tap, switchMap } from 'rxjs/operators';
import { CoreStart, IUiSettingsClient } from 'kibana/public';
import { getData } from '../services';
import {
getSearchParamsFromRequest,
SearchRequest,
Expand All @@ -19,6 +20,25 @@ import { search as dataPluginSearch } from '../../../data/public';
import { VegaInspectorAdapters } from '../vega_inspector';
import { RequestResponder } from '../../../inspector/public';

const extendSearchParamsWithRuntimeFields = async (
requestParams: ReturnType<typeof getSearchParamsFromRequest>,
indexPatternString?: string
) => {
if (indexPatternString) {
const indexPattern = (await getData().indexPatterns.find(indexPatternString)).find(
(index) => index.title === indexPatternString
);
const runtimeFields = indexPattern?.getComputedFields().runtimeFields;

return {
...requestParams,
body: { ...requestParams.body, runtime_mappings: runtimeFields },
};
}

return requestParams;
};

export interface SearchAPIDependencies {
uiSettings: IUiSettingsClient;
injectedMetadata: CoreStart['injectedMetadata'];
Expand All @@ -40,7 +60,7 @@ export class SearchAPI {
return combineLatest(
searchRequests.map((request) => {
const requestId = request.name;
const params = getSearchParamsFromRequest(request, {
const requestParams = getSearchParamsFromRequest(request, {
getConfig: this.dependencies.uiSettings.get.bind(this.dependencies.uiSettings),
});

Expand All @@ -49,18 +69,25 @@ export class SearchAPI {
...request,
searchSessionId: this.searchSessionId,
});
requestResponders[requestId].json(params.body);
requestResponders[requestId].json(requestParams.body);
}

return search
.search({ params }, { abortSignal: this.abortSignal, sessionId: this.searchSessionId })
.pipe(
tap((data) => this.inspectSearchResult(data, requestResponders[requestId])),
map((data) => ({
name: requestId,
rawResponse: data.rawResponse,
}))
);
return from(extendSearchParamsWithRuntimeFields(requestParams, request.index)).pipe(
switchMap((params) =>
search
.search(
{ params },
{ abortSignal: this.abortSignal, sessionId: this.searchSessionId }
)
.pipe(
tap((data) => this.inspectSearchResult(data, requestResponders[requestId])),
map((data) => ({
name: requestId,
rawResponse: data.rawResponse,
}))
)
)
);
})
);
}
Expand Down

0 comments on commit 9f9c417

Please sign in to comment.