Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Add metafields from uiSettings to index patterns created by ingest API #7301

Merged
merged 2 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,30 @@ describe('initDefaultFieldProps', function () {
expect(rawField).to.have.property('scripted', false);
expect(rawField).to.have.property('count', 0);
});

it('should apply some overrides to metafields', function () {
const results = initDefaultFieldProps([{name: '_source'}, {name: '_timestamp'}]);
const expected = [
{
name: '_source',
indexed: false,
analyzed: false,
doc_values: false,
count: 0,
scripted: false,
type: '_source'
},
{
name: '_timestamp',
indexed: true,
analyzed: false,
doc_values: false,
count: 0,
scripted: false,
type: 'date'
}
];

expect(_.isEqual(expected, results)).to.be.ok();
});
});
5 changes: 5 additions & 0 deletions src/plugins/kibana/server/lib/init_default_field_props.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import mappingOverrides from './mapping_overrides';

module.exports = function initDefaultFieldProps(fields) {
if (fields === undefined || !_.isArray(fields)) {
Expand Down Expand Up @@ -39,6 +40,10 @@ module.exports = function initDefaultFieldProps(fields) {
count: 0
});
}

if (mappingOverrides[newField.name]) {
_.assign(newField, mappingOverrides[newField.name]);
}
});

return results;
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/kibana/server/lib/mapping_overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default {
_source: {
type: '_source',
indexed: false,
analyzed: false,
doc_values: false
},
_index: {
type: 'string',
indexed: false,
analyzed: false,
doc_values: false
},
_type: {
type: 'string',
indexed: false,
analyzed: false,
doc_values: false
},
_id: {
type: 'string',
indexed: false,
analyzed: false,
doc_values: false
},
_timestamp: {
type: 'date',
indexed: true,
analyzed: false,
doc_values: false
},
_score: {
type: 'number',
indexed: false,
analyzed: false,
doc_values: false
}
};
8 changes: 6 additions & 2 deletions src/plugins/kibana/server/routes/api/ingest/register_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export function registerPost(server) {
payload: ingestConfigSchema
}
},
handler: function (req, reply) {
handler: async function (req, reply) {
const config = await server.uiSettings().getAll();
const boundCallWithRequest = _.partial(server.plugins.elasticsearch.callWithRequest, req);
const requestDocument = _.cloneDeep(req.payload);
const indexPattern = keysToCamelCaseShallow(requestDocument.index_pattern);
Expand All @@ -73,8 +74,11 @@ export function registerPost(server) {
delete indexPattern.id;

const mappings = createMappingsFromPatternFields(indexPattern.fields);
indexPattern.fields = initDefaultFieldProps(indexPattern.fields);

const metaFields = _.get(config, 'metaFields.userValue', config.metaFields.value);
Copy link
Contributor

@bevacqua bevacqua Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consumers of the uiSettings shouldn't need to know internals like this. I suggest we add a .get(key) method like:

key => getAll().then(config => _.get(config, `${key}.userValue`, config[key].value));

Code here becomes:

const metaFields = await server.uiSettings().get('metaFields');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I had to do a lot of trial and error to figure this out.

const indexPatternMetaFields = _.map(metaFields, name => ({name}));

indexPattern.fields = initDefaultFieldProps(indexPattern.fields.concat(indexPatternMetaFields));
indexPattern.fields = JSON.stringify(indexPattern.fields);
indexPattern.fieldFormatMap = JSON.stringify(indexPattern.fieldFormatMap);

Expand Down
19 changes: 19 additions & 0 deletions test/unit/api/ingest/_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ define(function (require) {
});
});

bdd.it('should include meta fields specified in uiSettings in the index pattern', function metaFields() {
return request.post('/kibana/ingest')
.send(createTestData())
.expect(204)
.then(function () {
return scenarioManager.client.get({
index: '.kibana',
type: 'index-pattern',
id: 'logstash-*'
})
.then(function (res) {
const fields = JSON.parse(res._source.fields);
const sourceField = _.find(fields, {name: '_source'});
expect(sourceField).to.be.ok();
expect(sourceField).to.have.property('name', '_source');
});
});
});

bdd.it('should create index template with _default_ mappings based on the info in the ingest config',
function createTemplate() {
return request.post('/kibana/ingest')
Expand Down