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

[GS] add the provider field to GlobalSearchResult #68878

Closed
Closed
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
4 changes: 4 additions & 0 deletions rfcs/text/0011_global_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ type GlobalSearchResult = Omit<GlobalSearchProviderResult, 'url'> & {
* This can be either an absolute url, or a relative path including the basePath
*/
url: string;
/**
* The id of the provider this result originated from.
*/
provider: string;
};


Expand Down
17 changes: 14 additions & 3 deletions x-pack/plugins/global_search/common/process_result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const createResult = (parts: Partial<GlobalSearchProviderResult>): GlobalSearchP
...parts,
});

const provider = { id: 'provider' };

describe('processProviderResult', () => {
let basePath: jest.Mocked<IBasePath>;

Expand All @@ -43,9 +45,10 @@ describe('processProviderResult', () => {
meta: { hello: 'dolly' },
});

expect(processProviderResult(r1, basePath)).toEqual({
expect(processProviderResult(r1, provider, basePath)).toEqual({
...r1,
url: expect.any(String),
provider: expect.any(String),
});
});

Expand All @@ -58,16 +61,24 @@ describe('processProviderResult', () => {

expect(convertResultUrlMock).not.toHaveBeenCalled();

const g1 = processProviderResult(r1, basePath);
const g1 = processProviderResult(r1, provider, basePath);

expect(g1.url).toEqual('/url-A');
expect(convertResultUrlMock).toHaveBeenCalledTimes(1);
expect(convertResultUrlMock).toHaveBeenCalledWith(r1.url, basePath);

const g2 = processProviderResult(r2, basePath);
const g2 = processProviderResult(r2, provider, basePath);

expect(g2.url).toEqual('/url-B');
expect(convertResultUrlMock).toHaveBeenCalledTimes(2);
expect(convertResultUrlMock).toHaveBeenCalledWith(r2.url, basePath);
});

it('adds the `provider` field to the result', () => {
const g1 = processProviderResult(createResult({}), { id: 'providerA' }, basePath);
const g2 = processProviderResult(createResult({}), { id: 'providerB' }, basePath);

expect(g1.provider).toEqual('providerA');
expect(g2.provider).toEqual('providerB');
});
});
2 changes: 2 additions & 0 deletions x-pack/plugins/global_search/common/process_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import { convertResultUrl, IBasePath } from './utils';
*/
export const processProviderResult = (
result: GlobalSearchProviderResult,
provider: { id: string },
basePath: IBasePath
): GlobalSearchResult => {
return {
...result,
provider: provider.id,
url: convertResultUrl(result.url, basePath),
};
};
4 changes: 4 additions & 0 deletions x-pack/plugins/global_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export type GlobalSearchResult = Omit<GlobalSearchProviderResult, 'url'> & {
* This can be either an absolute url, or a relative path including the basePath
*/
url: string;
/**
* The id of the provider this result originated from.
*/
provider: string;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const createResult = (id: string, parts: Partial<GlobalSearchResult> = {}): Glob
title: id,
type: 'type',
url: `/path/to/${id}`,
provider: 'provider',
score: 100,
...parts,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('SearchService', () => {
type: 'test',
url: '/foo/bar',
score: 100,
provider: 'provider',
...parts,
id,
});
Expand Down Expand Up @@ -397,10 +398,12 @@ describe('SearchService', () => {
expect(batch.results).toHaveLength(2);
expect(batch.results[0]).toEqual({
...resultA,
provider: 'A',
url: '/base-path/foo/bar',
});
expect(batch.results[1]).toEqual({
...resultB,
provider: 'A',
url: '/foo',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ export class SearchService {
aborted$,
};

const processResult = (result: GlobalSearchProviderResult) =>
processProviderResult(result, this.http!.basePath);
const processResult = (
result: GlobalSearchProviderResult,
provider: GlobalSearchResultProvider
) => processProviderResult(result, provider, this.http!.basePath);

const serverResults$ = fetchServerResults(this.http!, term, {
preference,
Expand All @@ -151,7 +153,7 @@ export class SearchService {
provider.find(term, providerOptions).pipe(
takeInArray(this.maxProviderResults),
takeUntil(aborted$),
map((results) => results.map((r) => processResult(r)))
map((results) => results.map((r) => processResult(r, provider)))
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const createResult = (id: string): GlobalSearchResult => ({
title: id,
type: 'test',
url: `/app/test/${id}`,
provider: 'provider',
score: 42,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,12 @@ describe('SearchService', () => {
expect(batch.results).toHaveLength(2);
expect(batch.results[0]).toEqual({
...resultA,
provider: 'A',
url: '/base-path/foo/bar',
});
expect(batch.results[1]).toEqual({
...resultB,
provider: 'A',
url: '/foo',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ export class SearchService {
aborted$,
};

const processResult = (result: GlobalSearchProviderResult) =>
processProviderResult(result, this.basePath!);
const processResult = (
result: GlobalSearchProviderResult,
provider: GlobalSearchResultProvider
) => processProviderResult(result, provider, this.basePath!);

const providersResults$ = [...this.providers.values()].map((provider) =>
provider.find(term, providerOptions, context).pipe(
takeInArray(this.maxProviderResults),
takeUntil(aborted$),
map((results) => results.map((r) => processResult(r)))
map((results) => results.map((r) => processResult(r, provider)))
)
);

Expand Down