private UpdateRequest prepareUpdate(UpdateQuery query) { String indexName = hasText(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName(); String type = hasText(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType(); Assert.notNull(indexName, "No index defined for Query"); Assert.notNull(type, "No type define for Query"); Assert.notNull(query.getId(), "No Id define for Query"); Assert.notNull(query.getUpdateRequest(), "No IndexRequest define for Query"); UpdateRequest updateRequest = new UpdateRequest(indexName, type, query.getId()); updateRequest.routing(query.getUpdateRequest().routing()); if (query.getUpdateRequest().script() == null) { // doc if (query.DoUpsert()) { updateRequest.docAsUpsert(true).doc(query.getUpdateRequest().doc()); } else { updateRequest.doc(query.getUpdateRequest().doc()); } } else { // or script updateRequest.script(query.getUpdateRequest().script()); } return updateRequest; }
public void scriptedUpsert(String key, String parent, String scriptSource, Map<String, ? extends Object> parameters) { Script script = new Script(scriptSource, ScriptType.INLINE, null, parameters); UpdateRequest request = new UpdateRequest(index, type, key); request.scriptedUpsert(true); request.script(script); request.upsert("{}"); // <--- The change if (parent != null) { request.parent(parent); } this.bulkProcessor.add(request); }
/** * The 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>. * */ public UpdateRequestBuilder setScript(Script script) { request.script(script); return this; }
if (request.scriptedUpsert() && request.script() != null) {
/** * Add a script parameter. * * @deprecated Use {@link #script(Script)} instead */ @Deprecated public UpdateRequest addScriptParam(String name, Object value) { Script script = script(); if (script == null) { HashMap<String, Object> scriptParams = new HashMap<>(); scriptParams.put(name, value); updateOrCreateScript(null, null, null, scriptParams); } else { Map<String, Object> scriptParams = script.getParams(); if (scriptParams == null) { scriptParams = new HashMap<>(); scriptParams.put(name, value); updateOrCreateScript(null, null, null, scriptParams); } else { scriptParams.put(name, value); } } return this; }
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) { Script script = script(); if (script == null) { script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params); } else { String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent; ScriptType newScriptType = type == null ? script.getType() : type; String newScriptLang = lang == null ? script.getLang() : lang; Map<String, Object> newScriptParams = params == null ? script.getParams() : params; script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams); } script(script); }
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) { Objects.requireNonNull(request, "'request' must not be null"); applyGlobalMandatoryParameters(request); requests.add(request); addPayload(payload); if (request.doc() != null) { sizeInBytes += request.doc().source().length(); } if (request.upsertRequest() != null) { sizeInBytes += request.upsertRequest().source().length(); } if (request.script() != null) { sizeInBytes += request.script().getIdOrCode().length() * 2; } indices.add(request.index()); return this; }
/** * The 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>. * */ public UpdateRequestBuilder setScript(Script script) { request.script(script); return this; }
/** * The 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>. * */ public UpdateRequestBuilder setScript(Script script) { request.script(script); return this; }
/** * The 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>. * */ public UpdateRequestBuilder setScript(Script script) { request.script(script); return this; }
/** * The 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>. * */ public UpdateRequestBuilder setScript(Script script) { request.script(script); return this; }
public UpdateRequestBuilder<JsonInput, JsonOutput> script(String script) { request.script(script); return this; }
/** * Prepares an update request by converting it into an index or delete request or an update response (no action, in the event of a * noop). */ @SuppressWarnings("unchecked") protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult getResult, LongSupplier nowInMillis) { if (getResult.isExists() == false) { // If the document didn't exist, execute the update request as an upsert return prepareUpsert(shardId, request, getResult, nowInMillis); } else if (getResult.internalSourceRef() == null) { // no source, we can't do anything, throw a failure... throw new DocumentSourceMissingException(shardId, request.type(), request.id()); } else if (request.script() == null && request.doc() != null) { // The request has no script, it is a new doc that should be merged with the old document return prepareUpdateIndexRequest(shardId, request, getResult, request.detectNoop()); } else { // The request has a script (or empty script), execute the script and prepare a new index request return prepareUpdateScriptRequest(shardId, request, getResult, nowInMillis); } }
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) { Script script = script(); if (script == null) { script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params); } else { String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent; ScriptType newScriptType = type == null ? script.getType() : type; String newScriptLang = lang == null ? script.getLang() : lang; Map<String, Object> newScriptParams = params == null ? script.getParams() : params; script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams); } script(script); }
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) { Script script = script(); if (script == null) { script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params); } else { String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent; ScriptType newScriptType = type == null ? script.getType() : type; String newScriptLang = lang == null ? script.getLang() : lang; Map<String, Object> newScriptParams = params == null ? script.getParams() : params; script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams); } script(script); }
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) { Script script = script(); if (script == null) { script = new Script(scriptContent == null ? "" : scriptContent, type == null ? ScriptType.INLINE : type, lang, params); } else { String newScriptContent = scriptContent == null ? script.getScript() : scriptContent; ScriptType newScriptType = type == null ? script.getType() : type; String newScriptLang = lang == null ? script.getLang() : lang; Map<String, Object> newScriptParams = params == null ? script.getParams() : params; script = new Script(newScriptContent, newScriptType, newScriptLang, newScriptParams); } script(script); }
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) { Script script = script(); if (script == null) { script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params); } else { String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent; ScriptType newScriptType = type == null ? script.getType() : type; String newScriptLang = lang == null ? script.getLang() : lang; Map<String, Object> newScriptParams = params == null ? script.getParams() : params; script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams); } script(script); }
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) { Objects.requireNonNull(request, "'request' must not be null"); requests.add(request); addPayload(payload); if (request.doc() != null) { sizeInBytes += request.doc().source().length(); } if (request.upsertRequest() != null) { sizeInBytes += request.upsertRequest().source().length(); } if (request.script() != null) { sizeInBytes += request.script().getIdOrCode().length() * 2; } return this; }
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) { Objects.requireNonNull(request, "'request' must not be null"); requests.add(request); addPayload(payload); if (request.doc() != null) { sizeInBytes += request.doc().source().length(); } if (request.upsertRequest() != null) { sizeInBytes += request.upsertRequest().source().length(); } if (request.script() != null) { sizeInBytes += request.script().getIdOrCode().length() * 2; } indices.add(request.index()); return this; }
BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) { requests.add(request); addPayload(payload); if (request.doc() != null) { sizeInBytes += request.doc().source().length(); } if (request.upsertRequest() != null) { sizeInBytes += request.upsertRequest().source().length(); } if (request.script() != null) { sizeInBytes += request.script().getScript().length() * 2; } return this; }