Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Update auto-complete-api-with-mds #8713

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
Loading