Java Code Examples for org.elasticsearch.action.index.IndexRequest#ttl()

The following examples show how to use org.elasticsearch.action.index.IndexRequest#ttl() . 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: 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);
        }
    });
}
 
Example 2
Source File: HttpBulkAction.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
@Override
protected HttpRequest createHttpRequest(URL base, BulkRequest request) {
    StringBuilder bulkContent = new StringBuilder();
    for (ActionRequest actionRequest : request.requests()) {
        if (actionRequest instanceof IndexRequest) {
            IndexRequest indexRequest = (IndexRequest) actionRequest;
            bulkContent.append("{\"").append(indexRequest.opType().lowercase()).append("\":{");
            bulkContent.append("\"_index\":\"").append(indexRequest.index()).append("\"");
            bulkContent.append(",\"_type\":\"").append(indexRequest.type()).append("\"");
            if (indexRequest.id() != null) {
                bulkContent.append(",\"_id\":\"").append(indexRequest.id()).append("\"");
            }
            if (indexRequest.routing() != null) {
                bulkContent.append(",\"_routing\":\"").append(indexRequest.routing()).append("\""); // _routing
            }
            if (indexRequest.parent() != null) {
                bulkContent.append(",\"_parent\":\"").append(indexRequest.parent()).append("\"");
            }
            if (indexRequest.timestamp() != null) {
                bulkContent.append(",\"_timestamp\":\"").append(indexRequest.timestamp()).append("\"");
            }
            // avoid _ttl <= 0 at all cost!
            if (indexRequest.ttl() != null && indexRequest.ttl().seconds() > 0) {
                bulkContent.append(",\"_ttl\":\"").append(indexRequest.ttl()).append("\"");
            }
            if (indexRequest.version() > 0) {
                bulkContent.append(",\"_version\":\"").append(indexRequest.version()).append("\"");
                if (indexRequest.versionType() != null) {
                    bulkContent.append(",\"_version_type\":\"").append(indexRequest.versionType().name()).append("\"");
                }
            }
            bulkContent.append("}}\n");
            bulkContent.append(indexRequest.source().toUtf8());
            bulkContent.append("\n");
        } else if (actionRequest instanceof DeleteRequest) {
            DeleteRequest deleteRequest = (DeleteRequest) actionRequest;
            bulkContent.append("{\"delete\":{");
            bulkContent.append("\"_index\":\"").append(deleteRequest.index()).append("\"");
            bulkContent.append(",\"_type\":\"").append(deleteRequest.type()).append("\"");
            bulkContent.append(",\"_id\":\"").append(deleteRequest.id()).append("\"");
            if (deleteRequest.routing() != null) {
                bulkContent.append(",\"_routing\":\"").append(deleteRequest.routing()).append("\""); // _routing
            }
            bulkContent.append("}}\n");
        }
    }
    return newPostRequest(base, "/_bulk", bulkContent);
}