Skip to content

Commit

Permalink
Backport of fix for #10397
Browse files Browse the repository at this point in the history
See #10526 for more details.
  • Loading branch information
Isabel Drost-Fromm committed Apr 24, 2015
1 parent 0a6d87c commit 68571e3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/elasticsearch/script/ScriptService.java
Expand Up @@ -247,7 +247,7 @@ private ScriptEngineService getScriptEngineServiceForFileExt(String fileExtensio
/**
* Compiles a script straight-away, or returns the previously compiled and cached script, without checking if it can be executed based on settings.
*/
public CompiledScript compile(String lang, String script, ScriptType scriptType) {
public CompiledScript compile(String lang, String scriptOrId, ScriptType scriptType) {
//scriptType might not get serialized depending on the version of the node we talk to, if null treat as inline
if (scriptType == null) {
scriptType = ScriptType.INLINE;
Expand All @@ -257,16 +257,16 @@ public CompiledScript compile(String lang, String script, ScriptType scriptType
lang = defaultLang;
}
if (logger.isTraceEnabled()) {
logger.trace("Compiling lang: [{}] type: [{}] script: {}", lang, scriptType, script);
logger.trace("Compiling lang: [{}] type: [{}] script: {}", lang, scriptType, scriptOrId);
}

ScriptEngineService scriptEngineService = getScriptEngineServiceForLang(lang);
CacheKey cacheKey = newCacheKey(scriptEngineService, script);
CacheKey cacheKey = newCacheKey(scriptEngineService, scriptOrId);

if (scriptType == ScriptType.FILE) {
CompiledScript compiled = staticCache.get(cacheKey); //On disk scripts will be loaded into the staticCache by the listener
if (compiled == null) {
throw new ElasticsearchIllegalArgumentException("Unable to find on disk script " + script);
throw new ElasticsearchIllegalArgumentException("Unable to find on disk script " + scriptOrId);
}
return compiled;
}
Expand All @@ -281,12 +281,14 @@ public CompiledScript compile(String lang, String script, ScriptType scriptType

verifyDynamicScripting(lang, scriptEngineService);

String script = scriptOrId;
if (scriptType == ScriptType.INDEXED) {
if (client == null) {
throw new ElasticsearchIllegalArgumentException("Got an indexed script with no Client registered.");
}
final IndexedScript indexedScript = new IndexedScript(lang, script);
final IndexedScript indexedScript = new IndexedScript(lang, scriptOrId);
script = getScriptFromIndex(client, indexedScript.lang, indexedScript.id);
cacheKey = newCacheKey(scriptEngineService, script);
}


Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java
Expand Up @@ -369,6 +369,45 @@ public void testIndexedTemplate() throws Exception {
assertHitCount(sr, 4);
}

// Relates to #10397
@Test
public void testIndexedTemplateOverwrite() throws Exception {
createIndex("testindex");
ensureGreen("testindex");

index("testindex", "test", "1", jsonBuilder().startObject().field("searchtext", "dev1").endObject());
refresh();

int iterations = randomIntBetween(2, 11);
for (int i = 1; i < iterations; i++) {
PutIndexedScriptResponse scriptResponse = client().preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"ooophrase_prefix\"}}}}").get();
assertEquals(i * 2 - 1, scriptResponse.getVersion());

GetIndexedScriptResponse getResponse = client().prepareGetIndexedScript(MustacheScriptEngineService.NAME, "git01").get();
assertTrue(getResponse.isExists());

Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("P_Keyword1", "dev");

try {
client().prepareSearch("testindex").setTypes("test").
setTemplateName("git01").setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
fail("Broken test template is parsing w/o error.");
} catch (SearchPhaseExecutionException e) {
// the above is expected to fail
}

scriptResponse = client()
.preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"phrase_prefix\"}}}}").get();
assertEquals(i * 2, scriptResponse.getVersion());
SearchResponse searchResponse = client().prepareSearch("testindex").setTypes("test").
setTemplateName("git01").setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 1);
}
}

@Test
public void testIndexedTemplateWithArray() throws Exception {
createIndex(ScriptService.SCRIPT_INDEX);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/elasticsearch/script/IndexedScriptTests.java
Expand Up @@ -20,6 +20,9 @@

package org.elasticsearch.script;

import org.elasticsearch.script.groovy.GroovyScriptEngineService;

import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.unit.TimeValue;
Expand Down Expand Up @@ -66,4 +69,29 @@ public void testFieldIndexedScript() throws ExecutionException, InterruptedExce
assertThat((Integer)sh.field("test1").getValue(), equalTo(2));
assertThat((Integer)sh.field("test2").getValue(), equalTo(6));
}

// Relates to #10397
@Test
public void testUpdateScripts() {
createIndex("test_index");
ensureGreen("test_index");
client().prepareIndex("test_index", "test_type", "1").setSource("{\"foo\":\"bar\"}").get();
flush("test_index");

int iterations = randomIntBetween(2, 11);
for (int i = 1; i < iterations; i++) {
PutIndexedScriptResponse response =
client().preparePutIndexedScript(GroovyScriptEngineService.NAME, "script1", "{\"script\":\"" + i + "\"}").get();
assertEquals(i, response.getVersion());

String query = "{"
+ " \"query\" : { \"match_all\": {}}, "
+ " \"script_fields\" : { \"test_field\" : { \"script_id\" : \"script1\", \"lang\":\"groovy\" } } }";
SearchResponse searchResponse = client().prepareSearch().setSource(query).setIndices("test_index").setTypes("test_type").get();
assertHitCount(searchResponse, 1);
SearchHit sh = searchResponse.getHits().getAt(0);
assertThat((Integer)sh.field("test_field").getValue(), equalTo(i));
}
}

}

0 comments on commit 68571e3

Please sign in to comment.