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]>
(cherry picked from commit c105d94)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Oct 28, 2024
1 parent a887cec commit 54676cb
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;

Check warning on line 68 in src/plugins/data/server/autocomplete/value_suggestions_route.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/autocomplete/value_suggestions_route.ts#L68

Added line #L68 was not covered by tests
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(

Check warning on line 86 in src/plugins/data/server/autocomplete/value_suggestions_route.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/autocomplete/value_suggestions_route.ts#L86

Added line #L86 was not covered by tests
dataSourceId
);
result = await dataSourceClient.callAPI('search', { index, body }, { signal });

Check warning on line 89 in src/plugins/data/server/autocomplete/value_suggestions_route.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/autocomplete/value_suggestions_route.ts#L89

Added line #L89 was not covered by tests
} else {
result = await client.callAsCurrentUser('search', { index, body }, { signal });

Check warning on line 91 in src/plugins/data/server/autocomplete/value_suggestions_route.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/autocomplete/value_suggestions_route.ts#L91

Added line #L91 was not covered by tests
}
const buckets: any[] =
get(result, 'aggregations.suggestions.buckets') ||
get(result, 'aggregations.nestedSuggestions.suggestions.buckets');
Expand Down

0 comments on commit 54676cb

Please sign in to comment.