Skip to content

Commit

Permalink
Fix other bucket for fields containing dashes (#81981)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Tim Roes and kibanamachine authored Nov 2, 2020
1 parent 5bf1a36 commit 8a1a3d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
buildOtherBucketAgg,
mergeOtherBucketAggResponse,
updateMissingBucket,
OTHER_BUCKET_SEPARATOR as SEP,
} from './_terms_other_bucket_helper';
import { AggConfigs, CreateAggConfigParams } from '../agg_configs';
import { BUCKET_TYPES } from './bucket_agg_types';
Expand Down Expand Up @@ -145,7 +146,7 @@ const nestedTermResponse = {
{ key: '__missing__', doc_count: 1430 },
],
},
key: 'US',
key: 'US-with-dash',
doc_count: 2850,
},
{
Expand All @@ -158,7 +159,7 @@ const nestedTermResponse = {
{ key: '__missing__', doc_count: 130 },
],
},
key: 'IN',
key: 'IN-with-dash',
doc_count: 2830,
},
],
Expand Down Expand Up @@ -211,7 +212,10 @@ const nestedOtherResponse = {
hits: { total: 14005, max_score: 0, hits: [] },
aggregations: {
'other-filter': {
buckets: { '-US': { doc_count: 2805 }, '-IN': { doc_count: 2804 } },
buckets: {
[`${SEP}US-with-dash`]: { doc_count: 2805 },
[`${SEP}IN-with-dash`]: { doc_count: 2804 },
},
},
},
status: 200,
Expand Down Expand Up @@ -278,11 +282,11 @@ describe('Terms Agg Other bucket helper', () => {
aggs: undefined,
filters: {
filters: {
'-IN': {
[`${SEP}IN-with-dash`]: {
bool: {
must: [],
filter: [
{ match_phrase: { 'geo.src': 'IN' } },
{ match_phrase: { 'geo.src': 'IN-with-dash' } },
{ exists: { field: 'machine.os.raw' } },
],
should: [],
Expand All @@ -292,11 +296,11 @@ describe('Terms Agg Other bucket helper', () => {
],
},
},
'-US': {
[`${SEP}US-with-dash`]: {
bool: {
must: [],
filter: [
{ match_phrase: { 'geo.src': 'US' } },
{ match_phrase: { 'geo.src': 'US-with-dash' } },
{ exists: { field: 'machine.os.raw' } },
],
should: [],
Expand Down Expand Up @@ -329,10 +333,10 @@ describe('Terms Agg Other bucket helper', () => {
aggs: undefined,
filters: {
filters: {
'-IN': {
[`${SEP}IN-with-dash`]: {
bool: {
must: [],
filter: [{ match_phrase: { 'geo.src': 'IN' } }],
filter: [{ match_phrase: { 'geo.src': 'IN-with-dash' } }],
should: [],
must_not: [
{
Expand All @@ -356,10 +360,10 @@ describe('Terms Agg Other bucket helper', () => {
],
},
},
'-US': {
[`${SEP}US-with-dash`]: {
bool: {
must: [],
filter: [{ match_phrase: { 'geo.src': 'US' } }],
filter: [{ match_phrase: { 'geo.src': 'US-with-dash' } }],
should: [],
must_not: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { AggGroupNames } from '../agg_groups';
import { IAggConfigs } from '../agg_configs';
import { IBucketAggConfig } from './bucket_agg_type';

export const OTHER_BUCKET_SEPARATOR = '╰┄►';

/**
* walks the aggregation DSL and returns DSL starting at aggregation with id of startFromAggId
* @param aggNestedDsl: aggregation config DSL (top level)
Expand Down Expand Up @@ -55,7 +57,7 @@ const getAggResultBuckets = (
aggWithOtherBucket: IBucketAggConfig,
key: string
) => {
const keyParts = key.split('-');
const keyParts = key.split(OTHER_BUCKET_SEPARATOR);
let responseAgg = response;
for (const i in keyParts) {
if (keyParts[i]) {
Expand Down Expand Up @@ -196,7 +198,7 @@ export const buildOtherBucketAgg = (
bucket,
newAgg.id,
newFilters,
`${key}-${bucketKey.toString()}`
`${key}${OTHER_BUCKET_SEPARATOR}${bucketKey.toString()}`
);
});
return;
Expand Down Expand Up @@ -252,7 +254,7 @@ export const mergeOtherBucketAggResponse = (
const updatedResponse = cloneDeep(response);
each(otherResponse.aggregations['other-filter'].buckets, (bucket, key) => {
if (!bucket.doc_count || key === undefined) return;
const bucketKey = key.replace(/^-/, '');
const bucketKey = key.replace(new RegExp(`^${OTHER_BUCKET_SEPARATOR}`), '');
const aggResultBuckets = getAggResultBuckets(
aggsConfig,
updatedResponse.aggregations,
Expand Down

0 comments on commit 8a1a3d9

Please sign in to comment.