Skip to content

Commit

Permalink
fixing boolean filters (#15927) (#16013)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Jan 12, 2018
1 parent 06b88ed commit 549b48f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/ui/public/agg_types/__tests__/buckets/create_filter/terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,24 @@ describe('AggConfig Filters', function () {
expect(filter.meta).to.have.property('index', indexPattern.id);

});

it('should set query to true or false for boolean filter', () => {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [ { type: 'terms', schema: 'segment', params: { field: 'ssl' } } ]
});
const aggConfig = vis.aggs.byTypeName.terms[0];
const filterFalse = createFilter(aggConfig, 0);
expect(filterFalse).to.have.property('query');
expect(filterFalse.query).to.have.property('match');
expect(filterFalse.query.match).to.have.property('ssl');
expect(filterFalse.query.match.ssl).to.have.property('query', false);

const filterTrue = createFilter(aggConfig, 1);
expect(filterTrue).to.have.property('query');
expect(filterTrue.query).to.have.property('match');
expect(filterTrue.query.match).to.have.property('ssl');
expect(filterTrue.query.match.ssl).to.have.property('query', true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const OtherBucketHelperProvider = (Private) => {
const bucketAggs = aggConfigs.filter(agg => agg.type.type === 'buckets');
const index = bucketAggs.findIndex(agg => agg.id === aggWithOtherBucket.id);
const aggs = aggConfigs.toDsl();
const indexPattern = aggWithOtherBucket.params.field.indexPattern;

// create filters aggregation
const filterAgg = new AggConfig(aggConfigs[index].vis, {
Expand Down Expand Up @@ -136,7 +137,7 @@ export const OtherBucketHelperProvider = (Private) => {
});

resultAgg.filters.filters[key] = {
bool: buildQueryFromFilters(filters, _.noop)
bool: buildQueryFromFilters(filters, _.noop, indexPattern)
};
};
walkBucketTree(0, response.aggregations, bucketAggs[0].id, [], '');
Expand Down
8 changes: 7 additions & 1 deletion src/ui/public/courier/data_source/_migrate_filter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import _ from 'lodash';
import { getConvertedValueForField } from 'ui/filter_manager/lib/phrase';

export function migrateFilter(filter) {
export function migrateFilter(filter, indexPattern) {
if (filter.match) {
const fieldName = Object.keys(filter.match)[0];


if (isMatchPhraseFilter(filter, fieldName)) {
const params = _.get(filter, ['match', fieldName]);
if (indexPattern) {
const field = indexPattern.fields.find(f => f.name === fieldName);
params.query = getConvertedValueForField(field, params.query);
}
return {
match_phrase: {
[fieldName]: _.omit(params, 'type'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function BuildESQueryProvider(Private) {
const kueryQuery = buildQueryFromKuery(indexPattern, queriesByLanguage.kuery);
const kqlQuery = buildQueryFromKql(indexPattern, queriesByLanguage.kql);
const luceneQuery = buildQueryFromLucene(queriesByLanguage.lucene, decorateQuery);
const filterQuery = buildQueryFromFilters(filters, decorateQuery);
const filterQuery = buildQueryFromFilters(filters, decorateQuery, indexPattern);

return {
bool: {
Expand Down
10 changes: 7 additions & 3 deletions src/ui/public/courier/data_source/build_query/from_filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const cleanFilter = function (filter) {
return _.omit(filter, ['meta', '$state']);
};

export function buildQueryFromFilters(filters, decorateQuery) {
export function buildQueryFromFilters(filters, decorateQuery, indexPattern) {
_.each(filters, function (filter) {
if (filter.query) {
decorateQuery(filter.query);
Expand All @@ -51,13 +51,17 @@ export function buildQueryFromFilters(filters, decorateQuery) {
.filter(filterNegate(false))
.map(translateToQuery)
.map(cleanFilter)
.map(migrateFilter),
.map(filter => {
return migrateFilter(filter, indexPattern);
}),
filter: [],
should: [],
must_not: (filters || [])
.filter(filterNegate(true))
.map(translateToQuery)
.map(cleanFilter)
.map(migrateFilter)
.map(filter => {
return migrateFilter(filter, indexPattern);
}),
};
}
5 changes: 3 additions & 2 deletions src/ui/public/filter_manager/lib/phrase.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export function buildPhraseFilter(field, value, indexPattern) {
const filter = { meta: { index: indexPattern.id } };
const convertedValue = getConvertedValueForField(field, value);

if (field.scripted) {
filter.script = getPhraseScript(field, value);
filter.meta.field = field.name;
} else {
filter.query = { match: {} };
filter.query.match[field.name] = {
query: value,
query: convertedValue,
type: 'phrase'
};
}
Expand All @@ -32,7 +33,7 @@ export function getPhraseScript(field, value) {
// See https://github.com/elastic/elasticsearch/issues/20941 and https://github.com/elastic/kibana/issues/8677
// and https://github.com/elastic/elasticsearch/pull/22201
// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0.
function getConvertedValueForField(field, value) {
export function getConvertedValueForField(field, value) {
if (typeof value !== 'boolean' && field.type === 'boolean') {
if (value !== 1 && value !== 0) {
throw new Error('Boolean scripted fields must return true or false');
Expand Down

0 comments on commit 549b48f

Please sign in to comment.