Merge pull request #16557 from talevy/ingest_hide_null_metadata

hide null-valued metadata fields from WriteableIngestDocument#toXContent
This commit is contained in:
Tal Levy 2016-02-10 08:29:16 -08:00
commit 321c463929
2 changed files with 34 additions and 1 deletions

View File

@ -69,7 +69,9 @@ final class WriteableIngestDocument implements Writeable<WriteableIngestDocument
builder.startObject("doc");
Map<IngestDocument.MetaData, String> metadataMap = ingestDocument.extractMetadata();
for (Map.Entry<IngestDocument.MetaData, String> metadata : metadataMap.entrySet()) {
builder.field(metadata.getKey().getFieldName(), metadata.getValue());
if (metadata.getValue() != null) {
builder.field(metadata.getKey().getFieldName(), metadata.getValue());
}
}
builder.field("_source", ingestDocument.getSourceAndMetadata());
builder.startObject("_ingest");

View File

@ -24,14 +24,19 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.elasticsearch.ingest.core.IngestDocument;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.XContentTestUtils;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.test.XContentTestUtils.convertToMap;
import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
public class WriteableIngestDocumentTests extends ESTestCase {
@ -111,4 +116,30 @@ public class WriteableIngestDocumentTests extends ESTestCase {
WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput);
assertThat(otherWriteableIngestDocument, equalTo(writeableIngestDocument));
}
@SuppressWarnings("unchecked")
public void testToXContent() throws IOException {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(ingestDocument));
Map<String, Object> toXContentMap = convertToMap(writeableIngestDocument);
Map<String, Object> toXContentDoc = (Map<String, Object>) toXContentMap.get("doc");
Map<String, Object> toXContentSource = (Map<String, Object>) toXContentDoc.get("_source");
Map<String, String> toXContentIngestMetadata = (Map<String, String>) toXContentDoc.get("_ingest");
Map<IngestDocument.MetaData, String> metadataMap = ingestDocument.extractMetadata();
for (Map.Entry<IngestDocument.MetaData, String> metadata : metadataMap.entrySet()) {
String fieldName = metadata.getKey().getFieldName();
if (metadata.getValue() == null) {
assertThat(toXContentDoc.containsKey(fieldName), is(false));
} else {
assertThat(toXContentDoc.get(fieldName), equalTo(metadata.getValue()));
}
}
String sourceDiff = differenceBetweenMapsIgnoringArrayOrder(toXContentSource, ingestDocument.getSourceAndMetadata());
assertThat(sourceDiff, is(nullValue()));
assertThat(toXContentIngestMetadata, equalTo(ingestDocument.getIngestMetadata()));
}
}