Skip to content

Commit

Permalink
Add more tests around updating scripts.
Browse files Browse the repository at this point in the history
As per comments on PR elastic#10526 adding more tests around updating scripts
and templates.
  • Loading branch information
Isabel Drost-Fromm committed Apr 21, 2015
1 parent 47c8155 commit 3842605
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java
Expand Up @@ -20,12 +20,10 @@

import com.google.common.collect.Maps;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.index.IndexRequest.OpType;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptResponse;
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequestBuilder;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
Expand Down Expand Up @@ -385,33 +383,35 @@ public void testIndexedTemplateOverwrite() throws Exception {
index("testindex", "test", "1", jsonBuilder().startObject().field("searchtext", "dev1").endObject());
refresh();

PutIndexedScriptResponse scriptResponse = client().preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"ooophrase_prefix\"}}}}").get();
assertTrue(scriptResponse.isCreated());

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

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

try {
for (int i = 1; i < randomIntBetween(2, 11); i++) {
PutIndexedScriptResponse scriptResponse = client().preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"ooophrase_prefix\"}}}}").get();
assertEquals(i, 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
}

PutIndexedScriptRequestBuilder builder = client()
.preparePutIndexedScript(MustacheScriptEngineService.NAME, "git01",
"{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\",\"type\": \"phrase_prefix\"}}}}")
.setOpType(OpType.INDEX);
scriptResponse = builder.get();
assertEquals(i * 2, scriptResponse.getVersion());
SearchResponse searchResponse = 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
setTemplateName("git01").setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(templateParams).get();
assertHitCount(searchResponse, 1);
}

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


Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/elasticsearch/script/IndexedScriptTests.java
Expand Up @@ -22,6 +22,7 @@

import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -87,6 +88,29 @@ public void testFieldIndexedScript() throws ExecutionException, InterruptedExce
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");

for (int i = 1; i < randomIntBetween(2, 11); 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));
}
}

@Test
public void testDisabledUpdateIndexedScriptsOnly() {
if (randomBoolean()) {
Expand Down

0 comments on commit 3842605

Please sign in to comment.