Indexed Scripts/Templates: cleanup
This commit cleans up some code around the indexed script/templates feature. Remove dead code in ScriptService. Remove setXScript methods for UpdateRequestBuilder and use setScript(script,type) instead
This commit is contained in:
parent
2bac455cd0
commit
73d93e5fd8
|
@ -90,52 +90,6 @@ public class UpdateRequestBuilder extends InstanceShardOperationRequestBuilder<U
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The indexed script to execute. Note, make sure not to send different script each times and instead
|
|
||||||
* use script params if possible with the same (automatically compiled) script.
|
|
||||||
* <p>
|
|
||||||
* The script works with the variable <code>ctx</code>, which is bound to the entry,
|
|
||||||
* e.g. <code>ctx._source.mycounter += 1</code>.
|
|
||||||
*
|
|
||||||
* @see #setScriptLang(String)
|
|
||||||
* @see #setScriptParams(Map)
|
|
||||||
*/
|
|
||||||
public UpdateRequestBuilder setIndexedScript(String script) {
|
|
||||||
request.script(script, ScriptService.ScriptType.INDEXED);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The on disk script to execute. Note, make sure not to send different script each times and instead
|
|
||||||
* use script params if possible with the same (automatically compiled) script.
|
|
||||||
* <p>
|
|
||||||
* The script works with the variable <code>ctx</code>, which is bound to the entry,
|
|
||||||
* e.g. <code>ctx._source.mycounter += 1</code>.
|
|
||||||
*
|
|
||||||
* @see #setScriptLang(String)
|
|
||||||
* @see #setScriptParams(Map)
|
|
||||||
*/
|
|
||||||
public UpdateRequestBuilder setOnDiskScript(String script) {
|
|
||||||
request.script(script, ScriptService.ScriptType.FILE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The inline script to execute. Note, make sure not to send different script each times and instead
|
|
||||||
* use script params if possible with the same (automatically compiled) script.
|
|
||||||
* <p>
|
|
||||||
* The script works with the variable <code>ctx</code>, which is bound to the entry,
|
|
||||||
* e.g. <code>ctx._source.mycounter += 1</code>.
|
|
||||||
*
|
|
||||||
* @see #setScriptLang(String)
|
|
||||||
* @see #setScriptParams(Map)
|
|
||||||
*/
|
|
||||||
public UpdateRequestBuilder setInlineScript(String script) {
|
|
||||||
request.script(script, ScriptService.ScriptType.INLINE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The language of the script to execute.
|
* The language of the script to execute.
|
||||||
* Valid options are: mvel, js, groovy, python, and native (Java)<br>
|
* Valid options are: mvel, js, groovy, python, and native (Java)<br>
|
||||||
|
|
|
@ -141,22 +141,15 @@ public class ScriptService extends AbstractComponent {
|
||||||
public static final ParseField SCRIPT_ID = new ParseField("script_id", "id");
|
public static final ParseField SCRIPT_ID = new ParseField("script_id", "id");
|
||||||
public static final ParseField SCRIPT_INLINE = new ParseField("script","scriptField");
|
public static final ParseField SCRIPT_INLINE = new ParseField("script","scriptField");
|
||||||
|
|
||||||
|
|
||||||
private static final int INLINE_VAL = 0;
|
|
||||||
private static final int INDEXED_VAL = 1;
|
|
||||||
private static final int FILE_VAL = 2;
|
|
||||||
|
|
||||||
public static enum ScriptType {
|
public static enum ScriptType {
|
||||||
|
|
||||||
INLINE(INLINE_VAL),
|
INLINE,
|
||||||
INDEXED(INDEXED_VAL),
|
INDEXED,
|
||||||
FILE(FILE_VAL);
|
FILE;
|
||||||
|
|
||||||
private int value;
|
private static final int INLINE_VAL = 0;
|
||||||
|
private static final int INDEXED_VAL = 1;
|
||||||
private ScriptType(int i) {
|
private static final int FILE_VAL = 2;
|
||||||
this.value = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ScriptType readFrom(StreamInput in) throws IOException {
|
public static ScriptType readFrom(StreamInput in) throws IOException {
|
||||||
int scriptTypeVal = in.readVInt();
|
int scriptTypeVal = in.readVInt();
|
||||||
|
@ -167,10 +160,12 @@ public class ScriptService extends AbstractComponent {
|
||||||
return INLINE;
|
return INLINE;
|
||||||
case FILE_VAL:
|
case FILE_VAL:
|
||||||
return FILE;
|
return FILE;
|
||||||
|
default:
|
||||||
|
throw new ElasticsearchIllegalArgumentException("Unexpected value read for ScriptType got [" + scriptTypeVal +
|
||||||
|
"] expected one of ["+INLINE_VAL +"," + INDEXED_VAL + "," + FILE_VAL+"]");
|
||||||
}
|
}
|
||||||
throw new ElasticsearchIllegalArgumentException("Unexpected value read for ScriptType got [" + scriptTypeVal +
|
|
||||||
"] expected one of ["+INLINE_VAL +"," + INDEXED_VAL + "," + FILE_VAL+"]");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeTo(ScriptType scriptType, StreamOutput out) throws IOException{
|
public static void writeTo(ScriptType scriptType, StreamOutput out) throws IOException{
|
||||||
if (scriptType != null) {
|
if (scriptType != null) {
|
||||||
|
@ -184,6 +179,8 @@ public class ScriptService extends AbstractComponent {
|
||||||
case FILE:
|
case FILE:
|
||||||
out.writeVInt(FILE_VAL);
|
out.writeVInt(FILE_VAL);
|
||||||
return;
|
return;
|
||||||
|
default:
|
||||||
|
throw new ElasticsearchIllegalStateException("Unknown ScriptType " + scriptType);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out.writeVInt(INLINE_VAL); //Default to inline
|
out.writeVInt(INLINE_VAL); //Default to inline
|
||||||
|
@ -431,12 +428,6 @@ public class ScriptService extends AbstractComponent {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putScriptToIndex(Client client, BytesReference scriptBytes, @Nullable String scriptLang, String id,
|
|
||||||
@Nullable TimeValue timeout, @Nullable String sOpType, ActionListener<IndexResponse> listener)
|
|
||||||
throws ElasticsearchIllegalArgumentException, IOException {
|
|
||||||
putScriptToIndex(client,scriptBytes,scriptLang,id,timeout,sOpType, Versions.MATCH_ANY, VersionType.INTERNAL, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putScriptToIndex(Client client, BytesReference scriptBytes, @Nullable String scriptLang, String id,
|
public void putScriptToIndex(Client client, BytesReference scriptBytes, @Nullable String scriptLang, String id,
|
||||||
@Nullable TimeValue timeout, @Nullable String sOpType, long version,
|
@Nullable TimeValue timeout, @Nullable String sOpType, long version,
|
||||||
VersionType versionType, ActionListener<IndexResponse> listener) {
|
VersionType versionType, ActionListener<IndexResponse> listener) {
|
||||||
|
@ -612,14 +603,10 @@ public class ScriptService extends AbstractComponent {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CacheKey {
|
public final static class CacheKey {
|
||||||
public final String lang;
|
public final String lang;
|
||||||
public final String script;
|
public final String script;
|
||||||
|
|
||||||
private CacheKey(){
|
|
||||||
throw new ElasticsearchIllegalStateException("CacheKey default constructor should never be called.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public CacheKey(String lang, String script) {
|
public CacheKey(String lang, String script) {
|
||||||
this.lang = lang;
|
this.lang = lang;
|
||||||
this.script = script;
|
this.script = script;
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.discovery.Discovery;
|
import org.elasticsearch.discovery.Discovery;
|
||||||
import org.elasticsearch.rest.RestStatus;
|
import org.elasticsearch.rest.RestStatus;
|
||||||
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
|
||||||
import org.elasticsearch.test.junit.annotations.TestLogging;
|
import org.elasticsearch.test.junit.annotations.TestLogging;
|
||||||
|
@ -98,7 +99,7 @@ public class NoMasterNodeTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
client().prepareUpdate("test", "type1", "1").setInlineScript("test script").setTimeout(timeout).execute().actionGet();
|
client().prepareUpdate("test", "type1", "1").setScript("test script", ScriptService.ScriptType.INLINE).setTimeout(timeout).execute().actionGet();
|
||||||
fail("Expected ClusterBlockException");
|
fail("Expected ClusterBlockException");
|
||||||
} catch (ClusterBlockException e) {
|
} catch (ClusterBlockException e) {
|
||||||
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
|
assertThat(System.currentTimeMillis() - now, greaterThan(timeout.millis() - 50));
|
||||||
|
|
|
@ -36,6 +36,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.index.VersionType;
|
import org.elasticsearch.index.VersionType;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -65,8 +66,10 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
assertThat(bulkResponse.getItems().length, equalTo(5));
|
assertThat(bulkResponse.getItems().length, equalTo(5));
|
||||||
|
|
||||||
bulkResponse = client().prepareBulk()
|
bulkResponse = client().prepareBulk()
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1").setInlineScript("ctx._source.field += 1"))
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1")
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2").setInlineScript("ctx._source.field += 1").setRetryOnConflict(3))
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
|
||||||
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setRetryOnConflict(3))
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject()))
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3").setDoc(jsonBuilder().startObject().field("field1", "test").endObject()))
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
|
@ -95,10 +98,13 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("test"));
|
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("test"));
|
||||||
|
|
||||||
bulkResponse = client().prepareBulk()
|
bulkResponse = client().prepareBulk()
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("6").setInlineScript("ctx._source.field += 1")
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("6")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.setUpsert(jsonBuilder().startObject().field("field", 0).endObject()))
|
.setUpsert(jsonBuilder().startObject().field("field", 0).endObject()))
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("7").setInlineScript("ctx._source.field += 1"))
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("7")
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2").setInlineScript("ctx._source.field += 1"))
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
|
||||||
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE))
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
assertThat(bulkResponse.hasFailures(), equalTo(true));
|
assertThat(bulkResponse.hasFailures(), equalTo(true));
|
||||||
|
@ -188,9 +194,12 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
assertThat(bulkResponse.getItems().length, equalTo(3));
|
assertThat(bulkResponse.getItems().length, equalTo(3));
|
||||||
|
|
||||||
bulkResponse = client().prepareBulk()
|
bulkResponse = client().prepareBulk()
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1").setInlineScript("ctx._source.field += a").setFields("field"))
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("1")
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2").setInlineScript("ctx._source.field += 1").setFields("field"))
|
.setScript("ctx._source.field += a", ScriptService.ScriptType.INLINE).setFields("field"))
|
||||||
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3").setInlineScript("ctx._source.field += a").setFields("field"))
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("2")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setFields("field"))
|
||||||
|
.add(client().prepareUpdate().setIndex("test").setType("type1").setId("3")
|
||||||
|
.setScript("ctx._source.field += a", ScriptService.ScriptType.INLINE).setFields("field"))
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
assertThat(bulkResponse.hasFailures(), equalTo(true));
|
assertThat(bulkResponse.hasFailures(), equalTo(true));
|
||||||
|
@ -224,7 +233,7 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
builder.add(
|
builder.add(
|
||||||
client().prepareUpdate()
|
client().prepareUpdate()
|
||||||
.setIndex("test").setType("type1").setId(Integer.toString(i))
|
.setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||||
.setInlineScript("ctx._source.counter += 1").setFields("counter")
|
.setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE).setFields("counter")
|
||||||
.setUpsert(jsonBuilder().startObject().field("counter", 1).endObject())
|
.setUpsert(jsonBuilder().startObject().field("counter", 1).endObject())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -255,7 +264,7 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
UpdateRequestBuilder updateBuilder = client().prepareUpdate()
|
UpdateRequestBuilder updateBuilder = client().prepareUpdate()
|
||||||
.setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
|
.setIndex("test").setType("type1").setId(Integer.toString(i)).setFields("counter");
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
updateBuilder.setInlineScript("ctx._source.counter += 1");
|
updateBuilder.setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE);
|
||||||
} else {
|
} else {
|
||||||
updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
|
updateBuilder.setDoc(jsonBuilder().startObject().field("counter", 2).endObject());
|
||||||
}
|
}
|
||||||
|
@ -285,7 +294,7 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
for (int i = (numDocs / 2); i < maxDocs; i++) {
|
for (int i = (numDocs / 2); i < maxDocs; i++) {
|
||||||
builder.add(
|
builder.add(
|
||||||
client().prepareUpdate()
|
client().prepareUpdate()
|
||||||
.setIndex("test").setType("type1").setId(Integer.toString(i)).setInlineScript("ctx._source.counter += 1")
|
.setIndex("test").setType("type1").setId(Integer.toString(i)).setScript("ctx._source.counter += 1", ScriptService.ScriptType.INLINE)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
response = builder.execute().actionGet();
|
response = builder.execute().actionGet();
|
||||||
|
@ -309,7 +318,8 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
for (int i = 0; i < numDocs; i++) {
|
for (int i = 0; i < numDocs; i++) {
|
||||||
builder.add(
|
builder.add(
|
||||||
client().prepareUpdate()
|
client().prepareUpdate()
|
||||||
.setIndex("test").setType("type1").setId(Integer.toString(i)).setInlineScript("ctx.op = \"none\"")
|
.setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||||
|
.setScript("ctx.op = \"none\"", ScriptService.ScriptType.INLINE)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
response = builder.execute().actionGet();
|
response = builder.execute().actionGet();
|
||||||
|
@ -327,7 +337,8 @@ public class BulkTests extends ElasticsearchIntegrationTest {
|
||||||
for (int i = 0; i < numDocs; i++) {
|
for (int i = 0; i < numDocs; i++) {
|
||||||
builder.add(
|
builder.add(
|
||||||
client().prepareUpdate()
|
client().prepareUpdate()
|
||||||
.setIndex("test").setType("type1").setId(Integer.toString(i)).setInlineScript("ctx.op = \"delete\"")
|
.setIndex("test").setType("type1").setId(Integer.toString(i))
|
||||||
|
.setScript("ctx.op = \"delete\"", ScriptService.ScriptType.INLINE)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
response = builder.execute().actionGet();
|
response = builder.execute().actionGet();
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.action.search.SearchType;
|
||||||
import org.elasticsearch.client.Requests;
|
import org.elasticsearch.client.Requests;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ public class AliasRoutingTests extends ElasticsearchIntegrationTest {
|
||||||
logger.info("--> updating with id [1] and routing through alias");
|
logger.info("--> updating with id [1] and routing through alias");
|
||||||
client().prepareUpdate("alias0", "type1", "1")
|
client().prepareUpdate("alias0", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
||||||
.setInlineScript("ctx._source.field = 'value2'")
|
.setScript("ctx._source.field = 'value2'", ScriptService.ScriptType.INLINE)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
assertThat(client().prepareGet("alias0", "type1", "1").execute().actionGet().isExists(), equalTo(true));
|
assertThat(client().prepareGet("alias0", "type1", "1").execute().actionGet().isExists(), equalTo(true));
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.script.AbstractExecutableScript;
|
import org.elasticsearch.script.AbstractExecutableScript;
|
||||||
import org.elasticsearch.script.ExecutableScript;
|
import org.elasticsearch.script.ExecutableScript;
|
||||||
import org.elasticsearch.script.NativeScriptFactory;
|
import org.elasticsearch.script.NativeScriptFactory;
|
||||||
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -58,7 +59,9 @@ public class UpdateByNativeScriptTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
Map<String, Object> params = Maps.newHashMap();
|
Map<String, Object> params = Maps.newHashMap();
|
||||||
params.put("foo", "SETVALUE");
|
params.put("foo", "SETVALUE");
|
||||||
client().prepareUpdate("test", "type", "1").setInlineScript("custom").setScriptLang("native").setScriptParams(params).get();
|
client().prepareUpdate("test", "type", "1")
|
||||||
|
.setScript("custom", ScriptService.ScriptType.INLINE)
|
||||||
|
.setScriptLang("native").setScriptParams(params).get();
|
||||||
|
|
||||||
Map<String, Object> data = client().prepareGet("test", "type", "1").get().getSource();
|
Map<String, Object> data = client().prepareGet("test", "type", "1").get().getSource();
|
||||||
assertThat(data, hasKey("foo"));
|
assertThat(data, hasKey("foo"));
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.elasticsearch.common.xcontent.XContentHelper;
|
||||||
import org.elasticsearch.index.VersionType;
|
import org.elasticsearch.index.VersionType;
|
||||||
import org.elasticsearch.index.engine.DocumentMissingException;
|
import org.elasticsearch.index.engine.DocumentMissingException;
|
||||||
import org.elasticsearch.index.engine.VersionConflictEngineException;
|
import org.elasticsearch.index.engine.VersionConflictEngineException;
|
||||||
|
import org.elasticsearch.script.ScriptService;
|
||||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -140,7 +141,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
||||||
.setInlineScript("ctx._source.field += 1")
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
assertTrue(updateResponse.isCreated());
|
assertTrue(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -151,7 +152,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1")
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
||||||
.setInlineScript("ctx._source.field += 1")
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
assertFalse(updateResponse.isCreated());
|
assertFalse(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -196,7 +197,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
||||||
.setInlineScript("ctx._source.extra = \"foo\"")
|
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE)
|
||||||
.setFields("_source")
|
.setFields("_source")
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
|
@ -206,7 +207,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1")
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
||||||
.setInlineScript("ctx._source.extra = \"foo\"")
|
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE)
|
||||||
.setFields("_source")
|
.setFields("_source")
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
|
@ -222,39 +223,46 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
index("test", "type", "1", "text", "value"); // version is now 1
|
index("test", "type", "1", "text", "value"); // version is now 1
|
||||||
|
|
||||||
assertThrows(client().prepareUpdate("test", "type", "1").setInlineScript("ctx._source.text = 'v2'").setVersion(2).execute(),
|
assertThrows(client().prepareUpdate("test", "type", "1")
|
||||||
|
.setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(2).execute(),
|
||||||
VersionConflictEngineException.class);
|
VersionConflictEngineException.class);
|
||||||
|
|
||||||
client().prepareUpdate("test", "type", "1").setInlineScript("ctx._source.text = 'v2'").setVersion(1).get();
|
client().prepareUpdate("test", "type", "1")
|
||||||
|
.setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(1).get();
|
||||||
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(2l));
|
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(2l));
|
||||||
|
|
||||||
// and again with a higher version..
|
// and again with a higher version..
|
||||||
client().prepareUpdate("test", "type", "1").setInlineScript("ctx._source.text = 'v3'").setVersion(2).get();
|
client().prepareUpdate("test", "type", "1")
|
||||||
|
.setScript("ctx._source.text = 'v3'", ScriptService.ScriptType.INLINE).setVersion(2).get();
|
||||||
|
|
||||||
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(3l));
|
assertThat(client().prepareGet("test", "type", "1").get().getVersion(), equalTo(3l));
|
||||||
|
|
||||||
// after delete
|
// after delete
|
||||||
client().prepareDelete("test", "type", "1").get();
|
client().prepareDelete("test", "type", "1").get();
|
||||||
assertThrows(client().prepareUpdate("test", "type", "1").setInlineScript("ctx._source.text = 'v2'").setVersion(3).execute(),
|
assertThrows(client().prepareUpdate("test", "type", "1")
|
||||||
|
.setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(3).execute(),
|
||||||
DocumentMissingException.class);
|
DocumentMissingException.class);
|
||||||
|
|
||||||
// external versioning
|
// external versioning
|
||||||
client().prepareIndex("test", "type", "2").setSource("text", "value").setVersion(10).setVersionType(VersionType.EXTERNAL).get();
|
client().prepareIndex("test", "type", "2").setSource("text", "value").setVersion(10).setVersionType(VersionType.EXTERNAL).get();
|
||||||
|
|
||||||
assertThrows(client().prepareUpdate("test", "type", "2").setInlineScript("ctx._source.text = 'v2'").setVersion(2).setVersionType(VersionType.EXTERNAL).execute(),
|
assertThrows(client().prepareUpdate("test", "type", "2")
|
||||||
|
.setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE).setVersion(2)
|
||||||
|
.setVersionType(VersionType.EXTERNAL).execute(),
|
||||||
ActionRequestValidationException.class);
|
ActionRequestValidationException.class);
|
||||||
|
|
||||||
// upserts - the combination with versions is a bit weird. Test are here to ensure we do not change our behavior unintentionally
|
// upserts - the combination with versions is a bit weird. Test are here to ensure we do not change our behavior unintentionally
|
||||||
|
|
||||||
// With internal versions, tt means "if object is there with version X, update it or explode. If it is not there, index.
|
// With internal versions, tt means "if object is there with version X, update it or explode. If it is not there, index.
|
||||||
client().prepareUpdate("test", "type", "3").setInlineScript("ctx._source.text = 'v2'").setVersion(10).setUpsert("{ \"text\": \"v0\" }").get();
|
client().prepareUpdate("test", "type", "3").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
|
||||||
|
.setVersion(10).setUpsert("{ \"text\": \"v0\" }").get();
|
||||||
GetResponse get = get("test", "type", "3");
|
GetResponse get = get("test", "type", "3");
|
||||||
assertThat(get.getVersion(), equalTo(1l));
|
assertThat(get.getVersion(), equalTo(1l));
|
||||||
assertThat((String) get.getSource().get("text"), equalTo("v0"));
|
assertThat((String) get.getSource().get("text"), equalTo("v0"));
|
||||||
|
|
||||||
// With force version
|
// With force version
|
||||||
client().prepareUpdate("test", "type", "4").setInlineScript("ctx._source.text = 'v2'").
|
client().prepareUpdate("test", "type", "4").setScript("ctx._source.text = 'v2'", ScriptService.ScriptType.INLINE)
|
||||||
setVersion(10).setVersionType(VersionType.FORCE).setUpsert("{ \"text\": \"v0\" }").get();
|
.setVersion(10).setVersionType(VersionType.FORCE).setUpsert("{ \"text\": \"v0\" }").get();
|
||||||
|
|
||||||
get = get("test", "type", "4");
|
get = get("test", "type", "4");
|
||||||
assertThat(get.getVersion(), equalTo(10l));
|
assertThat(get.getVersion(), equalTo(10l));
|
||||||
|
@ -271,7 +279,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
public void testIndexAutoCreation() throws Exception {
|
public void testIndexAutoCreation() throws Exception {
|
||||||
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
.setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
|
||||||
.setInlineScript("ctx._source.extra = \"foo\"")
|
.setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE)
|
||||||
.setFields("_source")
|
.setFields("_source")
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
||||||
|
@ -286,7 +294,8 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
ensureGreen();
|
ensureGreen();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client().prepareUpdate("test", "type1", "1").setInlineScript("ctx._source.field++").execute().actionGet();
|
client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx._source.field++", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
fail();
|
fail();
|
||||||
} catch (DocumentMissingException e) {
|
} catch (DocumentMissingException e) {
|
||||||
// all is well
|
// all is well
|
||||||
|
@ -294,7 +303,8 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
|
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
|
||||||
|
|
||||||
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1").setInlineScript("ctx._source.field += 1").execute().actionGet();
|
UpdateResponse updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
assertThat(updateResponse.getVersion(), equalTo(2L));
|
assertThat(updateResponse.getVersion(), equalTo(2L));
|
||||||
assertFalse(updateResponse.isCreated());
|
assertFalse(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -303,7 +313,9 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
|
assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1").setInlineScript("ctx._source.field += count").addScriptParam("count", 3).execute().actionGet();
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx._source.field += count", ScriptService.ScriptType.INLINE)
|
||||||
|
.addScriptParam("count", 3).execute().actionGet();
|
||||||
assertThat(updateResponse.getVersion(), equalTo(3L));
|
assertThat(updateResponse.getVersion(), equalTo(3L));
|
||||||
assertFalse(updateResponse.isCreated());
|
assertFalse(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -313,7 +325,8 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check noop
|
// check noop
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1").setInlineScript("ctx.op = 'none'").execute().actionGet();
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx.op = 'none'", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
assertThat(updateResponse.getVersion(), equalTo(3L));
|
assertThat(updateResponse.getVersion(), equalTo(3L));
|
||||||
assertFalse(updateResponse.isCreated());
|
assertFalse(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -323,7 +336,8 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check delete
|
// check delete
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1").setInlineScript("ctx.op = 'delete'").execute().actionGet();
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx.op = 'delete'", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
assertThat(updateResponse.getVersion(), equalTo(4L));
|
assertThat(updateResponse.getVersion(), equalTo(4L));
|
||||||
assertFalse(updateResponse.isCreated());
|
assertFalse(updateResponse.isCreated());
|
||||||
|
|
||||||
|
@ -337,13 +351,13 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
GetResponse getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
GetResponse getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
||||||
long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
long ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
||||||
assertThat(ttl, greaterThan(0L));
|
assertThat(ttl, greaterThan(0L));
|
||||||
client().prepareUpdate("test", "type1", "2").setInlineScript("ctx._source.field += 1").execute().actionGet();
|
client().prepareUpdate("test", "type1", "2").setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
||||||
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
||||||
assertThat(ttl, greaterThan(0L));
|
assertThat(ttl, greaterThan(0L));
|
||||||
|
|
||||||
// check TTL update
|
// check TTL update
|
||||||
client().prepareUpdate("test", "type1", "2").setInlineScript("ctx._ttl = 3600000").execute().actionGet();
|
client().prepareUpdate("test", "type1", "2").setScript("ctx._ttl = 3600000", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
getResponse = client().prepareGet("test", "type1", "2").setFields("_ttl").execute().actionGet();
|
||||||
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
ttl = ((Number) getResponse.getField("_ttl").getValue()).longValue();
|
||||||
assertThat(ttl, greaterThan(0L));
|
assertThat(ttl, greaterThan(0L));
|
||||||
|
@ -351,14 +365,16 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
// check timestamp update
|
// check timestamp update
|
||||||
client().prepareIndex("test", "type1", "3").setSource("field", 1).setRefresh(true).execute().actionGet();
|
client().prepareIndex("test", "type1", "3").setSource("field", 1).setRefresh(true).execute().actionGet();
|
||||||
client().prepareUpdate("test", "type1", "3").setInlineScript("ctx._timestamp = \"2009-11-15T14:12:12\"").execute().actionGet();
|
client().prepareUpdate("test", "type1", "3")
|
||||||
|
.setScript("ctx._timestamp = \"2009-11-15T14:12:12\"", ScriptService.ScriptType.INLINE).execute().actionGet();
|
||||||
getResponse = client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet();
|
getResponse = client().prepareGet("test", "type1", "3").setFields("_timestamp").execute().actionGet();
|
||||||
long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue();
|
long timestamp = ((Number) getResponse.getField("_timestamp").getValue()).longValue();
|
||||||
assertThat(timestamp, equalTo(1258294332000L));
|
assertThat(timestamp, equalTo(1258294332000L));
|
||||||
|
|
||||||
// check fields parameter
|
// check fields parameter
|
||||||
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
|
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
|
||||||
updateResponse = client().prepareUpdate("test", "type1", "1").setInlineScript("ctx._source.field += 1").setFields("_source", "field").execute().actionGet();
|
updateResponse = client().prepareUpdate("test", "type1", "1")
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE).setFields("_source", "field").execute().actionGet();
|
||||||
assertThat(updateResponse.getGetResult(), notNullValue());
|
assertThat(updateResponse.getGetResult(), notNullValue());
|
||||||
assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
|
assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
|
||||||
assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());
|
assertThat(updateResponse.getGetResult().field("field").getValue(), notNullValue());
|
||||||
|
@ -416,7 +432,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
try {
|
try {
|
||||||
client().prepareUpdate("test", "type1", "1")
|
client().prepareUpdate("test", "type1", "1")
|
||||||
.setDoc(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
.setDoc(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
|
||||||
.setInlineScript("ctx._source.field += 1")
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
fail("Should have thrown ActionRequestValidationException");
|
fail("Should have thrown ActionRequestValidationException");
|
||||||
} catch (ActionRequestValidationException e) {
|
} catch (ActionRequestValidationException e) {
|
||||||
|
@ -432,7 +448,7 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
ensureGreen();
|
ensureGreen();
|
||||||
try {
|
try {
|
||||||
client().prepareUpdate("test", "type1", "1")
|
client().prepareUpdate("test", "type1", "1")
|
||||||
.setInlineScript("ctx._source.field += 1")
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.setDocAsUpsert(true)
|
.setDocAsUpsert(true)
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
fail("Should have thrown ActionRequestValidationException");
|
fail("Should have thrown ActionRequestValidationException");
|
||||||
|
@ -465,12 +481,13 @@ public class UpdateTests extends ElasticsearchIntegrationTest {
|
||||||
for (int i = 0; i < numberOfUpdatesPerThread; i++) {
|
for (int i = 0; i < numberOfUpdatesPerThread; i++) {
|
||||||
if (useBulkApi) {
|
if (useBulkApi) {
|
||||||
UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate("test", "type1", Integer.toString(i))
|
UpdateRequestBuilder updateRequestBuilder = client().prepareUpdate("test", "type1", Integer.toString(i))
|
||||||
.setInlineScript("ctx._source.field += 1")
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.setRetryOnConflict(Integer.MAX_VALUE)
|
.setRetryOnConflict(Integer.MAX_VALUE)
|
||||||
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject());
|
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject());
|
||||||
client().prepareBulk().add(updateRequestBuilder).execute().actionGet();
|
client().prepareBulk().add(updateRequestBuilder).execute().actionGet();
|
||||||
} else {
|
} else {
|
||||||
client().prepareUpdate("test", "type1", Integer.toString(i)).setInlineScript("ctx._source.field += 1")
|
client().prepareUpdate("test", "type1", Integer.toString(i))
|
||||||
|
.setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
|
||||||
.setRetryOnConflict(Integer.MAX_VALUE)
|
.setRetryOnConflict(Integer.MAX_VALUE)
|
||||||
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject())
|
.setUpsert(jsonBuilder().startObject().field("field", 1).endObject())
|
||||||
.execute().actionGet();
|
.execute().actionGet();
|
||||||
|
|
Loading…
Reference in New Issue