Skip to content

Commit

Permalink
[Enhancement] Update auto-complete-api-with-mds (#8713)
Browse files Browse the repository at this point in the history
* Update auto-complete-api-with-mds

Signed-off-by: sumukhswamy <[email protected]>

* Update auto-complete-api-with-mds

Signed-off-by: sumukhswamy <[email protected]>

* added changelog

Signed-off-by: sumukhswamy <[email protected]>

---------

Signed-off-by: sumukhswamy <[email protected]>
  • Loading branch information
sumukhswamy authored Oct 28, 2024
1 parent 90c40da commit c105d94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8713.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
-[Enhancement] Update auto-complete-api-with-mds ([#8713](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8713))
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ export const getEmptyValueSuggestions = (() => Promise.resolve([])) as ValueSugg

export const setupValueSuggestionProvider = (core: CoreSetup): ValueSuggestionsGetFn => {
const requestSuggestions = memoize(
(index: string, field: IFieldType, query: string, boolFilter: any = [], signal?: AbortSignal) =>
(
index: string,
field: IFieldType,
query: string,
boolFilter: any = [],
signal?: AbortSignal,
dataSourceId?: string
) =>
core.http.fetch(`/api/opensearch-dashboards/suggestions/values/${index}`, {
method: 'POST',
body: JSON.stringify({ query, field: field.name, boolFilter }),
body: JSON.stringify({ query, field: field.name, boolFilter, dataSourceId }),
signal,
}),
resolver
Expand All @@ -79,7 +86,13 @@ export const setupValueSuggestionProvider = (core: CoreSetup): ValueSuggestionsG
} else if (!shouldSuggestValues || !field.aggregatable || field.type !== 'string') {
return [];
}

return await requestSuggestions(title, field, query, boolFilter, signal);
return await requestSuggestions(
title,
field,
query,
boolFilter,
signal,
indexPattern.dataSourceRef?.id
);
};
};
14 changes: 11 additions & 3 deletions src/plugins/data/server/autocomplete/value_suggestions_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ export function registerValueSuggestionsRoute(
field: schema.string(),
query: schema.string(),
boolFilter: schema.maybe(schema.any()),
dataSourceId: schema.maybe(schema.string({ defaultValue: '' })),
},
{ unknowns: 'allow' }
),
},
},
async (context, request, response) => {
const config = await config$.pipe(first()).toPromise();
const { field: fieldName, query, boolFilter } = request.body;
const { field: fieldName, query, boolFilter, dataSourceId } = request.body;
const { index } = request.params;
const { client } = context.core.opensearch.legacy;
const signal = getRequestAbortedSignal(request.events.aborted$);
Expand All @@ -80,8 +81,15 @@ export function registerValueSuggestionsRoute(
const body = await getBody(autocompleteSearchOptions, field || fieldName, query, boolFilter);

try {
const result = await client.callAsCurrentUser('search', { index, body }, { signal });

let result;
if (dataSourceId) {
const dataSourceClient = await context.dataSource.opensearch.legacy.getClient(
dataSourceId
);
result = await dataSourceClient.callAPI('search', { index, body }, { signal });
} else {
result = await client.callAsCurrentUser('search', { index, body }, { signal });
}
const buckets: any[] =
get(result, 'aggregations.suggestions.buckets') ||
get(result, 'aggregations.nestedSuggestions.suggestions.buckets');
Expand Down

0 comments on commit c105d94

Please sign in to comment.