Skip to content

Commit

Permalink
Fixes elastic#12602 - Change TSVB Fields API to use fieldCaps API
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Jun 30, 2017
1 parent aa729af commit 139ccea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 69 deletions.
79 changes: 29 additions & 50 deletions src/core_plugins/metrics/server/lib/__tests__/get_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]);
});
});
Expand Down
30 changes: 11 additions & 19 deletions src/core_plugins/metrics/server/lib/get_fields.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import { sortBy, uniq } from 'lodash';

export function getParams(req) {
const index = req.query.index || '*';
Expand All @@ -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;
Expand Down

0 comments on commit 139ccea

Please sign in to comment.