From d4e85bc7dae5f2ea0e3cf6564171e8b29a358771 Mon Sep 17 00:00:00 2001 From: Matthew Bargar Date: Mon, 4 Jan 2016 17:12:58 -0500 Subject: [PATCH] Better defaults for scripted fields and prevent scripted fields from getting in the index template --- .../api/index_patterns/register_post.js | 26 +++++++++++++------ test/unit/api/index_patterns/_post.js | 26 +++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/plugins/kibana/server/routes/api/index_patterns/register_post.js b/src/plugins/kibana/server/routes/api/index_patterns/register_post.js index 46a0d2847889..b431ccba194c 100644 --- a/src/plugins/kibana/server/routes/api/index_patterns/register_post.js +++ b/src/plugins/kibana/server/routes/api/index_patterns/register_post.js @@ -3,7 +3,6 @@ const _ = require('lodash'); const {templateToPattern, patternToTemplate} = require('../../../lib/convert_pattern_and_template_name'); const indexPatternSchema = require('../../../lib/schemas/resources/index_pattern_schema'); const handleESError = require('../../../lib/handle_es_error'); -const addMappingInfoToPatternFields = require('../../../lib/add_mapping_info_to_pattern_fields'); const { convertToCamelCase } = require('../../../lib/case_conversion'); const createMappingFromPatternField = require('../../../lib/create_mapping_from_pattern_field'); const castMappingType = require('../../../lib/cast_mapping_type'); @@ -24,16 +23,27 @@ module.exports = function registerPost(server) { const indexPattern = convertToCamelCase(requestDocument.data.attributes); _.forEach(indexPattern.fields, function (field) { - _.defaults(field, { - indexed: true, - analyzed: false, - doc_values: true, - scripted: false, - count: 0 - }); + if (field.scripted) { + _.defaults(field, { + indexed: false, + analyzed: false, + doc_values: false, + count: 0 + }); + } + else { + _.defaults(field, { + indexed: true, + analyzed: false, + doc_values: true, + scripted: false, + count: 0 + }); + } }); const mappings = _(indexPattern.fields) + .reject('scripted') .indexBy('name') .mapValues(createMappingFromPatternField) .value(); diff --git a/test/unit/api/index_patterns/_post.js b/test/unit/api/index_patterns/_post.js index fabd2296d1a6..57468a7750fd 100644 --- a/test/unit/api/index_patterns/_post.js +++ b/test/unit/api/index_patterns/_post.js @@ -97,6 +97,32 @@ define(function (require) { }); }); + bdd.it('scripted fields should not get added to the template', function createTemplate() { + var testData = createTestData().indexPattern; + testData.data.attributes.fields.push({ + 'name': 'Double Bytes', + 'type': 'number', + 'scripted': true, + 'script': 'doc[\'bytes\'].value * 2', + 'lang': 'expression', + 'indexed': false, + 'analyzed': false, + 'doc_values': false + }); + + return request.post('/kibana/index_patterns') + .send(testData) + .expect(201) + .then(function () { + return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'}) + .then(function (template) { + var mappings = template['kibana-logstash-*'].mappings._default_.properties; + expect(mappings).to.be.ok(); + expect(mappings).to.not.have.property('Double Bytes'); + }); + }); + }); + bdd.it('should return 409 conflict when a pattern with the given ID already exists', function patternConflict() { return request.post('/kibana/index_patterns') .send(createTestData().indexPattern)