Skip to content

Commit

Permalink
[APM] Kuery bar gives invalid suggestions (#105132)
Browse files Browse the repository at this point in the history
* adding method to support terms_agg

* adding more filters to kuery bar

* addressing PR comments

* fixing docs

* addressing PR comments

* moving file to common

* addressing PR comments

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
cauemarcondes and kibanamachine authored Jul 14, 2021
1 parent 08ae971 commit 94e379f
Show file tree
Hide file tree
Showing 71 changed files with 165 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface QuerySuggestionGetFnArgs
| [boolFilter](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.boolfilter.md) | <code>any</code> | |
| [indexPatterns](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.indexpatterns.md) | <code>IIndexPattern[]</code> | |
| [language](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.language.md) | <code>string</code> | |
| [method](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.method.md) | <code>ValueSuggestionsMethod</code> | |
| [query](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.query.md) | <code>string</code> | |
| [selectionEnd](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.selectionend.md) | <code>number</code> | |
| [selectionStart](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.selectionstart.md) | <code>number</code> | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [QuerySuggestionGetFnArgs](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.md) &gt; [method](./kibana-plugin-plugins-data-public.querysuggestiongetfnargs.method.md)

## QuerySuggestionGetFnArgs.method property

<b>Signature:</b>

```typescript
method?: ValueSuggestionsMethod;
```
2 changes: 2 additions & 0 deletions src/plugins/data/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const KIBANA_USER_QUERY_LANGUAGE_KEY = 'kibana.userQueryLanguage';
/** @public **/
export const INDEX_PATTERN_SAVED_OBJECT_TYPE = 'index-pattern';

export type ValueSuggestionsMethod = 'terms_enum' | 'terms_agg';

export const UI_SETTINGS = {
META_FIELDS: 'metaFields',
DOC_HIGHLIGHT: 'doc_table:highlight',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const setupGetValueSuggestions: KqlQuerySuggestionProvider = (
.getStartServices()
.then(([_, __, dataStart]) => dataStart.autocomplete);
return async (
{ indexPatterns, boolFilter, useTimeRange, signal },
{ indexPatterns, boolFilter, useTimeRange, signal, method },
{ start, end, prefix, suffix, fieldName, nestedPath }
): Promise<QuerySuggestion[]> => {
const fullFieldName = nestedPath ? `${nestedPath}.${fieldName}` : fieldName;
Expand All @@ -59,6 +59,7 @@ export const setupGetValueSuggestions: KqlQuerySuggestionProvider = (
boolFilter,
useTimeRange,
signal,
method,
}).then((valueSuggestions) => {
const quotedValues = valueSuggestions.map((value) =>
typeof value === 'string' ? `"${escapeQuotes(value)}"` : `${value}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { ValueSuggestionsMethod } from '../../../common';
import { IFieldType, IIndexPattern } from '../../../common/index_patterns';

export enum QuerySuggestionTypes {
Expand All @@ -30,6 +31,7 @@ export interface QuerySuggestionGetFnArgs {
signal?: AbortSignal;
useTimeRange?: boolean;
boolFilter?: any;
method?: ValueSuggestionsMethod;
}

/** @public **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
import dateMath from '@elastic/datemath';
import { memoize } from 'lodash';
import { CoreSetup } from 'src/core/public';
import { IIndexPattern, IFieldType, UI_SETTINGS, buildQueryFromFilters } from '../../../common';
import {
IIndexPattern,
IFieldType,
UI_SETTINGS,
buildQueryFromFilters,
ValueSuggestionsMethod,
} from '../../../common';
import { TimefilterSetup } from '../../query';
import { AutocompleteUsageCollector } from '../collectors';

Expand All @@ -22,6 +28,7 @@ interface ValueSuggestionsGetFnArgs {
useTimeRange?: boolean;
boolFilter?: any[];
signal?: AbortSignal;
method?: ValueSuggestionsMethod;
}

const getAutocompleteTimefilter = (
Expand Down Expand Up @@ -54,12 +61,25 @@ export const setupValueSuggestionProvider = (
}

const requestSuggestions = memoize(
(index: string, field: IFieldType, query: string, filters: any = [], signal?: AbortSignal) => {
(
index: string,
field: IFieldType,
query: string,
filters: any = [],
signal?: AbortSignal,
method?: ValueSuggestionsMethod
) => {
usageCollector?.trackRequest();
return core.http
.fetch(`/api/kibana/suggestions/values/${index}`, {
method: 'POST',
body: JSON.stringify({ query, field: field.name, fieldMeta: field?.toSpec?.(), filters }),
body: JSON.stringify({
query,
field: field.name,
fieldMeta: field?.toSpec?.(),
filters,
method,
}),
signal,
})
.then((r) => {
Expand All @@ -77,6 +97,7 @@ export const setupValueSuggestionProvider = (
useTimeRange,
boolFilter,
signal,
method,
}: ValueSuggestionsGetFnArgs): Promise<any[]> => {
const shouldSuggestValues = core!.uiSettings.get<boolean>(
UI_SETTINGS.FILTERS_EDITOR_SUGGEST_VALUES
Expand All @@ -98,7 +119,7 @@ export const setupValueSuggestionProvider = (
const filters = [...(boolFilter ? boolFilter : []), ...filterQuery];
try {
usageCollector?.trackCall();
return await requestSuggestions(title, field, query, filters, signal);
return await requestSuggestions(title, field, query, filters, signal, method);
} catch (e) {
if (!signal?.aborted) {
usageCollector?.trackError();
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,10 @@ export interface QuerySuggestionGetFnArgs {
indexPatterns: IIndexPattern[];
// (undocumented)
language: string;
// Warning: (ae-forgotten-export) The symbol "ValueSuggestionsMethod" needs to be exported by the entry point index.d.ts
//
// (undocumented)
method?: ValueSuggestionsMethod;
// (undocumented)
query: string;
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ export function registerValueSuggestionsRoute(router: IRouter, config$: Observab
query: schema.string(),
filters: schema.maybe(schema.any()),
fieldMeta: schema.maybe(schema.any()),
method: schema.maybe(
schema.oneOf([schema.literal('terms_agg'), schema.literal('terms_enum')])
),
},
{ unknowns: 'allow' }
),
},
},
async (context, request, response) => {
const config = await config$.pipe(first()).toPromise();
const { field: fieldName, query, filters, fieldMeta } = request.body;
const { field: fieldName, query, filters, fieldMeta, method } = request.body;
const { index } = request.params;
const abortSignal = getRequestAbortedSignal(request.events.aborted$);

try {
const fn =
config.autocomplete.valueSuggestions.method === 'terms_enum'
(method ?? config.autocomplete.valueSuggestions.method) === 'terms_enum'
? termsEnumSuggestions
: termsAggSuggestions;
const body = await fn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { SERVICE_ENVIRONMENT } from '../../common/elasticsearch_fieldnames';
import { ENVIRONMENT_NOT_DEFINED } from '../../common/environment_filter_values';
import { environmentQuery } from './queries';
import { SERVICE_ENVIRONMENT } from '../elasticsearch_fieldnames';
import { ENVIRONMENT_NOT_DEFINED } from '../environment_filter_values';
import { environmentQuery } from './environment_query';

describe('environmentQuery', () => {
describe('when environment is undefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
* 2.0.
*/

import { ESFilter } from '../../../../../src/core/types/elasticsearch';
import { SERVICE_ENVIRONMENT } from '../../common/elasticsearch_fieldnames';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/api/types';
import { SERVICE_ENVIRONMENT } from '../elasticsearch_fieldnames';
import {
ENVIRONMENT_ALL,
ENVIRONMENT_NOT_DEFINED,
} from '../../common/environment_filter_values';
export { kqlQuery, rangeQuery } from '../../../observability/server';

type QueryDslQueryContainer = ESFilter;
} from '../environment_filter_values';

export function environmentQuery(
environment?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TRANSACTION_TYPE,
} from '../../../../common/elasticsearch_fieldnames';
import { UIProcessorEvent } from '../../../../common/processor_event';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { IUrlParams } from '../../../context/url_params_context/types';

export function getBoolFilter({
Expand All @@ -35,6 +36,14 @@ export function getBoolFilter({
});
}

boolFilter.push(...environmentQuery(urlParams.environment));

if (urlParams.transactionType) {
boolFilter.push({
term: { [TRANSACTION_TYPE]: urlParams.transactionType },
});
}

switch (processorEvent) {
case 'transaction':
boolFilter.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function KueryBar(props: { prepend?: React.ReactNode | string }) {
selectionStart,
selectionEnd: selectionStart,
useTimeRange: true,
method: 'terms_agg',
})) || []
)
.filter((suggestion) => !startsWith(suggestion.text, 'span.'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
TRANSACTION_TYPE,
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import { environmentQuery, rangeQuery } from '../../../../server/utils/queries';
import { rangeQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { AlertParams } from '../../../routes/alerts/chart_preview';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import { SERVICE_NAME } from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import { AlertParams } from '../../../routes/alerts/chart_preview';
import { environmentQuery, rangeQuery } from '../../../../server/utils/queries';
import { rangeQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import { AlertParams } from '../../../routes/alerts/chart_preview';
import { environmentQuery, rangeQuery } from '../../../../server/utils/queries';
import { rangeQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { getBucketSize } from '../../helpers/get_bucket_size';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
SERVICE_NAME,
} from '../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../common/processor_event';
import { environmentQuery } from '../../../server/utils/queries';
import { environmentQuery } from '../../../common/utils/environment_query';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../common/processor_event';
import { getDurationFormatter } from '../../../common/utils/formatters';
import { environmentQuery } from '../../../server/utils/queries';
import { environmentQuery } from '../../../common/utils/environment_query';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { EventOutcome } from '../../../common/event_outcome';
import { ProcessorEvent } from '../../../common/processor_event';
import { asDecimalOrInteger } from '../../../common/utils/formatters';
import { environmentQuery } from '../../../server/utils/queries';
import { environmentQuery } from '../../../common/utils/environment_query';
import { getApmIndices } from '../settings/apm_indices/get_apm_indices';
import { apmActionVariables } from './action_variables';
import { alertingEsClient } from './alerting_es_client';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { snakeCase } from 'lodash';
import Boom from '@hapi/boom';
import { ML_ERRORS } from '../../../common/anomaly_detection';
import { ProcessorEvent } from '../../../common/processor_event';
import { environmentQuery } from '../../../server/utils/queries';
import { environmentQuery } from '../../../common/utils/environment_query';
import { Setup } from '../helpers/setup_request';
import {
TRANSACTION_DURATION,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/apm/server/lib/correlations/get_filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { ESFilter } from '../../../../../../src/core/types/elasticsearch';
import { environmentQuery, rangeQuery, kqlQuery } from '../../utils/queries';
import { rangeQuery, kqlQuery } from '../../../../observability/server';
import { environmentQuery } from '../../../common/utils/environment_query';
import {
SERVICE_NAME,
TRANSACTION_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../../common/elasticsearch_fieldnames';
import { ENVIRONMENT_NOT_DEFINED } from '../../../common/environment_filter_values';
import { ProcessorEvent } from '../../../common/processor_event';
import { rangeQuery } from '../../../server/utils/queries';
import { rangeQuery } from '../../../../observability/server';
import { getProcessorEventForAggregatedTransactions } from '../helpers/aggregated_transactions';
import { Setup, SetupTimeRange } from '../helpers/setup_request';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import {
SERVICE_NAME,
} from '../../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../../common/processor_event';
import {
environmentQuery,
rangeQuery,
kqlQuery,
} from '../../../../server/utils/queries';
import { rangeQuery, kqlQuery } from '../../../../../observability/server';
import { environmentQuery } from '../../../../common/utils/environment_query';
import { Setup, SetupTimeRange } from '../../helpers/setup_request';

export async function getBuckets({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import {
TRANSACTION_SAMPLED,
} from '../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../common/processor_event';
import {
environmentQuery,
rangeQuery,
kqlQuery,
} from '../../../server/utils/queries';
import { rangeQuery, kqlQuery } from '../../../../observability/server';
import { environmentQuery } from '../../../common/utils/environment_query';
import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { getTransaction } from '../transactions/get_transaction';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { SearchAggregatedTransactionSetting } from '../../../../common/aggregated_transactions';
import { kqlQuery, rangeQuery } from '../../../../server/utils/queries';
import { kqlQuery, rangeQuery } from '../../../../../observability/server';
import { ProcessorEvent } from '../../../../common/processor_event';
import {
TRANSACTION_DURATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { ProcessorEvent } from '../../../common/processor_event';
import { rangeQuery } from '../../../server/utils/queries';
import { rangeQuery } from '../../../../observability/server';
import { SERVICE_NAME } from '../../../common/elasticsearch_fieldnames';
import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { getProcessorEventForAggregatedTransactions } from '../helpers/aggregated_transactions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TRANSACTION_REQUEST,
} from '../../../common/transaction_types';
import { TRANSACTION_TYPE } from '../../../common/elasticsearch_fieldnames';
import { rangeQuery } from '../../../server/utils/queries';
import { rangeQuery } from '../../../../observability/server';
import { Setup, SetupTimeRange } from '../helpers/setup_request';
import { getProcessorEventForAggregatedTransactions } from '../helpers/aggregated_transactions';
import { calculateThroughput } from '../helpers/calculate_throughput';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/server/lib/rum_client/has_rum_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TRANSACTION_TYPE,
} from '../../../common/elasticsearch_fieldnames';
import { ProcessorEvent } from '../../../common/processor_event';
import { rangeQuery } from '../../../server/utils/queries';
import { rangeQuery } from '../../../../observability/server';
import { TRANSACTION_PAGE_LOAD } from '../../../common/transaction_types';

export async function hasRumData({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../../../common/ux_ui_filter';
import { ESFilter } from '../../../../../../../src/core/types/elasticsearch';
import { UxUIFilters } from '../../../../typings/ui_filters';
import { environmentQuery } from '../../../utils/queries';
import { environmentQuery } from '../../../../common/utils/environment_query';

export function getEsFilter(uiFilters: UxUIFilters, exclude?: boolean) {
const localFilterValues = uiFilters;
Expand Down
Loading

0 comments on commit 94e379f

Please sign in to comment.