-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
513 additions
and
4 deletions.
There are no files selected for viewing
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
89 changes: 89 additions & 0 deletions
89
src/plugins/data/common/search/aggs/buckets/significant_text.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,89 @@ | ||
/* | ||
* 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 { AggConfigs } from '../agg_configs'; | ||
import { mockAggTypesRegistry } from '../test_helpers'; | ||
import { BUCKET_TYPES } from './bucket_agg_types'; | ||
|
||
describe('Significant Text Agg', () => { | ||
const getAggConfigs = (params: Record<string, any> = {}) => { | ||
const indexPattern = { | ||
id: '1234', | ||
title: 'logstash-*', | ||
fields: { | ||
getByName: () => field, | ||
filter: () => [field], | ||
}, | ||
} as any; | ||
|
||
const field = { | ||
name: 'field', | ||
indexPattern, | ||
}; | ||
|
||
return new AggConfigs( | ||
indexPattern, | ||
[ | ||
{ | ||
id: 'test', | ||
type: BUCKET_TYPES.SIGNIFICANT_TEXT, | ||
params, | ||
}, | ||
], | ||
{ | ||
typesRegistry: mockAggTypesRegistry(), | ||
} | ||
); | ||
}; | ||
|
||
test('produces the expected expression ast', () => { | ||
const aggConfigs = getAggConfigs({ | ||
size: 'SIZE', | ||
field: { | ||
name: 'FIELD', | ||
}, | ||
}); | ||
expect(aggConfigs.aggs[0].toExpressionAst()).toMatchInlineSnapshot(` | ||
Object { | ||
"chain": Array [ | ||
Object { | ||
"arguments": Object { | ||
"enabled": Array [ | ||
true, | ||
], | ||
"field": Array [ | ||
"FIELD", | ||
], | ||
"id": Array [ | ||
"test", | ||
], | ||
"size": Array [ | ||
"SIZE", | ||
], | ||
}, | ||
"function": "aggSignificantText", | ||
"type": "function", | ||
}, | ||
], | ||
"type": "expression", | ||
} | ||
`); | ||
}); | ||
|
||
test('should generate correct label', () => { | ||
const aggConfigs = getAggConfigs({ | ||
size: 'SIZE', | ||
field: { | ||
name: 'FIELD', | ||
}, | ||
}); | ||
const label = aggConfigs.aggs[0].makeLabel(); | ||
|
||
expect(label).toBe('Top SIZE unusual terms from "FIELD" text'); | ||
}); | ||
}); |
90 changes: 90 additions & 0 deletions
90
src/plugins/data/common/search/aggs/buckets/significant_text.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,90 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { BucketAggType } from './bucket_agg_type'; | ||
import { migrateIncludeExcludeFormat } from './migrate_include_exclude_format'; | ||
import { BUCKET_TYPES } from './bucket_agg_types'; | ||
import { aggSignificantTextFnName } from './significant_text_fn'; | ||
import { KBN_FIELD_TYPES, ES_FIELD_TYPES } from '../../../../common'; | ||
import { BaseAggParams } from '../types'; | ||
import { createFilterTerms } from './create_filter/terms'; | ||
|
||
const significantTextTitle = i18n.translate('data.search.aggs.buckets.significantTextTitle', { | ||
defaultMessage: 'Significant Text', | ||
}); | ||
|
||
export interface AggParamsSignificantText extends BaseAggParams { | ||
field: string; | ||
size?: number; | ||
min_doc_count?: number; | ||
filter_duplicate_text?: boolean; | ||
exclude?: string; | ||
include?: string; | ||
} | ||
|
||
export const getSignificantTextBucketAgg = () => | ||
new BucketAggType({ | ||
name: BUCKET_TYPES.SIGNIFICANT_TEXT, | ||
expressionName: aggSignificantTextFnName, | ||
title: significantTextTitle, | ||
makeLabel(aggConfig) { | ||
return i18n.translate('data.search.aggs.buckets.significantTextLabel', { | ||
defaultMessage: 'Top {size} unusual terms from "{fieldName}" text', | ||
values: { | ||
size: aggConfig.params.size, | ||
fieldName: aggConfig.getFieldDisplayName(), | ||
}, | ||
}); | ||
}, | ||
createFilter: createFilterTerms, | ||
params: [ | ||
{ | ||
name: 'field', | ||
type: 'field', | ||
/** | ||
* Significant text is available only for ES_FIELD_TYPES.TEXT, | ||
* This information is not available from field.type, so we have to check this using underlying esTypes | ||
*/ | ||
filterField: (field) => | ||
Boolean( | ||
field.type === KBN_FIELD_TYPES.STRING && field.esTypes?.includes(ES_FIELD_TYPES.TEXT) | ||
), | ||
}, | ||
{ | ||
name: 'size', | ||
type: 'number', | ||
}, | ||
{ | ||
name: 'min_doc_count', | ||
type: 'number', | ||
}, | ||
{ | ||
name: 'filter_duplicate_text', | ||
type: 'boolean', | ||
}, | ||
{ | ||
name: 'exclude', | ||
displayName: i18n.translate('data.search.aggs.buckets.significantText.excludeLabel', { | ||
defaultMessage: 'Exclude', | ||
}), | ||
type: 'string', | ||
advanced: true, | ||
...migrateIncludeExcludeFormat, | ||
}, | ||
{ | ||
name: 'include', | ||
displayName: i18n.translate('data.search.aggs.buckets.significantText.includeLabel', { | ||
defaultMessage: 'Include', | ||
}), | ||
type: 'string', | ||
advanced: true, | ||
...migrateIncludeExcludeFormat, | ||
}, | ||
], | ||
}); |
85 changes: 85 additions & 0 deletions
85
src/plugins/data/common/search/aggs/buckets/significant_text_fn.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,85 @@ | ||
/* | ||
* 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 { functionWrapper } from '../test_helpers'; | ||
import { aggSignificantText } from './significant_text_fn'; | ||
|
||
describe('agg_expression_functions', () => { | ||
describe('aggSignificantText', () => { | ||
const fn = functionWrapper(aggSignificantText()); | ||
|
||
test('fills in defaults when only required args are provided', () => { | ||
const actual = fn({ | ||
field: 'machine.os', | ||
}); | ||
expect(actual).toMatchInlineSnapshot(` | ||
Object { | ||
"type": "agg_type", | ||
"value": Object { | ||
"enabled": true, | ||
"id": undefined, | ||
"params": Object { | ||
"customLabel": undefined, | ||
"exclude": undefined, | ||
"field": "machine.os", | ||
"filter_duplicate_text": undefined, | ||
"include": undefined, | ||
"json": undefined, | ||
"min_doc_count": undefined, | ||
"size": undefined, | ||
}, | ||
"schema": undefined, | ||
"type": "significant_text", | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('includes optional params when they are provided', () => { | ||
const actual = fn({ | ||
id: '1', | ||
enabled: false, | ||
schema: 'whatever', | ||
field: 'machine.os', | ||
size: 6, | ||
include: 'win', | ||
exclude: 'ios', | ||
filter_duplicate_text: true, | ||
min_doc_count: 10, | ||
}); | ||
|
||
expect(actual.value).toMatchInlineSnapshot(` | ||
Object { | ||
"enabled": false, | ||
"id": "1", | ||
"params": Object { | ||
"customLabel": undefined, | ||
"exclude": "ios", | ||
"field": "machine.os", | ||
"filter_duplicate_text": true, | ||
"include": "win", | ||
"json": undefined, | ||
"min_doc_count": 10, | ||
"size": 6, | ||
}, | ||
"schema": "whatever", | ||
"type": "significant_text", | ||
} | ||
`); | ||
}); | ||
|
||
test('correctly parses json string argument', () => { | ||
const actual = fn({ | ||
field: 'machine.os', | ||
json: '{ "foo": true }', | ||
}); | ||
|
||
expect(actual.value.params.json).toEqual('{ "foo": true }'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.