use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class ObjectMapperTests method testFieldsArrayMultiFieldsShouldThrowException.
public void testFieldsArrayMultiFieldsShouldThrowException() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("tweet").startObject("properties").startObject("name").field("type", "text").startArray("fields").startObject().field("test", "string").endObject().startObject().field("test2", "string").endObject().endArray().endObject().endObject().endObject().endObject().string();
try {
createIndex("test").mapperService().documentMapperParser().parse("tweet", new CompressedXContent(mapping));
fail("Expected MapperParsingException");
} catch (MapperParsingException e) {
assertThat(e.getMessage(), containsString("expected map for property [fields]"));
assertThat(e.getMessage(), containsString("but got a class java.util.ArrayList"));
}
}
use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class IndexActionIT method testDocumentWithBlankFieldName.
public void testDocumentWithBlankFieldName() {
MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
client().prepareIndex("test", "type", "1").setSource("", "value1_2").execute().actionGet();
});
assertThat(e.getMessage(), containsString("failed to parse"));
assertThat(e.getRootCause().getMessage(), containsString("object field starting or ending with a [.] makes object resolution ambiguous: []"));
}
use of org.elasticsearch.index.mapper.MapperParsingException in project crate by crate.
the class TransportBulkCreateIndicesAction 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.
*/
ClusterState executeCreateIndices(ClusterState currentState, BulkCreateIndicesRequest request) throws Exception {
List<String> indicesToCreate = new ArrayList<>(request.indices().size());
String removalReason = null;
String testIndex = null;
try {
validateAndFilterExistingIndices(currentState, indicesToCreate, request);
if (indicesToCreate.isEmpty()) {
return currentState;
}
Map<String, IndexMetaData.Custom> customs = Maps.newHashMap();
Map<String, Map<String, Object>> mappings = Maps.newHashMap();
Map<String, AliasMetaData> templatesAliases = Maps.newHashMap();
List<String> templateNames = Lists.newArrayList();
List<IndexTemplateMetaData> templates = findTemplates(request, currentState, indexTemplateFilter);
applyTemplates(customs, mappings, templatesAliases, templateNames, templates);
File mappingsDir = new File(environment.configFile().toFile(), "mappings");
if (mappingsDir.isDirectory()) {
addMappingFromMappingsFile(mappings, mappingsDir, request);
}
Settings indexSettings = createIndexSettings(currentState, templates);
testIndex = indicesToCreate.get(0);
indicesService.createIndex(testIndex, indexSettings, clusterService.localNode().getId());
// now add the mappings
IndexService indexService = indicesService.indexServiceSafe(testIndex);
MapperService mapperService = indexService.mapperService();
// first, add the default mapping
if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
try {
mapperService.merge(MapperService.DEFAULT_MAPPING, new CompressedXContent(XContentFactory.jsonBuilder().map(mappings.get(MapperService.DEFAULT_MAPPING)).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
} catch (Exception e) {
removalReason = "failed on parsing default mapping on index creation";
throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e);
}
}
for (Map.Entry<String, Map<String, Object>> entry : mappings.entrySet()) {
if (entry.getKey().equals(MapperService.DEFAULT_MAPPING)) {
continue;
}
try {
// apply the default here, its the first time we parse it
mapperService.merge(entry.getKey(), new CompressedXContent(XContentFactory.jsonBuilder().map(entry.getValue()).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
} catch (Exception e) {
removalReason = "failed on parsing mappings on index creation";
throw new MapperParsingException("mapping [" + entry.getKey() + "]", e);
}
}
IndexQueryParserService indexQueryParserService = indexService.queryParserService();
for (AliasMetaData aliasMetaData : templatesAliases.values()) {
if (aliasMetaData.filter() != null) {
aliasValidator.validateAliasFilter(aliasMetaData.alias(), aliasMetaData.filter().uncompressed(), indexQueryParserService);
}
}
// now, update the mappings with the actual source
Map<String, MappingMetaData> mappingsMetaData = Maps.newHashMap();
for (DocumentMapper mapper : mapperService.docMappers(true)) {
MappingMetaData mappingMd = new MappingMetaData(mapper);
mappingsMetaData.put(mapper.type(), mappingMd);
}
MetaData.Builder newMetaDataBuilder = MetaData.builder(currentState.metaData());
for (String index : indicesToCreate) {
Settings newIndexSettings = createIndexSettings(currentState, templates);
final IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(index).settings(newIndexSettings);
for (MappingMetaData mappingMd : mappingsMetaData.values()) {
indexMetaDataBuilder.putMapping(mappingMd);
}
for (AliasMetaData aliasMetaData : templatesAliases.values()) {
indexMetaDataBuilder.putAlias(aliasMetaData);
}
for (Map.Entry<String, IndexMetaData.Custom> customEntry : customs.entrySet()) {
indexMetaDataBuilder.putCustom(customEntry.getKey(), customEntry.getValue());
}
indexMetaDataBuilder.state(IndexMetaData.State.OPEN);
final IndexMetaData indexMetaData;
try {
indexMetaData = indexMetaDataBuilder.build();
} catch (Exception e) {
removalReason = "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.indicesLifecycle().beforeIndexAddedToCluster(new Index(index), indexMetaData.getSettings());
newMetaDataBuilder.put(indexMetaData, false);
}
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));
}
RoutingAllocation.Result routingResult = allocationService.reroute(ClusterState.builder(updatedState).routingTable(routingTableBuilder).build(), "bulk-index-creation");
updatedState = ClusterState.builder(updatedState).routingResult(routingResult).build();
removalReason = "cleaning up after validating index on master";
return updatedState;
} finally {
if (testIndex != null) {
// index was partially created - need to clean up
indicesService.deleteIndex(testIndex, removalReason != null ? removalReason : "failed to create index");
}
}
}
use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class TransportShardBulkAction method shardOperationOnReplica.
@Override
public WriteReplicaResult<BulkShardRequest> shardOperationOnReplica(BulkShardRequest request, IndexShard replica) throws Exception {
Translog.Location location = null;
for (int i = 0; i < request.items().length; i++) {
BulkItemRequest item = request.items()[i];
if (shouldExecuteReplicaItem(item, i)) {
DocWriteRequest docWriteRequest = item.request();
DocWriteResponse primaryResponse = item.getPrimaryResponse().getResponse();
final Engine.Result operationResult;
try {
switch(docWriteRequest.opType()) {
case CREATE:
case INDEX:
operationResult = executeIndexRequestOnReplica(primaryResponse, (IndexRequest) docWriteRequest, replica);
break;
case DELETE:
operationResult = executeDeleteRequestOnReplica(primaryResponse, (DeleteRequest) docWriteRequest, replica);
break;
default:
throw new IllegalStateException("Unexpected request operation type on replica: " + docWriteRequest.opType().getLowercase());
}
if (operationResult.hasFailure()) {
// check if any transient write operation failures should be bubbled up
Exception failure = operationResult.getFailure();
assert failure instanceof VersionConflictEngineException || failure instanceof MapperParsingException : "expected any one of [version conflict, mapper parsing, engine closed, index shard closed]" + " failures. got " + failure;
if (!TransportActions.isShardNotAvailableException(failure)) {
throw failure;
}
} else {
location = locationToSync(location, operationResult.getTranslogLocation());
}
} catch (Exception e) {
// so we will fail the shard
if (!TransportActions.isShardNotAvailableException(e)) {
throw e;
}
}
}
}
return new WriteReplicaResult<>(request, location, null, replica, logger);
}
use of org.elasticsearch.index.mapper.MapperParsingException in project elasticsearch by elastic.
the class SimpleIndexTemplateIT method testCombineTemplates.
public void testCombineTemplates() 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());
//Now, a complete mapping with two separated templates is error
// base template
client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("*")).setSettings(" {\n" + " \"index\" : {\n" + " \"analysis\" : {\n" + " \"analyzer\" : {\n" + " \"custom_1\" : {\n" + " \"tokenizer\" : \"whitespace\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n", XContentType.JSON).get();
// put template using custom_1 analyzer
MapperParsingException e = expectThrows(MapperParsingException.class, () -> client().admin().indices().preparePutTemplate("template_2").setPatterns(Collections.singletonList("test*")).setCreate(true).setOrder(1).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field2").field("type", "text").field("analyzer", "custom_1").endObject().endObject().endObject().endObject()).get());
assertThat(e.getMessage(), containsString("analyzer [custom_1] not found for field [field2]"));
response = client().admin().indices().prepareGetTemplates().get();
assertThat(response.getIndexTemplates(), hasSize(1));
}
Aggregations