diff --git a/src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts b/src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts index 4d9b381b1d97..35a5bdc7a693 100644 --- a/src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts +++ b/src/plugins/data/common/opensearch_query/opensearch_query/build_opensearch_query.ts @@ -73,10 +73,15 @@ export function buildOpenSearchQuery( return buildQueryFromSql(sqlQueries, config.dateFormatTZ); } - const validQueries = queries - .filter((query) => query.language !== 'SQL') - .filter((query) => has(query, 'query')); + const validQueries = queries.filter((query) => has(query, 'query')); const queriesByLanguage = groupBy(validQueries, 'language'); + const unsupportedQueries = Object.keys(queriesByLanguage).filter( + (language) => language !== 'kuery' && language !== 'lucene' + ); + if (unsupportedQueries.length > 0) { + return queries; + } + const kueryQuery = buildQueryFromKuery( indexPattern, queriesByLanguage.kuery, diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index c73e7881faa6..354b9507ceeb 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -74,6 +74,7 @@ export class SearchService implements Plugin { private readonly aggsService = new AggsService(); private readonly searchSourceService = new SearchSourceService(); private searchInterceptor!: ISearchInterceptor; + private defaultSearchInterceptor!: ISearchInterceptor; private usageCollector?: SearchUsageCollector; constructor(private initializerContext: PluginInitializerContext) {} @@ -95,6 +96,7 @@ export class SearchService implements Plugin { startServices: getStartServices(), usageCollector: this.usageCollector!, }); + this.defaultSearchInterceptor = this.searchInterceptor; expressions.registerFunction(opensearchdsl); expressions.registerType(opensearchRawResponse); @@ -154,6 +156,10 @@ export class SearchService implements Plugin { this.searchInterceptor.showError(e); }, searchSource: this.searchSourceService.start(indexPatterns, searchSourceDependencies), + __enhance: (enhancements: SearchEnhancements) => { + this.searchInterceptor = enhancements.searchInterceptor; + }, + getDefaultSearchInterceptor: () => this.defaultSearchInterceptor, }; } diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index 8a0d82b855c8..679c6624bad4 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -78,6 +78,8 @@ export interface ISearchStart { * {@link ISearchStartSearchSource} */ searchSource: ISearchStartSearchSource; + __enhance: (enhancements: SearchEnhancements) => void; + getDefaultSearchInterceptor: () => ISearchInterceptor; } export { SEARCH_EVENT_TYPE } from './collectors'; diff --git a/src/plugins/data/public/ui/query_string_input/language_switcher.tsx b/src/plugins/data/public/ui/query_string_input/language_switcher.tsx index aca6a3c9f667..b6eabe7fb04e 100644 --- a/src/plugins/data/public/ui/query_string_input/language_switcher.tsx +++ b/src/plugins/data/public/ui/query_string_input/language_switcher.tsx @@ -31,7 +31,7 @@ import { EuiComboBox, EuiComboBoxOptionOption, PopoverAnchorPosition } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import React from 'react'; -import { getUiService } from '../../services'; +import { getSearchService, getUiService } from '../../services'; interface Props { language: string; @@ -39,11 +39,11 @@ interface Props { anchorPosition?: PopoverAnchorPosition; } -function mapExternalLanguagesToOptions(externalLanguages: string[]) { - return externalLanguages.map((language) => ({ +function mapExternalLanguageToOptions(language: string) { + return { label: language, value: language, - })); + }; } export function QueryLanguageSwitcher(props: Props) { @@ -74,10 +74,10 @@ export function QueryLanguageSwitcher(props: Props) { }, ]; - const externalLanguages = getUiService().externalLanguages; - if (externalLanguages.length > 0) { - languageOptions.push(...mapExternalLanguagesToOptions(externalLanguages)); - } + const queryEnhancements = getUiService().queryEnhancements; + queryEnhancements.forEach((enhancement) => + languageOptions.push(mapExternalLanguageToOptions(enhancement.language)) + ); const selectedLanguage = { label: props.language === 'kuery' ? 'DQL' : props.language, @@ -86,6 +86,12 @@ export function QueryLanguageSwitcher(props: Props) { const handleLanguageChange = (newLanguage: EuiComboBoxOptionOption[]) => { const queryLanguage = newLanguage[0].label === 'DQL' ? 'kuery' : newLanguage[0].label; props.onSelectLanguage(queryLanguage); + const queryEnhancement = queryEnhancements.get(queryLanguage); + getSearchService().__enhance({ + searchInterceptor: queryEnhancement + ? queryEnhancement.search + : getSearchService().getDefaultSearchInterceptor(), + }); }; return ( diff --git a/src/plugins/data/public/ui/types.ts b/src/plugins/data/public/ui/types.ts index 8c1272072fd4..e76374c3df38 100644 --- a/src/plugins/data/public/ui/types.ts +++ b/src/plugins/data/public/ui/types.ts @@ -9,18 +9,24 @@ * GitHub history for details. */ +import { SearchInterceptor } from '../search'; import { IndexPatternSelectProps } from './index_pattern_select'; import { StatefulSearchBarProps } from './search_bar'; +export interface QueryEnhancement { + language: string; + search: SearchInterceptor; +} + export interface UiEnhancements { - externalLanguage: string; + query: QueryEnhancement; } /** * Data plugin prewired UI components */ export interface DataPublicPluginStartUi { - externalLanguages: string[]; + queryEnhancements: Map; IndexPatternSelect: React.ComponentType; SearchBar: React.ComponentType; } diff --git a/src/plugins/data/public/ui/ui_service.ts b/src/plugins/data/public/ui/ui_service.ts index 7cbf9fe78be5..b4877c604753 100644 --- a/src/plugins/data/public/ui/ui_service.ts +++ b/src/plugins/data/public/ui/ui_service.ts @@ -10,7 +10,7 @@ */ import { Plugin, CoreSetup, CoreStart, PluginInitializerContext } from 'src/core/public'; -import { DataPublicPluginStartUi, UiEnhancements } from './types'; +import { DataPublicPluginStartUi, QueryEnhancement, UiEnhancements } from './types'; import { ConfigSchema } from '../../config'; import { createIndexPatternSelect } from './index_pattern_select'; @@ -30,7 +30,7 @@ export interface UiServiceStartDependencies { export class UiService implements Plugin { constructor(private _: PluginInitializerContext) {} - private externalLanguages: string[] = []; + private queryEnhancements: Map = new Map(); public setup( { http, getStartServices, notifications, uiSettings }: CoreSetup, @@ -51,7 +51,7 @@ export class UiService implements Plugin { return { __enhance: (enhancements?: UiEnhancements) => { if (!enhancements) return; - this.externalLanguages.push(enhancements.externalLanguage); + this.queryEnhancements.set(enhancements.query.language, enhancements.query); }, }; } @@ -64,7 +64,7 @@ export class UiService implements Plugin { }); return { - externalLanguages: this.externalLanguages, + queryEnhancements: this.queryEnhancements, IndexPatternSelect: createIndexPatternSelect(core.savedObjects.client), SearchBar, };