Skip to content

Commit

Permalink
fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Dec 17, 2019
1 parent da9d2c7 commit df746fc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
22 changes: 3 additions & 19 deletions src/plugins/data/server/autocomplete/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,11 @@
* under the License.
*/

import { first } from 'rxjs/operators';
import {
APICaller,
CallAPIOptions,
CoreSetup,
ElasticsearchServiceSetup,
KibanaRequest,
} from 'kibana/server';
import { CoreSetup } from 'kibana/server';
import { registerValueSuggestionsRoute } from './value_suggestions_route';

const getAPICallerFn = (elasticsearch: ElasticsearchServiceSetup) => async (
request: KibanaRequest
): Promise<APICaller> => {
const client = await elasticsearch.dataClient$.pipe(first()).toPromise();

return (endpoint: string, params?: Record<string, any>, options?: CallAPIOptions) =>
client.asScoped(request).callAsCurrentUser(endpoint, params, options);
};

export function registerRoutes({ http, elasticsearch }: CoreSetup): void {
export function registerRoutes({ http }: CoreSetup): void {
const router = http.createRouter();

registerValueSuggestionsRoute(router, getAPICallerFn(elasticsearch));
registerValueSuggestionsRoute(router);
}
11 changes: 4 additions & 7 deletions src/plugins/data/server/autocomplete/value_suggestions_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@

import { get, map } from 'lodash';
import { schema } from '@kbn/config-schema';
import { APICaller, IRouter, KibanaRequest } from 'kibana/server';
import { IRouter } from 'kibana/server';

import { IFieldType, indexPatterns, esFilters } from '../index';

export function registerValueSuggestionsRoute(
router: IRouter,
apiCaller: (request: KibanaRequest) => Promise<APICaller>
) {
export function registerValueSuggestionsRoute(router: IRouter) {
router.post(
{
path: '/api/kibana/suggestions/values/{index}',
Expand All @@ -51,6 +48,7 @@ export function registerValueSuggestionsRoute(
const { client: uiSettings } = context.core.uiSettings;
const { field: fieldName, query, boolFilter } = request.body;
const { index } = request.params;
const { dataClient } = context.core.elasticsearch;

const autocompleteSearchOptions = {
timeout: await uiSettings.get<number>('kibana.autocompleteTimeout'),
Expand All @@ -66,8 +64,7 @@ export function registerValueSuggestionsRoute(
const body = await getBody(autocompleteSearchOptions, field || fieldName, query, boolFilter);

try {
const callCluster = await apiCaller(request);
const result = await callCluster('search', { index, body });
const result = await dataClient.callAsCurrentUser('search', { index, body });

const buckets: any[] =
get(result, 'aggregations.suggestions.buckets') ||
Expand Down

0 comments on commit df746fc

Please sign in to comment.