diff --git a/src/core_plugins/metrics/server/lib/__tests__/get_fields.js b/src/core_plugins/metrics/server/lib/__tests__/get_fields.js index 1877764f83b9e..0f147e65e4c0e 100644 --- a/src/core_plugins/metrics/server/lib/__tests__/get_fields.js +++ b/src/core_plugins/metrics/server/lib/__tests__/get_fields.js @@ -21,62 +21,41 @@ describe('getFields', () => { describe('handleResponse', () => { it('returns a valid response', () => { const resp = { - 'foo': { - 'mappings': { - 'bar': { - '@timestamp': { - 'full_name': '@timestamp', - 'mapping': { - '@timestamp': { - 'type': 'date' - } - } - } + fields: { + '@timestamp': { + 'date': { + 'type': 'date', + 'searchable': true, + 'aggregatable': true } - } - }, - 'twitter': { - 'mappings': { - 'tweet': { - 'message': { - 'full_name': 'message', - 'mapping': { - 'message': { - 'type': 'text', - 'fields': { - 'keyword': { - 'type': 'keyword', - 'ignore_above': 256 - } - } - } - } - }, - '@timestamp': { - 'full_name': '@timestamp', - 'mapping': { - '@timestamp': { - 'type': 'date' - } - } - }, - 'id.keyword': { - 'full_name': 'id.keyword', - 'mapping': { - 'keyword': { - 'type': 'keyword', - 'ignore_above': 256 - } - } - } + }, + 'id.keyword': { + 'keyword': { + 'type': 'keyword', + 'searchable': true, + 'aggregatable': true + } + }, + 'message': { + 'text': { + 'type': 'text', + 'searchable': true, + 'aggregatable': false + } + }, + 'beat.hostname': { + 'keyword': { + 'type': 'keyword', + 'searchable': true, + 'aggregatable': true } } } }; expect(handleResponse(resp)).to.eql([ - { name: '@timestamp', type: 'date' }, - { name: 'id.keyword', type: 'keyword' }, - { name: 'message', type: 'text' } + { name: '@timestamp', type: 'date', aggregatable: true }, + { name: 'id.keyword', type: 'keyword', aggregatable: true }, + { name: 'beat.hostname', type: 'keyword', aggregatable: true } ]); }); }); diff --git a/src/core_plugins/metrics/server/lib/get_fields.js b/src/core_plugins/metrics/server/lib/get_fields.js index 0c308e7537120..db1beeb6dbf2e 100644 --- a/src/core_plugins/metrics/server/lib/get_fields.js +++ b/src/core_plugins/metrics/server/lib/get_fields.js @@ -1,4 +1,4 @@ -import _ from 'lodash'; +import { sortBy, uniq } from 'lodash'; export function getParams(req) { const index = req.query.index || '*'; @@ -7,33 +7,25 @@ export function getParams(req) { fields: ['*'], ignoreUnavailable: false, allowNoIndices: false, - includeDefaults: true }; } export function handleResponse(resp) { - return _.reduce(resp, (acc, index) => { - _.each(index.mappings, (type) => { - _.each(type, (field, fullName) => { - const name = _.last(fullName.split(/\./)); - const enabled = _.get(field, `mapping.${name}.enabled`, true); - const fieldType = _.get(field, `mapping.${name}.type`); - if (enabled && fieldType) { - acc.push({ - name: _.get(field, 'full_name', fullName), - type: fieldType - }); - } - }); - }); - return _(acc).sortBy('name').uniq(row => row.name).value(); - }, []); + const fields = Object.keys(resp.fields) + .map(name => { + const def = resp.fields[name]; + const type = Object.keys(def)[0]; + const { aggregatable } = def[type]; + return { name, type, aggregatable }; + }) + .filter(field => field.aggregatable); + return uniq(sortBy(fields)); } function getFields(req) { const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('data'); const params = getParams(req); - return callWithRequest(req, 'indices.getFieldMapping', params).then(handleResponse); + return callWithRequest(req, 'fieldCaps', params).then(handleResponse); } export default getFields;