use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testBrokenMapping.
public void testBrokenMapping() throws Exception {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// check get all templates on an empty index.
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), empty());
MapperParsingException e = expectThrows(MapperParsingException.class, () -> client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addMapping("type1", "{\"foo\": \"abcde\"}", XContentType.JSON).get());
assertThat(e.getMessage(), containsString("Failed to parse mapping "));
response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), hasSize(0));
}
use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class TermVectorsUnitTests method testFieldTypeToTermVectorString.
public void testFieldTypeToTermVectorString() throws Exception {
FieldType ft = new FieldType();
ft.setStoreTermVectorOffsets(false);
ft.setStoreTermVectorPayloads(true);
ft.setStoreTermVectors(true);
ft.setStoreTermVectorPositions(true);
String ftOpts = FieldMapper.termVectorOptionsToString(ft);
assertThat("with_positions_payloads", equalTo(ftOpts));
AllFieldMapper.Builder builder = new AllFieldMapper.Builder(null);
boolean exceptiontrown = false;
try {
TypeParsers.parseTermVector("", ftOpts, builder);
} catch (MapperParsingException e) {
exceptiontrown = true;
}
assertThat("TypeParsers.parseTermVector should accept string with_positions_payloads but does not.", exceptiontrown, equalTo(false));
}
use of org.elasticsearch.index.mapper.MapperParsingException in project crate by crate.
the class SQLExceptions method createSQLActionException.
/**
* Create a {@link SQLActionException} out of a {@link Throwable}.
* If concrete {@link ElasticsearchException} is found, first transform it
* to a {@link CrateException}
*/
public static SQLActionException createSQLActionException(Throwable e) {
if (e instanceof SQLActionException) {
return (SQLActionException) e;
}
e = esToCrateException(e);
int errorCode = 5000;
RestStatus restStatus = RestStatus.INTERNAL_SERVER_ERROR;
if (e instanceof CrateException) {
CrateException crateException = (CrateException) e;
if (e instanceof ValidationException) {
errorCode = 4000 + crateException.errorCode();
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof ReadOnlyException) {
errorCode = 4030 + crateException.errorCode();
restStatus = RestStatus.FORBIDDEN;
} else if (e instanceof ResourceUnknownException) {
errorCode = 4040 + crateException.errorCode();
restStatus = RestStatus.NOT_FOUND;
} else if (e instanceof ConflictException) {
errorCode = 4090 + crateException.errorCode();
restStatus = RestStatus.CONFLICT;
} else if (e instanceof UnhandledServerException) {
errorCode = 5000 + crateException.errorCode();
}
} else if (e instanceof ParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof MapperParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
}
String message = e.getMessage();
if (message == null) {
if (e instanceof CrateException && e.getCause() != null) {
// use cause because it contains a more meaningful error in most cases
e = e.getCause();
}
StackTraceElement[] stackTraceElements = e.getStackTrace();
if (stackTraceElements.length > 0) {
message = String.format(Locale.ENGLISH, "%s in %s", e.getClass().getSimpleName(), stackTraceElements[0]);
} else {
message = "Error in " + e.getClass().getSimpleName();
}
} else {
message = e.getClass().getSimpleName() + ": " + message;
}
return new SQLActionException(message, errorCode, restStatus, e.getStackTrace());
}
use of org.elasticsearch.index.mapper.MapperParsingException in project crate by crate.
the class TransportCreatePartitionsAction method executeCreateIndices.
/**
* This code is more or less the same as the stuff in {@link MetadataCreateIndexService}
* but optimized for bulk operation without separate mapping/alias/index settings.
*/
private ClusterState executeCreateIndices(ClusterState currentState, CreatePartitionsRequest request) throws Exception {
List<String> indicesToCreate = new ArrayList<>(request.indices().size());
List<String> removalReasons = new ArrayList<>(request.indices().size());
List<Index> createdIndices = new ArrayList<>(request.indices().size());
try {
validateAndFilterExistingIndices(currentState, indicesToCreate, request);
if (indicesToCreate.isEmpty()) {
return currentState;
}
Map<String, Map<String, Object>> mappings = new HashMap<>();
Map<String, AliasMetadata> templatesAliases = new HashMap<>();
List<String> templateNames = new ArrayList<>();
List<IndexTemplateMetadata> templates = findTemplates(request, currentState);
applyTemplates(mappings, templatesAliases, templateNames, templates);
Metadata.Builder newMetadataBuilder = Metadata.builder(currentState.metadata());
for (String index : indicesToCreate) {
Settings indexSettings = createIndexSettings(currentState, templates);
shardLimitValidator.validateShardLimit(indexSettings, currentState);
int routingNumShards = IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings);
String testIndex = indicesToCreate.get(0);
IndexMetadata.Builder tmpImdBuilder = IndexMetadata.builder(testIndex).setRoutingNumShards(routingNumShards);
// Set up everything, now locally create the index to see that things are ok, and apply
final IndexMetadata tmpImd = tmpImdBuilder.settings(indexSettings).build();
ActiveShardCount waitForActiveShards = tmpImd.getWaitForActiveShards();
if (!waitForActiveShards.validate(tmpImd.getNumberOfReplicas())) {
throw new IllegalArgumentException("invalid wait_for_active_shards[" + waitForActiveShards + "]: cannot be greater than number of shard copies [" + (tmpImd.getNumberOfReplicas() + 1) + "]");
}
// create the index here (on the master) to validate it can be created, as well as adding the mapping
IndexService indexService = indicesService.createIndex(tmpImd, Collections.emptyList(), false);
createdIndices.add(indexService.index());
// now add the mappings
MapperService mapperService = indexService.mapperService();
if (!mappings.isEmpty()) {
assert mappings.size() == 1 : "Must have at most 1 mapping type";
var entry = mappings.entrySet().iterator().next();
try {
mapperService.merge(entry.getKey(), entry.getValue(), MapperService.MergeReason.MAPPING_UPDATE);
} catch (MapperParsingException mpe) {
removalReasons.add("failed on parsing mappings on index creation");
throw mpe;
}
}
// now, update the mappings with the actual source
HashMap<String, MappingMetadata> mappingsMetadata = new HashMap<>();
DocumentMapper mapper = mapperService.documentMapper();
if (mapper != null) {
MappingMetadata mappingMd = new MappingMetadata(mapper);
mappingsMetadata.put(mapper.type(), mappingMd);
}
final IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index).setRoutingNumShards(routingNumShards).settings(indexSettings);
for (MappingMetadata mappingMd : mappingsMetadata.values()) {
indexMetadataBuilder.putMapping(mappingMd);
}
for (AliasMetadata aliasMetadata : templatesAliases.values()) {
indexMetadataBuilder.putAlias(aliasMetadata);
}
indexMetadataBuilder.state(IndexMetadata.State.OPEN);
final IndexMetadata indexMetadata;
try {
indexMetadata = indexMetadataBuilder.build();
} catch (Exception e) {
removalReasons.add("failed to build index metadata");
throw e;
}
logger.info("[{}] creating index, cause [bulk], templates {}, shards [{}]/[{}], mappings {}", index, templateNames, indexMetadata.getNumberOfShards(), indexMetadata.getNumberOfReplicas(), mappings.keySet());
indexService.getIndexEventListener().beforeIndexAddedToCluster(indexMetadata.getIndex(), indexMetadata.getSettings());
newMetadataBuilder.put(indexMetadata, false);
removalReasons.add("cleaning up after validating index on master");
}
Metadata newMetadata = newMetadataBuilder.build();
ClusterState updatedState = ClusterState.builder(currentState).metadata(newMetadata).build();
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable());
for (String index : indicesToCreate) {
routingTableBuilder.addAsNew(updatedState.metadata().index(index));
}
return allocationService.reroute(ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build(), "bulk-index-creation");
} finally {
for (int i = 0; i < createdIndices.size(); i++) {
// Index was already partially created - need to clean up
String removalReason = removalReasons.size() > i ? removalReasons.get(i) : "failed to create index";
indicesService.removeIndex(createdIndices.get(i), IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED, removalReason);
}
}
}
use of org.elasticsearch.index.mapper.MapperParsingException in project crate by crate.
the class MetadataIndexTemplateService method validateAndAddTemplate.
private static void validateAndAddTemplate(final PutRequest request, IndexTemplateMetadata.Builder templateBuilder, IndicesService indicesService, NamedXContentRegistry xContentRegistry) throws Exception {
Index createdIndex = null;
final String temporaryIndexName = UUIDs.randomBase64UUID();
try {
// use the provided values, otherwise just pick valid dummy values
int dummyPartitionSize = IndexMetadata.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(request.settings);
int dummyShards = request.settings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1);
// create index service for parsing and validating "mappings"
Settings dummySettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(request.settings).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, dummyShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
final IndexMetadata tmpIndexMetadata = IndexMetadata.builder(temporaryIndexName).settings(dummySettings).build();
IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, Collections.emptyList(), false);
createdIndex = dummyIndexService.index();
templateBuilder.order(request.order);
templateBuilder.version(request.version);
templateBuilder.patterns(request.indexPatterns);
templateBuilder.settings(request.settings);
Map<String, Map<String, Object>> mappingsForValidation = new HashMap<>();
for (Map.Entry<String, String> entry : request.mappings.entrySet()) {
try {
templateBuilder.putMapping(entry.getKey(), entry.getValue());
} catch (Exception e) {
throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
}
mappingsForValidation.put(entry.getKey(), MapperService.parseMapping(xContentRegistry, entry.getValue()));
}
if (mappingsForValidation.isEmpty() == false) {
assert mappingsForValidation.size() == 1;
String type = mappingsForValidation.keySet().iterator().next();
dummyIndexService.mapperService().merge(type, mappingsForValidation.get(type), MergeReason.MAPPING_UPDATE);
}
} finally {
if (createdIndex != null) {
indicesService.removeIndex(createdIndex, NO_LONGER_ASSIGNED, " created for parsing template mapping");
}
}
}
Aggregations