Skip to content

Commit

Permalink
Fixes error when filters agg filters are a query_string query (elasti…
Browse files Browse the repository at this point in the history
  • Loading branch information
Bargs authored and lukeelmers committed Aug 15, 2019
1 parent ebc1c07 commit 6f47b00
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ export class QueryBarInputUI extends Component<Props, State> {
};

public componentDidMount() {
const parsedQuery = fromUser(toUser(this.props.query.query));
if (!isEqual(this.props.query.query, parsedQuery)) {
this.onChange({ ...this.props.query, query: parsedQuery });
}

this.persistedLog = this.props.persistedLog
? this.props.persistedLog
: getQueryLog(this.props.appName, this.props.query.language);
Expand All @@ -397,6 +402,11 @@ export class QueryBarInputUI extends Component<Props, State> {
}

public componentDidUpdate(prevProps: Props) {
const parsedQuery = fromUser(toUser(this.props.query.query));
if (!isEqual(this.props.query.query, parsedQuery)) {
this.onChange({ ...this.props.query, query: parsedQuery });
}

this.persistedLog = this.props.persistedLog
? this.props.persistedLog
: getQueryLog(this.props.appName, this.props.query.language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export function fromUser(userInput: object | string) {
const matchAll = '';

if (_.isObject(userInput)) {
// If we get an empty object, treat it as a *
if (!Object.keys(userInput).length) {
return matchAll;
}
return userInput;
}

Expand Down
38 changes: 38 additions & 0 deletions src/legacy/core_plugins/kibana/migrations/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,39 @@ function replaceMovAvgToMovFn(doc, logger) {
return doc;
}

function migrateFiltersAggQueryStringQueries(doc) {
const visStateJSON = get(doc, 'attributes.visState');

if (visStateJSON) {
try {
const visState = JSON.parse(visStateJSON);
if (visState && visState.aggs) {
visState.aggs.forEach(agg => {
if (agg.type !== 'filters') return doc;

agg.params.filters.forEach(filter => {
if (filter.input.query.query_string) {
filter.input.query = filter.input.query.query_string.query;
}
});
});

return {
...doc,
attributes: {
...doc.attributes,
visState: JSON.stringify(visState),
},
};
}
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
}
}
return doc;

}

const executeMigrations720 = flow(
migratePercentileRankAggregation,
migrateDateHistogramAggregation
Expand All @@ -376,6 +409,10 @@ const executeMigrations730 = flow(
replaceMovAvgToMovFn
);

const executeVisualizationMigrations731 = flow(
migrateFiltersAggQueryStringQueries,
);

export const migrations = {
'index-pattern': {
'6.5.0': doc => {
Expand Down Expand Up @@ -481,6 +518,7 @@ export const migrations = {
'7.0.1': removeDateHistogramTimeZones,
'7.2.0': doc => executeMigrations720(doc),
'7.3.0': executeMigrations730,
'7.3.1': executeVisualizationMigrations731,
},
dashboard: {
'7.0.0': doc => {
Expand Down
37 changes: 37 additions & 0 deletions src/legacy/core_plugins/kibana/migrations/migrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,43 @@ Array [
expect(series[0].filter).toEqual(params.series[0].filter);
});
});

describe('7.3.1', () => {
const migrate = migrations.visualization['7.3.1'];

it('should migrate filters agg query string queries', () => {
const state = {
aggs: [
{ type: 'count', params: {} },
{
type: 'filters',
params: {
filters: [{
input: {
query: {
query_string: { query: 'machine.os.keyword:\"win 8\"' }
}
}
}]
}
}
],
};
const expected = {
aggs: [
{ type: 'count', params: {} },
{
type: 'filters',
params: {
filters: [{ input: { query: 'machine.os.keyword:\"win 8\"' } }]
}
}
],
};
const migratedDoc = migrate({ attributes: { visState: JSON.stringify(state) } });
expect(migratedDoc).toEqual({ attributes: { visState: JSON.stringify(expected) } });
});
});
});

describe('dashboard', () => {
Expand Down

0 comments on commit 6f47b00

Please sign in to comment.