use of org.elasticsearch.index.get.GetResult in project elasticsearch by elastic.
the class UpdateHelper method prepare.
/**
* Prepares an update request by converting it into an index or delete request or an update response (no action).
*/
@SuppressWarnings("unchecked")
protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult getResult, LongSupplier nowInMillis) {
if (!getResult.isExists()) {
if (request.upsertRequest() == null && !request.docAsUpsert()) {
throw new DocumentMissingException(shardId, request.type(), request.id());
}
IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest();
if (request.scriptedUpsert() && request.script() != null) {
// Run the script to perform the create logic
IndexRequest upsert = request.upsertRequest();
Map<String, Object> upsertDoc = upsert.sourceAsMap();
Map<String, Object> ctx = new HashMap<>(2);
// Tell the script that this is a create and not an update
ctx.put("op", "create");
ctx.put("_source", upsertDoc);
ctx.put("_now", nowInMillis.getAsLong());
ctx = executeScript(request.script, ctx);
//Allow the script to abort the create by setting "op" to "none"
String scriptOpChoice = (String) ctx.get("op");
// (the default) or "none", meaning abort upsert
if (!"create".equals(scriptOpChoice)) {
if (!"none".equals(scriptOpChoice)) {
logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice, request.script.getIdOrCode());
}
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
update.setGetResult(getResult);
return new Result(update, DocWriteResponse.Result.NOOP, upsertDoc, XContentType.JSON);
}
indexRequest.source((Map) ctx.get("_source"));
}
indexRequest.index(request.index()).type(request.type()).id(request.id()).create(true).setRefreshPolicy(request.getRefreshPolicy()).routing(request.routing()).parent(request.parent()).waitForActiveShards(request.waitForActiveShards());
if (request.versionType() != VersionType.INTERNAL) {
// in all but the internal versioning mode, we want to create the new document using the given version.
indexRequest.version(request.version()).versionType(request.versionType());
}
return new Result(indexRequest, DocWriteResponse.Result.CREATED, null, null);
}
long updateVersion = getResult.getVersion();
if (request.versionType() != VersionType.INTERNAL) {
assert request.versionType() == VersionType.FORCE;
// remember, match_any is excluded by the conflict test
updateVersion = request.version();
}
if (getResult.internalSourceRef() == null) {
// no source, we can't do nothing, through a failure...
throw new DocumentSourceMissingException(shardId, request.type(), request.id());
}
Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
String operation = null;
final Map<String, Object> updatedSourceAsMap;
final XContentType updateSourceContentType = sourceAndContent.v1();
String routing = getResult.getFields().containsKey(RoutingFieldMapper.NAME) ? getResult.field(RoutingFieldMapper.NAME).getValue().toString() : null;
String parent = getResult.getFields().containsKey(ParentFieldMapper.NAME) ? getResult.field(ParentFieldMapper.NAME).getValue().toString() : null;
if (request.script() == null && request.doc() != null) {
IndexRequest indexRequest = request.doc();
updatedSourceAsMap = sourceAndContent.v2();
if (indexRequest.routing() != null) {
routing = indexRequest.routing();
}
if (indexRequest.parent() != null) {
parent = indexRequest.parent();
}
boolean noop = !XContentHelper.update(updatedSourceAsMap, indexRequest.sourceAsMap(), request.detectNoop());
// cases where users repopulating multi-fields or adding synonyms, etc.
if (request.detectNoop() && noop) {
operation = "none";
}
} else {
Map<String, Object> ctx = new HashMap<>(16);
ctx.put("_index", getResult.getIndex());
ctx.put("_type", getResult.getType());
ctx.put("_id", getResult.getId());
ctx.put("_version", getResult.getVersion());
ctx.put("_routing", routing);
ctx.put("_parent", parent);
ctx.put("_source", sourceAndContent.v2());
ctx.put("_now", nowInMillis.getAsLong());
ctx = executeScript(request.script, ctx);
operation = (String) ctx.get("op");
updatedSourceAsMap = (Map<String, Object>) ctx.get("_source");
}
if (operation == null || "index".equals(operation)) {
final IndexRequest indexRequest = Requests.indexRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
return new Result(indexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType);
} else if ("delete".equals(operation)) {
DeleteRequest deleteRequest = Requests.deleteRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
return new Result(deleteRequest, DocWriteResponse.Result.DELETED, updatedSourceAsMap, updateSourceContentType);
} else if ("none".equals(operation)) {
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
update.setGetResult(extractGetResult(request, request.index(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
} else {
logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script.getIdOrCode());
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
}
}
use of org.elasticsearch.index.get.GetResult in project elasticsearch by elastic.
the class GetResponseTests method testToAndFromXContent.
public void testToAndFromXContent() throws Exception {
XContentType xContentType = randomFrom(XContentType.values());
Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
GetResponse getResponse = new GetResponse(tuple.v1());
GetResponse expectedGetResponse = new GetResponse(tuple.v2());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(getResponse, xContentType, humanReadable);
//test that we can parse what we print out
GetResponse parsedGetResponse;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsedGetResponse = GetResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(expectedGetResponse, parsedGetResponse);
//print the parsed object out and test that the output is the same as the original output
BytesReference finalBytes = toXContent(parsedGetResponse, xContentType, humanReadable);
assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
//check that the source stays unchanged, no shuffling of keys nor anything like that
assertEquals(expectedGetResponse.getSourceAsString(), parsedGetResponse.getSourceAsString());
}
use of org.elasticsearch.index.get.GetResult in project elasticsearch by elastic.
the class PercolateQueryBuilderTests method executeGet.
@Override
protected GetResponse executeGet(GetRequest getRequest) {
assertThat(getRequest.index(), Matchers.equalTo(indexedDocumentIndex));
assertThat(getRequest.type(), Matchers.equalTo(indexedDocumentType));
assertThat(getRequest.id(), Matchers.equalTo(indexedDocumentId));
assertThat(getRequest.routing(), Matchers.equalTo(indexedDocumentRouting));
assertThat(getRequest.preference(), Matchers.equalTo(indexedDocumentPreference));
assertThat(getRequest.version(), Matchers.equalTo(indexedDocumentVersion));
if (indexedDocumentExists) {
return new GetResponse(new GetResult(indexedDocumentIndex, indexedDocumentType, indexedDocumentId, 0L, true, documentSource, Collections.emptyMap()));
} else {
return new GetResponse(new GetResult(indexedDocumentIndex, indexedDocumentType, indexedDocumentId, -1, false, null, Collections.emptyMap()));
}
}
use of org.elasticsearch.index.get.GetResult in project elasticsearch by elastic.
the class UpdateRequestTests method testNowInScript.
public void testNowInScript() throws IOException {
Path genericConfigFolder = createTempDir();
Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
Environment environment = new Environment(baseSettings);
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
scripts.put("ctx._source.update_timestamp = ctx._now", (vars) -> {
Map<String, Object> vars2 = vars;
@SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars2.get("ctx");
@SuppressWarnings("unchecked") Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
source.put("update_timestamp", ctx.get("_now"));
return null;
});
scripts.put("ctx._timestamp = ctx._now", (vars) -> {
@SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars.get("ctx");
ctx.put("_timestamp", ctx.get("_now"));
return null;
});
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new MockScriptEngine("mock", scripts)));
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
ScriptService scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings);
Settings settings = settings(Version.CURRENT).build();
UpdateHelper updateHelper = new UpdateHelper(settings, scriptService);
// We just upsert one document with now() using a script
IndexRequest indexRequest = new IndexRequest("test", "type1", "2").source(jsonBuilder().startObject().field("foo", "bar").endObject());
{
UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._source.update_timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
long nowInMillis = randomNonNegativeLong();
// We simulate that the document is not existing yet
GetResult getResult = new GetResult("test", "type1", "2", 0, false, null, null);
UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> nowInMillis);
Streamable action = result.action();
assertThat(action, instanceOf(IndexRequest.class));
IndexRequest indexAction = (IndexRequest) action;
assertEquals(indexAction.sourceAsMap().get("update_timestamp"), nowInMillis);
}
{
UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
// We simulate that the document is not existing yet
GetResult getResult = new GetResult("test", "type1", "2", 0, true, new BytesArray("{}"), null);
UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> 42L);
Streamable action = result.action();
assertThat(action, instanceOf(IndexRequest.class));
}
}
use of org.elasticsearch.index.get.GetResult in project elasticsearch by elastic.
the class UpdateResponseTests method randomUpdateResponse.
/**
* Returns a tuple of {@link UpdateResponse}s.
* <p>
* The left element is the actual {@link UpdateResponse} to serialize while the right element is the
* expected {@link UpdateResponse} after parsing.
*/
public static Tuple<UpdateResponse, UpdateResponse> randomUpdateResponse(XContentType xContentType) {
Tuple<GetResult, GetResult> getResults = GetResultTests.randomGetResult(xContentType);
GetResult actualGetResult = getResults.v1();
GetResult expectedGetResult = getResults.v2();
String index = actualGetResult.getIndex();
String type = actualGetResult.getType();
String id = actualGetResult.getId();
long version = actualGetResult.getVersion();
DocWriteResponse.Result result = actualGetResult.isExists() ? DocWriteResponse.Result.UPDATED : DocWriteResponse.Result.NOT_FOUND;
String indexUUid = randomAsciiOfLength(5);
int shardId = randomIntBetween(0, 5);
// We also want small number values (randomNonNegativeLong() tend to generate high numbers)
// in order to catch some conversion error that happen between int/long after parsing.
Long seqNo = randomFrom(randomNonNegativeLong(), (long) randomIntBetween(0, 10_000), null);
ShardId actualShardId = new ShardId(index, indexUUid, shardId);
ShardId expectedShardId = new ShardId(index, INDEX_UUID_NA_VALUE, -1);
UpdateResponse actual, expected;
if (seqNo != null) {
Tuple<ReplicationResponse.ShardInfo, ReplicationResponse.ShardInfo> shardInfos = RandomObjects.randomShardInfo(random());
actual = new UpdateResponse(shardInfos.v1(), actualShardId, type, id, seqNo, version, result);
expected = new UpdateResponse(shardInfos.v2(), expectedShardId, type, id, seqNo, version, result);
} else {
actual = new UpdateResponse(actualShardId, type, id, version, result);
expected = new UpdateResponse(expectedShardId, type, id, version, result);
}
if (actualGetResult.isExists()) {
actual.setGetResult(actualGetResult);
}
if (expectedGetResult.isExists()) {
expected.setGetResult(expectedGetResult);
}
boolean forcedRefresh = randomBoolean();
actual.setForcedRefresh(forcedRefresh);
expected.setForcedRefresh(forcedRefresh);
return Tuple.tuple(actual, expected);
}
Aggregations