Skip to content

Commit ad92657

Browse files
authored
Add debug logging for doc parsing exceptions (#117768) (#117816)
1 parent dd6e820 commit ad92657

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
package org.elasticsearch.index.mapper;
1111

12+
import org.apache.logging.log4j.Logger;
1213
import org.elasticsearch.common.compress.CompressedXContent;
14+
import org.elasticsearch.common.logging.Loggers;
1315
import org.elasticsearch.features.NodeFeature;
1416
import org.elasticsearch.index.IndexSettings;
1517
import org.elasticsearch.index.IndexSortConfig;
@@ -25,6 +27,7 @@ public class DocumentMapper {
2527
private final DocumentParser documentParser;
2628
private final MapperMetrics mapperMetrics;
2729
private final IndexVersion indexVersion;
30+
private final Logger logger;
2831

2932
static final NodeFeature INDEX_SORTING_ON_NESTED = new NodeFeature("mapper.index_sorting_on_nested");
3033

@@ -44,7 +47,8 @@ public static DocumentMapper createEmpty(MapperService mapperService) {
4447
mapping,
4548
mapping.toCompressedXContent(),
4649
IndexVersion.current(),
47-
mapperService.getMapperMetrics()
50+
mapperService.getMapperMetrics(),
51+
mapperService.index().getName()
4852
);
4953
}
5054

@@ -53,19 +57,27 @@ public static DocumentMapper createEmpty(MapperService mapperService) {
5357
Mapping mapping,
5458
CompressedXContent source,
5559
IndexVersion version,
56-
MapperMetrics mapperMetrics
60+
MapperMetrics mapperMetrics,
61+
String indexName
5762
) {
5863
this.documentParser = documentParser;
5964
this.type = mapping.getRoot().fullPath();
6065
this.mappingLookup = MappingLookup.fromMapping(mapping);
6166
this.mappingSource = source;
6267
this.mapperMetrics = mapperMetrics;
6368
this.indexVersion = version;
69+
this.logger = Loggers.getLogger(getClass(), indexName);
6470

6571
assert mapping.toCompressedXContent().equals(source) || isSyntheticSourceMalformed(source, version)
6672
: "provided source [" + source + "] differs from mapping [" + mapping.toCompressedXContent() + "]";
6773
}
6874

75+
private void maybeLogDebug(Exception ex) {
76+
if (logger.isDebugEnabled()) {
77+
logger.debug("Error while parsing document: " + ex.getMessage(), ex);
78+
}
79+
}
80+
6981
/**
7082
* Indexes built at v.8.7 were missing an explicit entry for synthetic_source.
7183
* This got restored in v.8.10 to avoid confusion. The change is only restricted to mapping printout, it has no
@@ -110,7 +122,12 @@ public MappingLookup mappers() {
110122
}
111123

112124
public ParsedDocument parse(SourceToParse source) throws DocumentParsingException {
113-
return documentParser.parseDocument(source, mappingLookup);
125+
try {
126+
return documentParser.parseDocument(source, mappingLookup);
127+
} catch (Exception e) {
128+
maybeLogDebug(e);
129+
throw e;
130+
}
114131
}
115132

116133
public void validate(IndexSettings settings, boolean checkLimits) {

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,14 @@ private DocumentMapper doMerge(String type, MergeReason reason, Map<String, Obje
595595
}
596596

597597
private DocumentMapper newDocumentMapper(Mapping mapping, MergeReason reason, CompressedXContent mappingSource) {
598-
DocumentMapper newMapper = new DocumentMapper(documentParser, mapping, mappingSource, indexVersionCreated, mapperMetrics);
598+
DocumentMapper newMapper = new DocumentMapper(
599+
documentParser,
600+
mapping,
601+
mappingSource,
602+
indexVersionCreated,
603+
mapperMetrics,
604+
index().getName()
605+
);
599606
newMapper.validate(indexSettings, reason != MergeReason.MAPPING_RECOVERY);
600607
return newMapper;
601608
}

server/src/test/java/org/elasticsearch/index/mapper/DocumentMapperTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public void testAddFields() throws Exception {
7575
merged,
7676
merged.toCompressedXContent(),
7777
IndexVersion.current(),
78-
MapperMetrics.NOOP
78+
MapperMetrics.NOOP,
79+
"myIndex"
7980
);
8081
assertThat(mergedMapper.mappers().getMapper("age"), notNullValue());
8182
assertThat(mergedMapper.mappers().getMapper("obj1.prop1"), notNullValue());

server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,8 @@ same name need to be part of the same mappings (hence the same document). If th
26252625
newMapping,
26262626
newMapping.toCompressedXContent(),
26272627
IndexVersion.current(),
2628-
MapperMetrics.NOOP
2628+
MapperMetrics.NOOP,
2629+
"myIndex"
26292630
);
26302631
ParsedDocument doc2 = newDocMapper.parse(source("""
26312632
{

0 commit comments

Comments
 (0)