Java Code Examples for org.elasticsearch.action.update.UpdateRequest#upsertRequest()

The following examples show how to use org.elasticsearch.action.update.UpdateRequest#upsertRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchChannel.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static String toString(UpdateRequest request) {
  String res = "update {[" + request.index() + "][" + request.type() + "][" + request.id() + "]";
  if (request.docAsUpsert()) {
    res += (", doc_as_upsert[" + request.docAsUpsert() + "]");
  }
  if (request.doc() != null) {
    res += (", doc[" + request.doc() + "]");
  }
  if (request.script() != null) {
    res += (", script[" + request.script() + "]");
  }
  if (request.upsertRequest() != null) {
    res += (", upsert[" + request.upsertRequest() + "]");
  }
  if (request.scriptedUpsert()) {
    res += (", scripted_upsert[" + request.scriptedUpsert() + "]");
  }
  if (request.detectNoop()) {
    res += (", detect_noop[" + request.detectNoop() + "]");
  }
  if (request.fields() != null) {
    res += (", fields[" + Arrays.toString(request.fields()) + "]");
  }
  return res + "}";
}
 
Example 2
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: ElasticsearchRequestUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static int getSizeOfUpdateRequest(UpdateRequest updateRequest) {
    int sizeInBytes = 0;
    if (updateRequest.doc() != null) {
        sizeInBytes += updateRequest.doc().source().length();
    }
    if (updateRequest.upsertRequest() != null) {
        sizeInBytes += updateRequest.upsertRequest().source().length();
    }
    if (updateRequest.script() != null) {
        sizeInBytes += updateRequest.script().getIdOrCode().length() * 2;
    }
    return sizeInBytes;
}
 
Example 4
Source File: ElasticsearchRequestUtils.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static int getSizeOfUpdateRequest(UpdateRequest updateRequest) {
    int sizeInBytes = 0;
    if (updateRequest.doc() != null) {
        sizeInBytes += updateRequest.doc().source().length();
    }
    if (updateRequest.upsertRequest() != null) {
        sizeInBytes += updateRequest.upsertRequest().source().length();
    }
    if (updateRequest.script() != null) {
        sizeInBytes += updateRequest.script().getIdOrCode().length() * 2;
    }
    return sizeInBytes;
}
 
Example 5
Source File: RestUpdateAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    UpdateRequest updateRequest = new UpdateRequest(request.param("index"), request.param("type"), request.param("id"));
    updateRequest.routing(request.param("routing"));
    updateRequest.parent(request.param("parent"));
    updateRequest.timeout(request.paramAsTime("timeout", updateRequest.timeout()));
    updateRequest.refresh(request.paramAsBoolean("refresh", updateRequest.refresh()));
    String consistencyLevel = request.param("consistency");
    if (consistencyLevel != null) {
        updateRequest.consistencyLevel(WriteConsistencyLevel.fromString(consistencyLevel));
    }
    updateRequest.docAsUpsert(request.paramAsBoolean("doc_as_upsert", updateRequest.docAsUpsert()));
    ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
    scriptParameterParser.parseParams(request);
    ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
    if (scriptValue != null) {
        Map<String, Object> scriptParams = new HashMap<>();
        for (Map.Entry<String, String> entry : request.params().entrySet()) {
            if (entry.getKey().startsWith("sp_")) {
                scriptParams.put(entry.getKey().substring(3), entry.getValue());
            }
        }
        updateRequest.script(new Script(scriptValue.script(), scriptValue.scriptType(), scriptParameterParser.lang(), scriptParams));
    }
    String sField = request.param("fields");
    if (sField != null) {
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        if (sFields != null) {
            updateRequest.fields(sFields);
        }
    }
    updateRequest.retryOnConflict(request.paramAsInt("retry_on_conflict", updateRequest.retryOnConflict()));
    updateRequest.version(RestActions.parseVersion(request));
    updateRequest.versionType(VersionType.fromString(request.param("version_type"), updateRequest.versionType()));


    // see if we have it in the body
    if (request.hasContent()) {
        updateRequest.source(request.content());
        IndexRequest upsertRequest = updateRequest.upsertRequest();
        if (upsertRequest != null) {
            upsertRequest.routing(request.param("routing"));
            upsertRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing
            upsertRequest.timestamp(request.param("timestamp"));
            if (request.hasParam("ttl")) {
                upsertRequest.ttl(request.param("ttl"));
            }
            upsertRequest.version(RestActions.parseVersion(request));
            upsertRequest.versionType(VersionType.fromString(request.param("version_type"), upsertRequest.versionType()));
        }
        IndexRequest doc = updateRequest.doc();
        if (doc != null) {
            doc.routing(request.param("routing"));
            doc.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing
            doc.timestamp(request.param("timestamp"));
            if (request.hasParam("ttl")) {
                doc.ttl(request.param("ttl"));
            }
            doc.version(RestActions.parseVersion(request));
            doc.versionType(VersionType.fromString(request.param("version_type"), doc.versionType()));
        }
    }

    client.update(updateRequest, new RestBuilderListener<UpdateResponse>(channel) {
        @Override
        public RestResponse buildResponse(UpdateResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            ActionWriteResponse.ShardInfo shardInfo = response.getShardInfo();
            builder.field(Fields._INDEX, response.getIndex())
                    .field(Fields._TYPE, response.getType())
                    .field(Fields._ID, response.getId())
                    .field(Fields._VERSION, response.getVersion());

            shardInfo.toXContent(builder, request);
            if (response.getGetResult() != null) {
                builder.startObject(Fields.GET);
                response.getGetResult().toXContentEmbedded(builder, request);
                builder.endObject();
            }

            builder.endObject();
            RestStatus status = shardInfo.status();
            if (response.isCreated()) {
                status = CREATED;
            }
            return new BytesRestResponse(status, builder);
        }
    });
}