Search in sources :

Example 6 with ElasticsearchGenerationException

use of org.elasticsearch.ElasticsearchGenerationException in project elasticsearch by elastic.

the class UpdateSettingsRequest method settings.

/**
     * Sets the settings to be updated (either json or yaml format)
     */
@SuppressWarnings("unchecked")
public UpdateSettingsRequest settings(Map source) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.map(source);
        settings(builder.string(), builder.contentType());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
    return this;
}
Also used : IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 7 with ElasticsearchGenerationException

use of org.elasticsearch.ElasticsearchGenerationException in project elasticsearch by elastic.

the class IndexRequest method source.

/**
     * Sets the content source to index.
     * <p>
     * <b>Note: the number of objects passed to this method as varargs must be an even
     * number. Also the first argument in each pair (the field name) must have a
     * valid String representation.</b>
     * </p>
     */
public IndexRequest source(XContentType xContentType, Object... source) {
    if (source.length % 2 != 0) {
        throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
    }
    if (source.length == 2 && source[0] instanceof BytesReference && source[1] instanceof Boolean) {
        throw new IllegalArgumentException("you are using the removed method for source with bytes and unsafe flag, the unsafe flag was removed, please just use source(BytesReference)");
    }
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
        builder.startObject();
        for (int i = 0; i < source.length; i++) {
            builder.field(source[i++].toString(), source[i]);
        }
        builder.endObject();
        return source(builder);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate", e);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 8 with ElasticsearchGenerationException

use of org.elasticsearch.ElasticsearchGenerationException in project crate by crate.

the class TransportShardUpsertAction method prepareUpdate.

/**
     * Prepares an update request by converting it into an index request.
     * <p/>
     * TODO: detect a NOOP and return an update response if true
     */
@SuppressWarnings("unchecked")
private SourceAndVersion prepareUpdate(DocTableInfo tableInfo, ShardUpsertRequest request, ShardUpsertRequest.Item item, IndexShard indexShard) throws ElasticsearchException {
    final GetResult getResult = indexShard.getService().get(request.type(), item.id(), new String[] { RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME }, true, Versions.MATCH_ANY, VersionType.INTERNAL, FetchSourceContext.FETCH_SOURCE, false);
    if (!getResult.isExists()) {
        throw new DocumentMissingException(request.shardId(), request.type(), item.id());
    }
    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(request.shardId(), request.type(), item.id());
    }
    if (item.version() != Versions.MATCH_ANY && item.version() != getResult.getVersion()) {
        throw new VersionConflictEngineException(indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, item.id(), getResult.getVersion(), item.version());
    }
    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();
    updatedSourceAsMap = sourceAndContent.v2();
    SymbolToFieldExtractorContext ctx = new SymbolToFieldExtractorContext(functions, item.insertValues());
    Map<String, Object> pathsToUpdate = new LinkedHashMap<>();
    Map<String, Object> updatedGeneratedColumns = new LinkedHashMap<>();
    for (int i = 0; i < request.updateColumns().length; i++) {
        /*
             * NOTE: mapping isn't applied. So if an Insert was done using the ES Rest Endpoint
             * the data might be returned in the wrong format (date as string instead of long)
             */
        String columnPath = request.updateColumns()[i];
        Object value = SYMBOL_TO_FIELD_EXTRACTOR.convert(item.updateAssignments()[i], ctx).apply(getResult);
        Reference reference = tableInfo.getReference(ColumnIdent.fromPath(columnPath));
        if (reference != null) {
            /*
                 * it is possible to insert NULL into column that does not exist yet.
                 * if there is no column reference, we must not validate!
                 */
            ConstraintsValidator.validate(value, reference);
        }
        if (reference instanceof GeneratedReference) {
            updatedGeneratedColumns.put(columnPath, value);
        } else {
            pathsToUpdate.put(columnPath, value);
        }
    }
    // For updates we always have to enforce the validation of constraints on shards.
    // Currently the validation is done only for generated columns.
    processGeneratedColumns(tableInfo, pathsToUpdate, updatedGeneratedColumns, true, getResult);
    updateSourceByPaths(updatedSourceAsMap, pathsToUpdate);
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(updateSourceContentType);
        builder.map(updatedSourceAsMap);
        return new SourceAndVersion(builder.bytes(), getResult.getVersion());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + updatedSourceAsMap + "]", e);
    }
}
Also used : GetResult(org.elasticsearch.index.get.GetResult) BytesReference(org.elasticsearch.common.bytes.BytesReference) IOException(java.io.IOException) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 9 with ElasticsearchGenerationException

use of org.elasticsearch.ElasticsearchGenerationException in project elasticsearch by elastic.

the class IndexRequest method source.

/**
     * Index the Map as the provided content type.
     *
     * @param source The map to index
     */
public IndexRequest source(Map source, XContentType contentType) throws ElasticsearchGenerationException {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(contentType);
        builder.map(source);
        return source(builder);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
}
Also used : IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 10 with ElasticsearchGenerationException

use of org.elasticsearch.ElasticsearchGenerationException in project elasticsearch by elastic.

the class PutIndexTemplateRequest method aliases.

/**
     * Sets the aliases that will be associated with the index when it gets created
     */
@SuppressWarnings("unchecked")
public PutIndexTemplateRequest aliases(Map source) {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.map(source);
        return aliases(builder.bytes());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Aggregations

IOException (java.io.IOException)21 ElasticsearchGenerationException (org.elasticsearch.ElasticsearchGenerationException)21 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)19 UncheckedIOException (java.io.UncheckedIOException)7 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 FileContext (org.apache.hadoop.fs.FileContext)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 GetResult (org.elasticsearch.index.get.GetResult)1 InvalidTypeNameException (org.elasticsearch.indices.InvalidTypeNameException)1 TypeMissingException (org.elasticsearch.indices.TypeMissingException)1