Skip to content

Commit 321c463

Browse files
committed
Merge pull request #16557 from talevy/ingest_hide_null_metadata
hide null-valued metadata fields from WriteableIngestDocument#toXContent
2 parents 7625595 + f9453e5 commit 321c463

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

core/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6969
builder.startObject("doc");
7070
Map<IngestDocument.MetaData, String> metadataMap = ingestDocument.extractMetadata();
7171
for (Map.Entry<IngestDocument.MetaData, String> metadata : metadataMap.entrySet()) {
72-
builder.field(metadata.getKey().getFieldName(), metadata.getValue());
72+
if (metadata.getValue() != null) {
73+
builder.field(metadata.getKey().getFieldName(), metadata.getValue());
74+
}
7375
}
7476
builder.field("_source", ingestDocument.getSourceAndMetadata());
7577
builder.startObject("_ingest");

core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424
import org.elasticsearch.ingest.RandomDocumentPicks;
2525
import org.elasticsearch.ingest.core.IngestDocument;
2626
import org.elasticsearch.test.ESTestCase;
27+
import org.elasticsearch.test.XContentTestUtils;
2728

2829
import java.io.IOException;
2930
import java.util.Collections;
3031
import java.util.HashMap;
3132
import java.util.Map;
3233

34+
import static org.elasticsearch.test.XContentTestUtils.convertToMap;
35+
import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder;
3336
import static org.hamcrest.Matchers.equalTo;
37+
import static org.hamcrest.Matchers.is;
3438
import static org.hamcrest.Matchers.not;
39+
import static org.hamcrest.Matchers.nullValue;
3540

3641
public class WriteableIngestDocumentTests extends ESTestCase {
3742

@@ -111,4 +116,30 @@ public void testSerialization() throws IOException {
111116
WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput);
112117
assertThat(otherWriteableIngestDocument, equalTo(writeableIngestDocument));
113118
}
119+
120+
@SuppressWarnings("unchecked")
121+
public void testToXContent() throws IOException {
122+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
123+
WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(ingestDocument));
124+
125+
Map<String, Object> toXContentMap = convertToMap(writeableIngestDocument);
126+
Map<String, Object> toXContentDoc = (Map<String, Object>) toXContentMap.get("doc");
127+
Map<String, Object> toXContentSource = (Map<String, Object>) toXContentDoc.get("_source");
128+
Map<String, String> toXContentIngestMetadata = (Map<String, String>) toXContentDoc.get("_ingest");
129+
130+
Map<IngestDocument.MetaData, String> metadataMap = ingestDocument.extractMetadata();
131+
for (Map.Entry<IngestDocument.MetaData, String> metadata : metadataMap.entrySet()) {
132+
String fieldName = metadata.getKey().getFieldName();
133+
if (metadata.getValue() == null) {
134+
assertThat(toXContentDoc.containsKey(fieldName), is(false));
135+
} else {
136+
assertThat(toXContentDoc.get(fieldName), equalTo(metadata.getValue()));
137+
}
138+
}
139+
140+
String sourceDiff = differenceBetweenMapsIgnoringArrayOrder(toXContentSource, ingestDocument.getSourceAndMetadata());
141+
assertThat(sourceDiff, is(nullValue()));
142+
143+
assertThat(toXContentIngestMetadata, equalTo(ingestDocument.getIngestMetadata()));
144+
}
114145
}

0 commit comments

Comments
 (0)