-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Make Indices discoverable in Kibana Global Search #175473
Merged
Samiul-TheSoccerFan
merged 7 commits into
elastic:main
from
Samiul-TheSoccerFan:search-index-in-kibana-search
Feb 12, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b58b21f
ES client object added in find function to enable providers to call ES
Samiul-TheSoccerFan 0b74d3b
registered a provider to return indices to show up in global search
Samiul-TheSoccerFan 62f31b8
Fix linting errors in enterprise_search
Samiul-TheSoccerFan b35b841
Fix integration tests for find function
Samiul-TheSoccerFan 944e05d
Setting searchoptimization to false as we need to match with search term
Samiul-TheSoccerFan 6c9e46a
Added svg, icon path in the search results and refine search term
Samiul-TheSoccerFan 6d64980
Merge branch 'main' into search-index-in-kibana-search
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpicking here, but I think it's neater to pass the concrete Elasticsearch Client with permissions (so the result of
client.asCurrentUser
) instead of the cluster client which still needs to choose betweenasCurrentUser
andasInternalUser
, if possible?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_index_data
expects aClusterClient
in here and that is why I kept it asclusterClient
. Should I write a different method which expects onlycurrentUser
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably pass that function an ElasticsearchClient in all of its usages, but maybe that's too much effort for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch_indices
andindices_search_results_providers*
both is using this function and probably their related test files. So not much, however, then we should updategetSearchIndexData
function and its usage too.Is it okay if I create a separate small PR to handle this update for both functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sphilipse : Can I get an approval if everything looks good?