Merge pull request #10976 from colings86/fix/10926

Allow script language to be null when parsing
This commit is contained in:
Colin Goodheart-Smithe 2015-05-07 09:46:37 +01:00
commit 8905e7297b
4 changed files with 102 additions and 6 deletions

View File

@ -585,7 +585,9 @@ public class DocumentMapper implements ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field("script", script); builder.field("script", script);
if (language != null) {
builder.field("lang", language); builder.field("lang", language);
}
if (parameters != null) { if (parameters != null) {
builder.field("params", parameters); builder.field("params", parameters);
} }

View File

@ -26,8 +26,13 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.script.ScriptService.ScriptType; import org.elasticsearch.script.ScriptService.ScriptType;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
public class ScriptParameterParser { public class ScriptParameterParser {
@ -102,7 +107,7 @@ public class ScriptParameterParser {
String parameterName = entry.getKey(); String parameterName = entry.getKey();
Object parameterValue = entry.getValue(); Object parameterValue = entry.getValue();
if (ScriptService.SCRIPT_LANG.match(parameterName)) { if (ScriptService.SCRIPT_LANG.match(parameterName)) {
if (parameterValue instanceof String) { if (parameterValue instanceof String || parameterValue == null) {
lang = (String) parameterValue; lang = (String) parameterValue;
if (removeMatchedEntries) { if (removeMatchedEntries) {
itr.remove(); itr.remove();

View File

@ -0,0 +1,89 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.bwcompat;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.ElasticsearchBackwardsCompatIntegrationTest;
import org.junit.Test;
import java.io.IOException;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertExists;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
public class ScriptTransformBackwardsCompatibilityTests extends ElasticsearchBackwardsCompatIntegrationTest {
@Test
public void testTransformWithNoLangSpecified() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
builder.field("transform");
if (getRandom().nextBoolean()) {
// Single transform
builder.startObject();
buildTransformScript(builder);
builder.endObject();
} else {
// Multiple transforms
int total = between(1, 10);
int actual = between(0, total - 1);
builder.startArray();
for (int s = 0; s < total; s++) {
builder.startObject();
if (s == actual) {
buildTransformScript(builder);
} else {
builder.field("script", "true");
}
builder.endObject();
}
builder.endArray();
}
assertAcked(client().admin().indices().prepareCreate("test").addMapping("test", builder));
indexRandom(getRandom().nextBoolean(), client().prepareIndex("test", "test", "notitle").setSource("content", "findme"), client()
.prepareIndex("test", "test", "badtitle").setSource("content", "findme", "title", "cat"),
client().prepareIndex("test", "test", "righttitle").setSource("content", "findme", "title", "table"));
GetResponse response = client().prepareGet("test", "test", "righttitle").get();
assertExists(response);
assertThat(response.getSource(), both(hasEntry("content", (Object) "findme")).and(not(hasKey("destination"))));
response = client().prepareGet("test", "test", "righttitle").setTransformSource(true).get();
assertExists(response);
assertThat(response.getSource(), both(hasEntry("destination", (Object) "findme")).and(not(hasKey("content"))));
}
private void buildTransformScript(XContentBuilder builder) throws IOException {
String script = "if (ctx._source['title']?.startsWith('t')) { ctx._source['destination'] = ctx._source[sourceField] }; ctx._source.remove(sourceField);";
if (getRandom().nextBoolean()) {
script = script.replace("sourceField", "'content'");
} else {
builder.field("params", ImmutableMap.of("sourceField", "content"));
}
builder.field("script", script);
}
}

View File

@ -130,7 +130,7 @@ public class TransformOnIndexMapperIntegrationTest extends ElasticsearchIntegrat
// Single transform // Single transform
builder.startObject(); builder.startObject();
buildTransformScript(builder); buildTransformScript(builder);
builder.field("lang", GroovyScriptEngineService.NAME); builder.field("lang", randomFrom(null, GroovyScriptEngineService.NAME));
builder.endObject(); builder.endObject();
} else { } else {
// Multiple transforms // Multiple transforms
@ -144,7 +144,7 @@ public class TransformOnIndexMapperIntegrationTest extends ElasticsearchIntegrat
} else { } else {
builder.field("script", "true"); builder.field("script", "true");
} }
builder.field("lang", GroovyScriptEngineService.NAME); builder.field("lang", randomFrom(null, GroovyScriptEngineService.NAME));
builder.endObject(); builder.endObject();
} }
builder.endArray(); builder.endArray();