Skip to content

Commit

Permalink
use type filter in providers
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Nov 17, 2020
1 parent 46f6543 commit a12bbdb
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/global_search/common/search_syntax/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
*/

export { parseSearchParams } from './parse_search_params';
export { SearchParams, FilterValues, FilterValueType } from './types';
export { GlobalSearchProviderFindParams, FilterValues, FilterValueType } from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Query } from '@elastic/eui';
// @ts-expect-error need nested import, as this is also used on the server-side.
// importing from eui entrypoint causes errors.
import { Query } from '@elastic/eui/lib/components/search_bar/query';
import { getSearchTerm, getFieldValueMap, applyAliases } from './query_utils';
import { FilterValues, SearchParams } from './types';
import { FilterValues, GlobalSearchProviderFindParams } from './types';

const knownFilters = ['tag', 'type'];

Expand All @@ -15,7 +17,7 @@ const aliasMap = {
type: ['types'],
};

export const parseSearchParams = (term: string): SearchParams => {
export const parseSearchParams = (term: string): GlobalSearchProviderFindParams => {
let query: Query;

try {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/global_search/common/search_syntax/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type FilterValueType = string | boolean | number;

export type FilterValues<ValueType extends FilterValueType = FilterValueType> = ValueType[];

export interface SearchParams {
export interface GlobalSearchProviderFindParams {
/**
* The parsed search term.
* Can be undefined if the query was only composed of field terms.
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/global_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import { Observable } from 'rxjs';
import { Serializable } from 'src/core/types';

export {
FilterValues,
GlobalSearchProviderFindParams,
FilterValueType,
} from './search_syntax/types';

/**
* Options provided to {@link GlobalSearchResultProvider | a result provider}'s `find` method.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { takeInArray } from '../../common/operators';
import { defaultMaxProviderResults } from '../../common/constants';
import { processProviderResult } from '../../common/process_result';
import { ILicenseChecker } from '../../common/license_checker';
import { parseSearchParams } from '../../common/search_syntax';
import { GlobalSearchResultProvider } from '../types';
import { GlobalSearchClientConfigType } from '../config';
import { GlobalSearchFindOptions } from './types';
Expand Down Expand Up @@ -147,8 +148,10 @@ export class SearchService {
aborted$,
});

const searchParams = parseSearchParams(term);

const providersResults$ = [...this.providers.values()].map((provider) =>
provider.find(term, providerOptions).pipe(
provider.find(searchParams, providerOptions).pipe(
takeInArray(this.maxProviderResults),
takeUntil(aborted$),
map((results) => results.map((r) => processResult(r)))
Expand Down
10 changes: 7 additions & 3 deletions x-pack/plugins/global_search/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/

import { Observable } from 'rxjs';
import { GlobalSearchProviderFindOptions, GlobalSearchProviderResult } from '../common/types';
import {
GlobalSearchProviderFindOptions,
GlobalSearchProviderResult,
GlobalSearchProviderFindParams,
} from '../common/types';
import { SearchServiceSetup, SearchServiceStart } from './services';

export type GlobalSearchPluginSetup = Pick<SearchServiceSetup, 'registerResultProvider'>;
Expand All @@ -29,15 +33,15 @@ export interface GlobalSearchResultProvider {
* // returning all results in a single batch
* setupDeps.globalSearch.registerResultProvider({
* id: 'my_provider',
* find: (term, { aborted$, preference, maxResults }, context) => {
* find: ({ term, filters }, { aborted$, preference, maxResults }, context) => {
* const resultPromise = myService.search(term, { preference, maxResults }, context.core.savedObjects.client);
* return from(resultPromise).pipe(takeUntil(aborted$));
* },
* });
* ```
*/
find(
term: string,
search: GlobalSearchProviderFindParams,
options: GlobalSearchProviderFindOptions
): Observable<GlobalSearchProviderResult[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GlobalSearchFindError } from '../../common/errors';
import { takeInArray } from '../../common/operators';
import { defaultMaxProviderResults } from '../../common/constants';
import { ILicenseChecker } from '../../common/license_checker';

import { parseSearchParams } from '../../common/search_syntax';
import { processProviderResult } from '../../common/process_result';
import { GlobalSearchConfigType } from '../config';
import { getContextFactory, GlobalSearchContextFactory } from './context';
Expand Down Expand Up @@ -137,7 +137,8 @@ export class SearchService {

const timeout$ = timer(this.config!.search_timeout.asMilliseconds()).pipe(map(mapToUndefined));
const aborted$ = options.aborted$ ? merge(options.aborted$, timeout$) : timeout$;
const providerOptions = {
const findParams = parseSearchParams(term);
const findOptions = {
...options,
preference: options.preference ?? 'default',
maxResults: this.maxProviderResults,
Expand All @@ -148,7 +149,7 @@ export class SearchService {
processProviderResult(result, basePath);

const providersResults$ = [...this.providers.values()].map((provider) =>
provider.find(term, providerOptions, context).pipe(
provider.find(findParams, findOptions, context).pipe(
takeInArray(this.maxProviderResults),
takeUntil(aborted$),
map((results) => results.map((r) => processResult(r)))
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/global_search/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
GlobalSearchBatchedResults,
GlobalSearchProviderFindOptions,
GlobalSearchProviderResult,
GlobalSearchProviderFindParams,
} from '../common/types';
import { SearchServiceSetup, SearchServiceStart } from './services';

Expand Down Expand Up @@ -97,15 +98,15 @@ export interface GlobalSearchResultProvider {
* // returning all results in a single batch
* setupDeps.globalSearch.registerResultProvider({
* id: 'my_provider',
* find: (term, { aborted$, preference, maxResults }, context) => {
* find: ({term, filters }, { aborted$, preference, maxResults }, context) => {
* const resultPromise = myService.search(term, { preference, maxResults }, context.core.savedObjects.client);
* return from(resultPromise).pipe(takeUntil(aborted$));
* },
* });
* ```
*/
find(
term: string,
search: GlobalSearchProviderFindParams,
options: GlobalSearchProviderFindOptions,
context: GlobalSearchProviderContext
): Observable<GlobalSearchProviderResult[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ export function SearchBar({
}

let arr: GlobalSearchResult[] = [];
if (searchValue.length !== 0) trackUiMetric(METRIC_TYPE.COUNT, 'search_request');
if (searchValue.length !== 0) {
trackUiMetric(METRIC_TYPE.COUNT, 'search_request');
}
searchSubscription.current = globalSearch(searchValue, {}).subscribe({
next: ({ results }) => {
if (searchValue.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { from } from 'rxjs';
import { from, of } from 'rxjs';
import { take, map, takeUntil, mergeMap, shareReplay } from 'rxjs/operators';
import { ApplicationStart } from 'src/core/public';
import { GlobalSearchResultProvider } from '../../../global_search/public';
Expand All @@ -26,12 +26,15 @@ export const createApplicationResultProvider = (

return {
id: 'application',
find: (term, { aborted$, maxResults }) => {
find: ({ term, filters }, { aborted$, maxResults }) => {
if (filters.types && !filters.types.includes('application')) {
return of([]);
}
return searchableApps$.pipe(
takeUntil(aborted$),
take(1),
map((apps) => {
const results = getAppResults(term, [...apps.values()]);
const results = getAppResults(term ?? '', [...apps.values()]);
return results.sort((a, b) => b.score - a.score).slice(0, maxResults);
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { mapToResults } from './map_object_to_result';
export const createSavedObjectsResultProvider = (): GlobalSearchResultProvider => {
return {
id: 'savedObjects',
find: (term, { aborted$, maxResults, preference }, { core }) => {
find: ({ term, filters }, { aborted$, maxResults, preference }, { core }) => {
if (!term) {
return of([]);
}
Expand All @@ -24,7 +24,9 @@ export const createSavedObjectsResultProvider = (): GlobalSearchResultProvider =

const searchableTypes = typeRegistry
.getVisibleTypes()
.filter(filters.types ? (type) => filters.types!.includes(type.name) : () => true)
.filter((type) => type.management?.defaultSearchField && type.management?.getInAppUrl);

const searchFields = uniq(
searchableTypes.map((type) => type.management!.defaultSearchField!)
);
Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/lens/public/search_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import levenshtein from 'js-levenshtein';
import { ApplicationStart } from 'kibana/public';
import { from } from 'rxjs';
import { from, of } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public';
import { GlobalSearchResultProvider } from '../../global_search/public';
Expand All @@ -26,7 +26,10 @@ export const getSearchProvider: (
uiCapabilities: Promise<ApplicationStart['capabilities']>
) => GlobalSearchResultProvider = (uiCapabilities) => ({
id: 'lens',
find: (term) => {
find: ({ term = '', filters }) => {
if (filters.types && !filters.types.includes('application')) {
return of([]);
}
return from(
uiCapabilities.then(({ navLinks: { visualize: visualizeNavLink } }) => {
if (!visualizeNavLink) {
Expand Down

0 comments on commit a12bbdb

Please sign in to comment.