Skip to content

Commit

Permalink
Remove type filter helper (#30887) (#30908)
Browse files Browse the repository at this point in the history
* Remove type filter helper.

This is no longer required in ES 7.0 onwards.

* Remove more usages of filter helper
  • Loading branch information
ycombinator authored Feb 13, 2019
1 parent c38a299 commit ae7d3e4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 47 deletions.
19 changes: 3 additions & 16 deletions x-pack/plugins/monitoring/server/lib/__tests__/create_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@ import expect from 'expect.js';
import { set } from 'lodash';
import { MissingRequiredError } from '../error_missing_required';
import { ElasticsearchMetric } from '../metrics';
import { createTypeFilter, createQuery } from '../create_query.js';
import { createQuery } from '../create_query.js';

let metric;


describe('Create Type Filter', () => {
it('Builds a type filter syntax', () => {
const typeFilter = createTypeFilter('my_type');
expect(typeFilter).to.eql({
bool: { should: [
{ term: { _type: 'my_type' } },
{ term: { type: 'my_type' } }
] }
});
});
});

describe('Create Query', () => {
beforeEach(() => {
metric = ElasticsearchMetric.getMetricFields();
Expand Down Expand Up @@ -97,7 +84,7 @@ describe('Create Query', () => {
const options = { type: 'test-type-yay', metric };
const result = createQuery(options);
let expected = {};
expected = set(expected, 'bool.filter[0].bool.should', [ { term: { _type: 'test-type-yay' } }, { term: { type: 'test-type-yay' } } ]);
expected = set(expected, 'bool.filter[0].term', { type: 'test-type-yay' });
expect(result).to.be.eql(expected);
});

Expand All @@ -111,7 +98,7 @@ describe('Create Query', () => {
};
const result = createQuery(options);
let expected = {};
expected = set(expected, 'bool.filter[0].bool.should', [ { term: { _type: 'test-type-yay' } }, { term: { type: 'test-type-yay' } } ]);
expected = set(expected, 'bool.filter[0].term', { type: 'test-type-yay' });
expected = set(expected, 'bool.filter[1].term', {
'source_node.uuid': 'abc123'
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import expect from 'expect.js';
import { createTypeFilter } from '../../create_query';
import { createBeatsQuery } from '../create_beats_query';

describe('createBeatsQuery', () => {
Expand All @@ -24,9 +23,9 @@ describe('createBeatsQuery', () => {
const query1 = createBeatsQuery();
const query2 = createBeatsQuery({});

expect(query1.bool.filter[0]).to.eql(createTypeFilter('beats_stats'));
expect(query1.bool.filter[0]).to.eql({ term: { type: 'beats_stats' } });
expect(query1.bool.filter[query1.bool.filter.length - 1]).to.eql(noApmFilter);
expect(query2.bool.filter[0]).to.eql(createTypeFilter('beats_stats'));
expect(query2.bool.filter[0]).to.eql({ term: { type: 'beats_stats' } });
expect(query2.bool.filter[query2.bool.filter.length - 1]).to.eql(noApmFilter);
});

Expand All @@ -44,7 +43,7 @@ describe('createBeatsQuery', () => {
const queryFilters = query.bool.filter;
const filterCount = queryFilters.length;

expect(queryFilters[0]).to.eql(createTypeFilter('beats_stats'));
expect(queryFilters[0]).to.eql({ term: { type: 'beats_stats' } });

filters.forEach((filter, index) => {
// "custom" filters are added at the end of all known filters, and the last "custom" filter is the noApmFilter
Expand All @@ -55,4 +54,4 @@ describe('createBeatsQuery', () => {
});
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { get, set, find } from 'lodash';
import { checkParam } from '../error_missing_required';
import { createTypeFilter } from '../create_query';
import { LOGGING_TAG, STANDALONE_CLUSTER_CLUSTER_UUID } from '../../../common/constants';

async function findSupportedBasicLicenseCluster(req, clusters, kbnIndexPattern, kibanaUuid, serverLog) {
Expand All @@ -29,7 +28,7 @@ async function findSupportedBasicLicenseCluster(req, clusters, kbnIndexPattern,
query: {
bool: {
filter: [
{ ...createTypeFilter('kibana_stats') },
{ term: { type: 'kibana_stats' } },
{ term: { 'kibana_stats.kibana.uuid': kibanaUuid } },
{ range: { timestamp: { gte, lte, format: 'strict_date_optional_time' } } }
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { get, find } from 'lodash';
import { checkParam } from '../error_missing_required';
import { createTypeFilter } from '../create_query';

/**
* Augment the {@clusters} with their cluster state's from the {@code response}.
Expand Down Expand Up @@ -64,7 +63,7 @@ export function getClustersState(req, esIndexPattern, clusters) {
query: {
bool: {
filter: [
{ ...createTypeFilter('cluster_state') },
{ term: { type: 'cluster_state' } },
{ terms: { 'cluster_uuid': clusterUuids } }
]
}
Expand Down
23 changes: 1 addition & 22 deletions x-pack/plugins/monitoring/server/lib/create_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,10 @@ import moment from 'moment';
import { standaloneClusterFilter } from './standalone_clusters';
import { STANDALONE_CLUSTER_CLUSTER_UUID } from '../../common/constants';

/*
* Builds a type filter syntax that supports backwards compatibility to read
* from indices before and after `_type` is removed from Elasticsearch
*
* TODO: this backwards compatibility helper will only be supported for 5.x-6. This
* function should be removed in 7.0
*/
export const createTypeFilter = (type) => {
return {
bool: {
should: [
{ term: { _type: type } },
{ term: { type } }
]
}
};
};

/*
* Creates the boilerplace for querying monitoring data, including filling in
* document UUIDs, start time and end time, and injecting additional filters.
*
* Create a bool for types that will query for documents using `_type` (true type) and `type` (as a field)
* Makes backwards compatibility for types being deprecated in Elasticsearch
*
* Options object:
* @param {String} options.type - `type` field value of the documents
* @param {Array} options.filters - additional filters to add to the `bool` section of the query. Default: []
Expand All @@ -52,7 +31,7 @@ export function createQuery(options) {

let typeFilter;
if (type) {
typeFilter = createTypeFilter(type);
typeFilter = { term: { type } };
}

let clusterUuidFilter;
Expand Down

0 comments on commit ae7d3e4

Please sign in to comment.