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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations