forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Indices discoverable in Kibana Global Search (elastic#175473)
## Summary Added a search provider to return only visible indices and land in the overview page once clicked or selected. ### No indices available <img width="1046" alt="Screenshot 2024-01-24 at 2 48 58 PM" src="https://github.com/elastic/kibana/assets/150824886/82d5a997-2d0b-42e0-8818-2f4b83170230"> ### Matched index shows up <img width="920" alt="Screenshot 2024-01-24 at 2 42 13 PM" src="https://github.com/elastic/kibana/assets/150824886/59bd676c-f5c3-4b10-a1ae-cd8b4008c81e"> ### Shows all matched index <img width="1048" alt="Screenshot 2024-01-24 at 2 50 09 PM" src="https://github.com/elastic/kibana/assets/150824886/30877185-ac9c-4fe2-b859-d597dd3941b1"> ### Checklist Delete any items that are not applicable to this PR. - [X] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [x] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [x] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
Showing
11 changed files
with
317 additions
and
2 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
x-pack/plugins/enterprise_search/public/assets/source_icons/index.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
x-pack/plugins/enterprise_search/server/utils/indices_search_result_provider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { NEVER, lastValueFrom } from 'rxjs'; | ||
|
||
import { IScopedClusterClient } from '@kbn/core/server'; | ||
|
||
import { ENTERPRISE_SEARCH_CONTENT_PLUGIN } from '../../common/constants'; | ||
|
||
import { getIndicesSearchResultProvider } from './indices_search_result_provider'; | ||
|
||
describe('Enterprise Search - indices search provider', () => { | ||
const basePathMock = { | ||
prepend: (input: string) => `/kbn${input}`, | ||
} as any; | ||
|
||
const indicesSearchResultProvider = getIndicesSearchResultProvider(basePathMock); | ||
|
||
const regularIndexResponse = { | ||
'search-github-api': { | ||
aliases: {}, | ||
}, | ||
'search-msft-sql-index': { | ||
aliases: {}, | ||
}, | ||
}; | ||
|
||
const mockClient = { | ||
asCurrentUser: { | ||
count: jest.fn().mockReturnValue({ count: 100 }), | ||
indices: { | ||
get: jest.fn(), | ||
stats: jest.fn(), | ||
}, | ||
security: { | ||
hasPrivileges: jest.fn(), | ||
}, | ||
}, | ||
asInternalUser: {}, | ||
}; | ||
const client = mockClient as unknown as IScopedClusterClient; | ||
mockClient.asCurrentUser.indices.get.mockResolvedValue(regularIndexResponse); | ||
|
||
const githubIndex = { | ||
id: 'search-github-api', | ||
score: 75, | ||
title: 'search-github-api', | ||
type: 'Index', | ||
url: { | ||
path: `${ENTERPRISE_SEARCH_CONTENT_PLUGIN.URL}/search_indices/search-github-api`, | ||
prependBasePath: true, | ||
}, | ||
icon: '/kbn/plugins/enterpriseSearch/assets/source_icons/index.svg', | ||
}; | ||
|
||
const msftIndex = { | ||
id: 'search-msft-sql-index', | ||
score: 75, | ||
title: 'search-msft-sql-index', | ||
type: 'Index', | ||
url: { | ||
path: `${ENTERPRISE_SEARCH_CONTENT_PLUGIN.URL}/search_indices/search-msft-sql-index`, | ||
prependBasePath: true, | ||
}, | ||
icon: '/kbn/plugins/enterpriseSearch/assets/source_icons/index.svg', | ||
}; | ||
|
||
beforeEach(() => {}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('find', () => { | ||
it('returns formatted results', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'search-github-api' }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([{ ...githubIndex, score: 100 }]); | ||
}); | ||
|
||
it('returns all matched results', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'search' }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([ | ||
{ ...githubIndex, score: 90 }, | ||
{ ...msftIndex, score: 90 }, | ||
]); | ||
}); | ||
|
||
it('returns all indices on empty string', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: '' }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toHaveLength(0); | ||
}); | ||
|
||
it('respect maximum results', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'search' }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 1, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([{ ...githubIndex, score: 90 }]); | ||
}); | ||
|
||
describe('returns empty results', () => { | ||
it('when term does not match with created indices', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'sample' }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([]); | ||
}); | ||
|
||
it('if client is undefined', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'sample' }, | ||
{ | ||
aborted$: NEVER, | ||
client: undefined, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([]); | ||
}); | ||
|
||
it('if tag is specified', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'search', tags: ['tag'] }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([]); | ||
}); | ||
|
||
it('if unknown type is specified', async () => { | ||
const results = await lastValueFrom( | ||
indicesSearchResultProvider.find( | ||
{ term: 'search', types: ['tag'] }, | ||
{ | ||
aborted$: NEVER, | ||
client, | ||
maxResults: 100, | ||
preference: '', | ||
}, | ||
{} as any | ||
) | ||
); | ||
expect(results).toEqual([]); | ||
}); | ||
}); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
x-pack/plugins/enterprise_search/server/utils/indices_search_result_provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { from, takeUntil } from 'rxjs'; | ||
|
||
import { IBasePath } from '@kbn/core-http-server'; | ||
import { | ||
GlobalSearchProviderResult, | ||
GlobalSearchResultProvider, | ||
} from '@kbn/global-search-plugin/server'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { ENTERPRISE_SEARCH_CONTENT_PLUGIN } from '../../common/constants'; | ||
|
||
import { getIndexData } from '../lib/indices/utils/get_index_data'; | ||
|
||
export function getIndicesSearchResultProvider(basePath: IBasePath): GlobalSearchResultProvider { | ||
return { | ||
find: ({ term, types, tags }, { aborted$, client, maxResults }) => { | ||
if (!client || !term || tags || (types && !types.includes('indices'))) { | ||
return from([[]]); | ||
} | ||
const fetchIndices = async (): Promise<GlobalSearchProviderResult[]> => { | ||
const { indexNames } = await getIndexData(client, false, false, term); | ||
|
||
const searchResults: GlobalSearchProviderResult[] = indexNames | ||
.map((indexName) => { | ||
let score = 0; | ||
const searchTerm = (term || '').toLowerCase(); | ||
const searchName = indexName.toLowerCase(); | ||
if (!searchTerm) { | ||
score = 80; | ||
} else if (searchName === searchTerm) { | ||
score = 100; | ||
} else if (searchName.startsWith(searchTerm)) { | ||
score = 90; | ||
} else if (searchName.includes(searchTerm)) { | ||
score = 75; | ||
} | ||
|
||
return { | ||
id: indexName, | ||
title: indexName, | ||
icon: basePath.prepend('/plugins/enterpriseSearch/assets/source_icons/index.svg'), | ||
type: i18n.translate('xpack.enterpriseSearch.searchIndexProvider.type.name', { | ||
defaultMessage: 'Index', | ||
}), | ||
url: { | ||
path: `${ENTERPRISE_SEARCH_CONTENT_PLUGIN.URL}/search_indices/${indexName}`, | ||
prependBasePath: true, | ||
}, | ||
score, | ||
}; | ||
}) | ||
.filter(({ score }) => score > 0) | ||
.slice(0, maxResults); | ||
return searchResults; | ||
}; | ||
return from(fetchIndices()).pipe(takeUntil(aborted$)); | ||
}, | ||
getSearchableTypes: () => ['indices'], | ||
id: 'enterpriseSearchIndices', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.