-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
terms.ts
182 lines (174 loc) · 5.53 KB
/
terms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { noop } from 'lodash';
import { i18n } from '@kbn/i18n';
import { BucketAggType } from './bucket_agg_type';
import { BUCKET_TYPES } from './bucket_agg_types';
import { createFilterTerms } from './create_filter/terms';
import {
isStringOrNumberType,
migrateIncludeExcludeFormat,
} from './migrate_include_exclude_format';
import { aggTermsFnName } from './terms_fn';
import { AggConfigSerialized, BaseAggParams } from '../types';
import { KBN_FIELD_TYPES } from '../../../../common';
import {
createOtherBucketPostFlightRequest,
constructSingleTermOtherFilter,
} from './_terms_other_bucket_helper';
import { termsOrderAggParamDefinition } from './_terms_order_helper';
export { termsAggFilter } from './_terms_order_helper';
const termsTitle = i18n.translate('data.search.aggs.buckets.termsTitle', {
defaultMessage: 'Terms',
});
export interface AggParamsTerms extends BaseAggParams {
field: string;
orderBy: string;
orderAgg?: AggConfigSerialized;
order?: 'asc' | 'desc';
size?: number;
shardSize?: number;
missingBucket?: boolean;
missingBucketLabel?: string;
otherBucket?: boolean;
otherBucketLabel?: string;
// advanced
exclude?: string;
include?: string;
}
export const getTermsBucketAgg = () =>
new BucketAggType({
name: BUCKET_TYPES.TERMS,
expressionName: aggTermsFnName,
title: termsTitle,
makeLabel(agg) {
const params = agg.params;
return agg.getFieldDisplayName() + ': ' + params.order.text;
},
getSerializedFormat(agg) {
const format = agg.params.field
? agg.aggConfigs.indexPattern.getFormatterForField(agg.params.field).toJSON()
: { id: undefined, params: undefined };
return {
id: 'terms',
params: {
id: format.id,
otherBucketLabel: agg.params.otherBucketLabel,
missingBucketLabel: agg.params.missingBucketLabel,
...format.params,
},
};
},
createFilter: createFilterTerms,
postFlightRequest: createOtherBucketPostFlightRequest(constructSingleTermOtherFilter),
hasPrecisionError: (aggBucket) => Boolean(aggBucket?.doc_count_error_upper_bound),
params: [
{
name: 'field',
type: 'field',
filterFieldTypes: [
KBN_FIELD_TYPES.NUMBER,
KBN_FIELD_TYPES.BOOLEAN,
KBN_FIELD_TYPES.DATE,
KBN_FIELD_TYPES.IP,
KBN_FIELD_TYPES.STRING,
],
},
{
name: 'orderBy',
write: noop, // prevent default write, it's handled by orderAgg
},
termsOrderAggParamDefinition,
{
name: 'order',
type: 'optioned',
default: 'desc',
options: [
{
text: i18n.translate('data.search.aggs.buckets.terms.orderDescendingTitle', {
defaultMessage: 'Descending',
}),
value: 'desc',
},
{
text: i18n.translate('data.search.aggs.buckets.terms.orderAscendingTitle', {
defaultMessage: 'Ascending',
}),
value: 'asc',
},
],
write: noop, // prevent default write, it's handled by orderAgg
},
{
name: 'size',
default: 5,
},
{
name: 'shardSize',
write: (aggConfig, output) => {
output.params.shard_size = aggConfig.params.shardSize;
},
},
{
name: 'otherBucket',
default: false,
write: noop,
},
{
name: 'otherBucketLabel',
type: 'string',
default: i18n.translate('data.search.aggs.buckets.terms.otherBucketLabel', {
defaultMessage: 'Other',
}),
displayName: i18n.translate('data.search.aggs.otherBucket.labelForOtherBucketLabel', {
defaultMessage: 'Label for other bucket',
}),
shouldShow: (agg) => agg.getParam('otherBucket'),
write: noop,
},
{
name: 'missingBucket',
default: false,
write: noop,
},
{
name: 'missingBucketLabel',
default: i18n.translate('data.search.aggs.buckets.terms.missingBucketLabel', {
defaultMessage: 'Missing',
description: `Default label used in charts when documents are missing a field.
Visible when you create a chart with a terms aggregation and enable "Show missing values"`,
}),
type: 'string',
displayName: i18n.translate('data.search.aggs.otherBucket.labelForMissingValuesLabel', {
defaultMessage: 'Label for missing values',
}),
shouldShow: (agg) => agg.getParam('missingBucket'),
write: noop,
},
{
name: 'exclude',
displayName: i18n.translate('data.search.aggs.buckets.terms.excludeLabel', {
defaultMessage: 'Exclude',
}),
type: 'string',
advanced: true,
shouldShow: isStringOrNumberType,
...migrateIncludeExcludeFormat,
},
{
name: 'include',
displayName: i18n.translate('data.search.aggs.buckets.terms.includeLabel', {
defaultMessage: 'Include',
}),
type: 'string',
advanced: true,
shouldShow: isStringOrNumberType,
...migrateIncludeExcludeFormat,
},
],
});