Clean up XContentBuilder

This commit cleans most of the methods of XContentBuilder so that:
- Jackson's convenience methods are used instead of our custom ones (ie field(String,long) now uses Jackson's writeNumberField(String, long) instead of calling writeField(String) then writeNumber(long))
- null checks are added for all field names and values
- methods are grouped by type in the class source
- methods have the same parameters names
- duplicated methods like field(String, String...) and array(String, String...) are removed
- varargs methods now have the "array" name to reflect that it builds arrays
- unused methods like field(String,BigDecimal) are removed
- all methods now follow the execution path: field(String,?) -> field(String) then value(?), and value(?) -> writeSomething() method. Methods to build arrays also follow the same execution path.
This commit is contained in:
Tanguy Leroux 2016-09-02 18:11:55 +02:00
parent f761038dfd
commit 4fb7ac8254
42 changed files with 1682 additions and 938 deletions

View File

@ -292,7 +292,7 @@ public class DetailAnalyzeResponse implements Streamable, ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(Fields.NAME, name);
builder.field(Fields.FILTERED_TEXT, texts);
builder.array(Fields.FILTERED_TEXT, texts);
builder.endObject();
return builder;
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import java.io.IOException;

View File

@ -20,14 +20,13 @@
package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.bytes.BytesReference;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
/**
*
*/
public interface XContentGenerator extends Closeable {
public interface XContentGenerator extends Closeable, Flushable {
XContentType contentType();
@ -37,68 +36,62 @@ public interface XContentGenerator extends Closeable {
void usePrintLineFeedAtEnd();
void writeStartArray() throws IOException;
void writeEndArray() throws IOException;
void writeStartObject() throws IOException;
void writeEndObject() throws IOException;
void writeStartArray() throws IOException;
void writeEndArray() throws IOException;
void writeFieldName(String name) throws IOException;
void writeString(String text) throws IOException;
void writeString(char[] text, int offset, int len) throws IOException;
void writeUTF8String(byte[] text, int offset, int length) throws IOException;
void writeBinary(byte[] data, int offset, int len) throws IOException;
void writeBinary(byte[] data) throws IOException;
void writeNumber(int v) throws IOException;
void writeNumber(long v) throws IOException;
void writeNumber(double d) throws IOException;
void writeNumber(float f) throws IOException;
void writeBoolean(boolean state) throws IOException;
void writeNull() throws IOException;
void writeStringField(String fieldName, String value) throws IOException;
void writeNullField(String name) throws IOException;
void writeBooleanField(String fieldName, boolean value) throws IOException;
void writeBooleanField(String name, boolean value) throws IOException;
void writeNullField(String fieldName) throws IOException;
void writeBoolean(boolean value) throws IOException;
void writeNumberField(String fieldName, int value) throws IOException;
void writeNumberField(String name, double value) throws IOException;
void writeNumberField(String fieldName, long value) throws IOException;
void writeNumber(double value) throws IOException;
void writeNumberField(String fieldName, double value) throws IOException;
void writeNumberField(String name, float value) throws IOException;
void writeNumberField(String fieldName, float value) throws IOException;
void writeNumber(float value) throws IOException;
void writeBinaryField(String fieldName, byte[] data) throws IOException;
void writeNumberField(String name, int value) throws IOException;
void writeArrayFieldStart(String fieldName) throws IOException;
void writeNumber(int value) throws IOException;
void writeObjectFieldStart(String fieldName) throws IOException;
void writeNumberField(String name, long value) throws IOException;
void writeRawField(String fieldName, InputStream content) throws IOException;
void writeNumber(long value) throws IOException;
void writeRawField(String fieldName, BytesReference content) throws IOException;
void writeNumber(short value) throws IOException;
void writeRawValue(BytesReference content) throws IOException;
void writeStringField(String name, String value) throws IOException;
void writeString(String value) throws IOException;
void writeString(char[] text, int offset, int len) throws IOException;
void writeUTF8String(byte[] value, int offset, int length) throws IOException;
void writeBinaryField(String name, byte[] value) throws IOException;
void writeBinary(byte[] value) throws IOException;
void writeBinary(byte[] value, int offset, int length) throws IOException;
void writeRawField(String name, InputStream value) throws IOException;
void writeRawField(String name, BytesReference value) throws IOException;
void writeRawValue(BytesReference value) throws IOException;
void copyCurrentStructure(XContentParser parser) throws IOException;
void flush() throws IOException;
@Override
void close() throws IOException;
}

View File

@ -47,9 +47,6 @@ import java.util.Collections;
import java.util.Objects;
import java.util.Set;
/**
*
*/
public class JsonXContentGenerator implements XContentGenerator {
/** Generator used to write content **/
@ -130,16 +127,6 @@ public class JsonXContentGenerator implements XContentGenerator {
writeLineFeedAtEnd = true;
}
@Override
public void writeStartArray() throws IOException {
generator.writeStartArray();
}
@Override
public void writeEndArray() throws IOException {
generator.writeEndArray();
}
private boolean isFiltered() {
return filter != null;
}
@ -184,118 +171,124 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.writeEndObject();
}
@Override
public void writeStartArray() throws IOException {
generator.writeStartArray();
}
@Override
public void writeEndArray() throws IOException {
generator.writeEndArray();
}
@Override
public void writeFieldName(String name) throws IOException {
generator.writeFieldName(name);
}
@Override
public void writeString(String text) throws IOException {
generator.writeString(text);
}
@Override
public void writeString(char[] text, int offset, int len) throws IOException {
generator.writeString(text, offset, len);
}
@Override
public void writeUTF8String(byte[] text, int offset, int length) throws IOException {
generator.writeUTF8String(text, offset, length);
}
@Override
public void writeBinary(byte[] data, int offset, int len) throws IOException {
generator.writeBinary(data, offset, len);
}
@Override
public void writeBinary(byte[] data) throws IOException {
generator.writeBinary(data);
}
@Override
public void writeNumber(int v) throws IOException {
generator.writeNumber(v);
}
@Override
public void writeNumber(long v) throws IOException {
generator.writeNumber(v);
}
@Override
public void writeNumber(double d) throws IOException {
generator.writeNumber(d);
}
@Override
public void writeNumber(float f) throws IOException {
generator.writeNumber(f);
}
@Override
public void writeBoolean(boolean state) throws IOException {
generator.writeBoolean(state);
}
@Override
public void writeNull() throws IOException {
generator.writeNull();
}
@Override
public void writeStringField(String fieldName, String value) throws IOException {
generator.writeStringField(fieldName, value);
public void writeNullField(String name) throws IOException {
generator.writeNullField(name);
}
@Override
public void writeBooleanField(String fieldName, boolean value) throws IOException {
generator.writeBooleanField(fieldName, value);
public void writeBooleanField(String name, boolean value) throws IOException {
generator.writeBooleanField(name, value);
}
@Override
public void writeNullField(String fieldName) throws IOException {
generator.writeNullField(fieldName);
public void writeBoolean(boolean value) throws IOException {
generator.writeBoolean(value);
}
@Override
public void writeNumberField(String fieldName, int value) throws IOException {
generator.writeNumberField(fieldName, value);
public void writeNumberField(String name, double value) throws IOException {
generator.writeNumberField(name, value);
}
@Override
public void writeNumberField(String fieldName, long value) throws IOException {
generator.writeNumberField(fieldName, value);
public void writeNumber(double value) throws IOException {
generator.writeNumber(value);
}
@Override
public void writeNumberField(String fieldName, double value) throws IOException {
generator.writeNumberField(fieldName, value);
public void writeNumberField(String name, float value) throws IOException {
generator.writeNumberField(name, value);
}
@Override
public void writeNumberField(String fieldName, float value) throws IOException {
generator.writeNumberField(fieldName, value);
public void writeNumber(float value) throws IOException {
generator.writeNumber(value);
}
@Override
public void writeBinaryField(String fieldName, byte[] data) throws IOException {
generator.writeBinaryField(fieldName, data);
public void writeNumberField(String name, int value) throws IOException {
generator.writeNumberField(name, value);
}
@Override
public void writeArrayFieldStart(String fieldName) throws IOException {
generator.writeArrayFieldStart(fieldName);
public void writeNumber(int value) throws IOException {
generator.writeNumber(value);
}
@Override
public void writeObjectFieldStart(String fieldName) throws IOException {
generator.writeObjectFieldStart(fieldName);
public void writeNumberField(String name, long value) throws IOException {
generator.writeNumberField(name, value);
}
private void writeStartRaw(String fieldName) throws IOException {
writeFieldName(fieldName);
@Override
public void writeNumber(long value) throws IOException {
generator.writeNumber(value);
}
@Override
public void writeNumber(short value) throws IOException {
generator.writeNumber(value);
}
@Override
public void writeStringField(String name, String value) throws IOException {
generator.writeStringField(name, value);
}
@Override
public void writeString(String value) throws IOException {
generator.writeString(value);
}
@Override
public void writeString(char[] value, int offset, int len) throws IOException {
generator.writeString(value, offset, len);
}
@Override
public void writeUTF8String(byte[] value, int offset, int length) throws IOException {
generator.writeUTF8String(value, offset, length);
}
@Override
public void writeBinaryField(String name, byte[] value) throws IOException {
generator.writeBinaryField(name, value);
}
@Override
public void writeBinary(byte[] value) throws IOException {
generator.writeBinary(value);
}
@Override
public void writeBinary(byte[] value, int offset, int len) throws IOException {
generator.writeBinary(value, offset, len);
}
private void writeStartRaw(String name) throws IOException {
writeFieldName(name);
generator.writeRaw(':');
}
@ -309,7 +302,7 @@ public class JsonXContentGenerator implements XContentGenerator {
}
@Override
public void writeRawField(String fieldName, InputStream content) throws IOException {
public void writeRawField(String name, InputStream content) throws IOException {
if (content.markSupported() == false) {
// needed for the XContentFactory.xContentType call
content = new BufferedInputStream(content);
@ -321,11 +314,11 @@ public class JsonXContentGenerator implements XContentGenerator {
if (mayWriteRawData(contentType) == false) {
try (XContentParser parser = XContentFactory.xContent(contentType).createParser(content)) {
parser.nextToken();
writeFieldName(fieldName);
writeFieldName(name);
copyCurrentStructure(parser);
}
} else {
writeStartRaw(fieldName);
writeStartRaw(name);
flush();
Streams.copy(content, os);
writeEndRaw();
@ -333,16 +326,16 @@ public class JsonXContentGenerator implements XContentGenerator {
}
@Override
public final void writeRawField(String fieldName, BytesReference content) throws IOException {
public final void writeRawField(String name, BytesReference content) throws IOException {
XContentType contentType = XContentFactory.xContentType(content);
if (contentType == null) {
throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
}
if (mayWriteRawData(contentType) == false) {
writeFieldName(fieldName);
writeFieldName(name);
copyRawValue(content, contentType.xContent());
} else {
writeStartRaw(fieldName);
writeStartRaw(name);
flush();
content.writeTo(os);
writeEndRaw();
@ -416,7 +409,7 @@ public class JsonXContentGenerator implements XContentGenerator {
}
JsonStreamContext context = generator.getOutputContext();
if ((context != null) && (context.inRoot() == false)) {
throw new IOException("unclosed object or array found");
throw new IOException("Unclosed object or array found");
}
if (writeLineFeedAtEnd) {
flush();

View File

@ -275,15 +275,15 @@ public class SourceFieldMapper extends MetadataFieldMapper {
}
if (includes != null) {
builder.field("includes", includes);
builder.array("includes", includes);
} else if (includeDefaults) {
builder.field("includes", Strings.EMPTY_ARRAY);
builder.array("includes", Strings.EMPTY_ARRAY);
}
if (excludes != null) {
builder.field("excludes", excludes);
builder.array("excludes", excludes);
} else if (includeDefaults) {
builder.field("excludes", Strings.EMPTY_ARRAY);
builder.array("excludes", Strings.EMPTY_ARRAY);
}
builder.endObject();

View File

@ -132,7 +132,7 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder<IndicesQueryBuilde
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field(INDICES_FIELD.getPreferredName(), indices);
builder.array(INDICES_FIELD.getPreferredName(), indices);
builder.field(QUERY_FIELD.getPreferredName());
innerQuery.toXContent(builder, params);
builder.field(NO_MATCH_QUERY.getPreferredName());

View File

@ -780,7 +780,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (fields != null) {
builder.field(Field.FIELDS.getPreferredName(), fields);
builder.array(Field.FIELDS.getPreferredName(), fields);
}
buildLikeField(builder, Field.LIKE.getPreferredName(), likeTexts, likeItems);
buildLikeField(builder, Field.UNLIKE.getPreferredName(), unlikeTexts, unlikeItems);
@ -791,7 +791,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
builder.field(Field.MIN_WORD_LENGTH.getPreferredName(), minWordLength);
builder.field(Field.MAX_WORD_LENGTH.getPreferredName(), maxWordLength);
if (stopWords != null) {
builder.field(Field.STOP_WORDS.getPreferredName(), stopWords);
builder.array(Field.STOP_WORDS.getPreferredName(), stopWords);
}
if (analyzer != null) {
builder.field(Field.ANALYZER.getPreferredName(), analyzer);

View File

@ -260,9 +260,9 @@ public class RecoveryState implements ToXContent, Streamable {
builder.field(Fields.TYPE, recoverySource.getType());
builder.field(Fields.STAGE, stage.toString());
builder.field(Fields.PRIMARY, primary);
builder.dateValueField(Fields.START_TIME_IN_MILLIS, Fields.START_TIME, timer.startTime);
builder.dateField(Fields.START_TIME_IN_MILLIS, Fields.START_TIME, timer.startTime);
if (timer.stopTime > 0) {
builder.dateValueField(Fields.STOP_TIME_IN_MILLIS, Fields.STOP_TIME, timer.stopTime);
builder.dateField(Fields.STOP_TIME_IN_MILLIS, Fields.STOP_TIME, timer.stopTime);
}
builder.timeValueField(Fields.TOTAL_TIME_IN_MILLIS, Fields.TOTAL_TIME, timer.time());

View File

@ -431,7 +431,7 @@ public class JvmInfo implements Writeable, ToXContent {
builder.field(Fields.VM_NAME, vmName);
builder.field(Fields.VM_VERSION, vmVersion);
builder.field(Fields.VM_VENDOR, vmVendor);
builder.dateValueField(Fields.START_TIME_IN_MILLIS, Fields.START_TIME, startTime);
builder.dateField(Fields.START_TIME_IN_MILLIS, Fields.START_TIME, startTime);
builder.startObject(Fields.MEM);
builder.byteSizeField(Fields.HEAP_INIT_IN_BYTES, Fields.HEAP_INIT, mem.heapInit);
@ -441,8 +441,8 @@ public class JvmInfo implements Writeable, ToXContent {
builder.byteSizeField(Fields.DIRECT_MAX_IN_BYTES, Fields.DIRECT_MAX, mem.directMemoryMax);
builder.endObject();
builder.field(Fields.GC_COLLECTORS, gcCollectors);
builder.field(Fields.MEMORY_POOLS, memoryPools);
builder.array(Fields.GC_COLLECTORS, gcCollectors);
builder.array(Fields.MEMORY_POOLS, memoryPools);
builder.field(Fields.USING_COMPRESSED_OOPS, useCompressedOops);

View File

@ -180,7 +180,7 @@ public class PercentileRanksAggregationBuilder extends LeafOnly<ValuesSource.Num
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(PercentileRanksParser.VALUES_FIELD.getPreferredName(), values);
builder.array(PercentileRanksParser.VALUES_FIELD.getPreferredName(), values);
builder.field(AbstractPercentilesParser.KEYED_FIELD.getPreferredName(), keyed);
builder.startObject(method.getName());
if (method == PercentilesMethod.TDIGEST) {

View File

@ -180,7 +180,7 @@ public class PercentilesAggregationBuilder extends LeafOnly<ValuesSource.Numeric
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(PercentilesParser.PERCENTS_FIELD.getPreferredName(), percents);
builder.array(PercentilesParser.PERCENTS_FIELD.getPreferredName(), percents);
builder.field(AbstractPercentilesParser.KEYED_FIELD.getPreferredName(), keyed);
builder.startObject(method.getName());
if (method == PercentilesMethod.TDIGEST) {

View File

@ -113,7 +113,7 @@ public class PercentilesBucketPipelineAggregationBuilder
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
if (percents != null) {
builder.field(PERCENTS_FIELD.getPreferredName(), percents);
builder.array(PERCENTS_FIELD.getPreferredName(), percents);
}
return builder;
}

View File

@ -1177,7 +1177,7 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
}
if (searchAfterBuilder != null) {
builder.field(SEARCH_AFTER.getPreferredName(), searchAfterBuilder.getSortValues());
builder.array(SEARCH_AFTER.getPreferredName(), searchAfterBuilder.getSortValues());
}
if (sliceBuilder != null) {

View File

@ -476,7 +476,7 @@ public class HighlightBuilder extends AbstractHighlighterBuilder<HighlightBuilde
builder.field(FRAGMENT_OFFSET_FIELD.getPreferredName(), fragmentOffset);
}
if (matchedFields != null) {
builder.field(MATCHED_FIELDS_FIELD.getPreferredName(), matchedFields);
builder.array(MATCHED_FIELDS_FIELD.getPreferredName(), matchedFields);
}
builder.endObject();
}

View File

@ -202,7 +202,7 @@ public class SearchAfterBuilder implements ToXContent, Writeable {
}
void innerToXContent(XContentBuilder builder) throws IOException {
builder.field(SEARCH_AFTER.getPreferredName(), sortValues);
builder.array(SEARCH_AFTER.getPreferredName(), sortValues);
}
public static SearchAfterBuilder fromXContent(XContentParser parser, ParseFieldMatcher parseFieldMatcher) throws IOException {

View File

@ -208,7 +208,7 @@ public class GeolocationContextMapping extends ContextMapping {
@Override
protected XContentBuilder toInnerXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(FIELD_PRECISION, precision);
builder.array(FIELD_PRECISION, precision);
builder.field(FIELD_NEIGHBORS, neighbors);
if (defaultLocations != null) {
builder.startArray(FIELD_MISSING);
@ -741,7 +741,7 @@ public class GeolocationContextMapping extends ContextMapping {
} else {
builder.startObject(name);
builder.field(FIELD_VALUE, location);
builder.field(FIELD_PRECISION, precisions);
builder.array(FIELD_PRECISION, precisions);
builder.endObject();
}
return builder;

View File

@ -180,7 +180,7 @@ public final class TaskInfo implements Writeable, ToXContent {
if (description != null) {
builder.field("description", description);
}
builder.dateValueField("start_time_in_millis", "start_time", startTime);
builder.dateField("start_time_in_millis", "start_time", startTime);
builder.timeValueField("running_time_in_nanos", "running_time", runningTimeNanos, TimeUnit.NANOSECONDS);
builder.field("cancellable", cancellable);
if (parentTaskId.isSet()) {

View File

@ -115,12 +115,12 @@ public class AliasActionsTests extends ESTestCase {
b.startObject(); {
b.startObject("add"); {
if (indices.length > 1 || randomBoolean()) {
b.field("indices", indices);
b.array("indices", indices);
} else {
b.field("index", indices[0]);
}
if (aliases.length > 1 || randomBoolean()) {
b.field("aliases", aliases);
b.array("aliases", aliases);
} else {
b.field("alias", aliases[0]);
}
@ -196,12 +196,12 @@ public class AliasActionsTests extends ESTestCase {
b.startObject(); {
b.startObject("remove"); {
if (indices.length > 1 || randomBoolean()) {
b.field("indices", indices);
b.array("indices", indices);
} else {
b.field("index", indices[0]);
}
if (aliases.length > 1 || randomBoolean()) {
b.field("aliases", aliases);
b.array("aliases", aliases);
} else {
b.field("alias", aliases[0]);
}
@ -224,7 +224,7 @@ public class AliasActionsTests extends ESTestCase {
b.startObject(); {
b.startObject("remove_index"); {
if (indices.length > 1 || randomBoolean()) {
b.field("indices", indices);
b.array("indices", indices);
} else {
b.field("index", indices[0]);
}
@ -246,7 +246,7 @@ public class AliasActionsTests extends ESTestCase {
b.startObject(); {
b.startObject(randomFrom("add", "remove")); {
b.field("index", randomAsciiOfLength(5));
b.field("indices", generateRandomStringArray(10, 5, false, false));
b.array("indices", generateRandomStringArray(10, 5, false, false));
b.field("alias", randomAsciiOfLength(5));
}
b.endObject();
@ -265,7 +265,7 @@ public class AliasActionsTests extends ESTestCase {
b.startObject(randomFrom("add", "remove")); {
b.field("index", randomAsciiOfLength(5));
b.field("alias", randomAsciiOfLength(5));
b.field("aliases", generateRandomStringArray(10, 5, false, false));
b.array("aliases", generateRandomStringArray(10, 5, false, false));
}
b.endObject();
}

View File

@ -19,23 +19,657 @@
package org.elasticsearch.common.xcontent;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Instant;
import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
public abstract class BaseXContentTestCase extends ESTestCase {
public abstract XContentType xcontentType();
protected abstract XContentType xcontentType();
private XContentBuilder builder() throws IOException {
return XContentBuilder.builder(xcontentType().xContent());
}
public void testContentType() throws IOException {
assertThat(builder().contentType(), equalTo(xcontentType()));
}
public void testStartEndObject() throws IOException {
expectUnclosedException(() -> builder().startObject().bytes());
expectUnclosedException(() -> builder().startObject().close());
expectUnclosedException(() -> builder().startObject().string());
expectObjectException(() -> builder().endObject().bytes());
expectObjectException(() -> builder().endObject().close());
expectObjectException(() -> builder().endObject().string());
expectValueException(() -> builder().startObject("foo").endObject());
expectNonNullFieldException(() -> builder().startObject().startObject(null));
assertResult("{}", () -> builder().startObject().endObject());
assertResult("{'foo':{}}", () -> builder().startObject().startObject("foo").endObject().endObject());
assertResult("{'foo':{'bar':{}}}", () -> builder()
.startObject()
.startObject("foo")
.startObject("bar")
.endObject()
.endObject()
.endObject());
}
public void testStartEndArray() throws IOException {
expectUnclosedException(() -> builder().startArray().bytes());
expectUnclosedException(() -> builder().startArray().close());
expectUnclosedException(() -> builder().startArray().string());
expectArrayException(() -> builder().endArray().bytes());
expectArrayException(() -> builder().endArray().close());
expectArrayException(() -> builder().endArray().string());
expectValueException(() -> builder().startArray("foo").endObject());
expectFieldException(() -> builder().startObject().startArray().endArray().endObject());
expectNonNullFieldException(() -> builder().startObject().startArray(null).endArray().endObject());
assertResult("{'foo':[]}", () -> builder().startObject().startArray("foo").endArray().endObject());
assertResult("{'foo':[1,2,3]}", () -> builder()
.startObject()
.startArray("foo")
.value(1)
.value(2)
.value(3)
.endArray()
.endObject());
}
public void testField() throws IOException {
expectValueException(() -> builder().field("foo").bytes());
expectNonNullFieldException(() -> builder().field(null).bytes());
expectUnclosedException(() -> builder().startObject().field("foo").bytes());
assertResult("{'foo':'bar'}", () -> builder().startObject().field("foo").value("bar").endObject());
}
public void testNullField() throws IOException {
expectValueException(() -> builder().nullField("foo").bytes());
expectNonNullFieldException(() -> builder().nullField(null).bytes());
expectUnclosedException(() -> builder().startObject().nullField("foo").bytes());
assertResult("{'foo':null}", () -> builder().startObject().nullField("foo").endObject());
}
public void testNullValue() throws IOException {
assertResult("{'foo':null}", () -> builder().startObject().field("foo").nullValue().endObject());
}
public void testBooleans() throws IOException {
assertResult("{'boolean':null}", () -> builder().startObject().field("boolean", (Boolean) null).endObject());
assertResult("{'boolean':true}", () -> builder().startObject().field("boolean", Boolean.TRUE).endObject());
assertResult("{'boolean':false}", () -> builder().startObject().field("boolean", Boolean.FALSE).endObject());
assertResult("{'boolean':[true,false,true]}", () -> builder().startObject().array("boolean", true, false, true).endObject());
assertResult("{'boolean':[false,true]}", () -> builder().startObject().array("boolean", new boolean[]{false, true}).endObject());
assertResult("{'boolean':null}", () -> builder().startObject().array("boolean", (boolean[]) null).endObject());
assertResult("{'boolean':[]}", () -> builder().startObject().array("boolean", new boolean[]{}).endObject());
assertResult("{'boolean':null}", () -> builder().startObject().field("boolean").value((Boolean) null).endObject());
assertResult("{'boolean':true}", () -> builder().startObject().field("boolean").value(Boolean.TRUE).endObject());
assertResult("{'boolean':false}", () -> builder().startObject().field("boolean").value(Boolean.FALSE).endObject());
}
public void testBytes() throws IOException {
assertResult("{'byte':null}", () -> builder().startObject().field("byte", (Byte) null).endObject());
assertResult("{'byte':0}", () -> builder().startObject().field("byte", (byte) 0).endObject());
assertResult("{'byte':1}", () -> builder().startObject().field("byte", (byte) 1).endObject());
assertResult("{'byte':null}", () -> builder().startObject().field("byte").value((Byte) null).endObject());
assertResult("{'byte':0}", () -> builder().startObject().field("byte").value((byte) 0).endObject());
assertResult("{'byte':1}", () -> builder().startObject().field("byte").value((byte) 1).endObject());
}
public void testDoubles() throws IOException {
assertResult("{'double':null}", () -> builder().startObject().field("double", (Double) null).endObject());
assertResult("{'double':42.5}", () -> builder().startObject().field("double", Double.valueOf(42.5)).endObject());
assertResult("{'double':1.2}", () -> builder().startObject().field("double", 1.2).endObject());
assertResult("{'double':[42.0,43.0,45]}", () -> builder().startObject().array("double", 42.0, 43.0, 45).endObject());
assertResult("{'double':null}", () -> builder().startObject().array("double", (double[]) null).endObject());
assertResult("{'double':[]}", () -> builder().startObject().array("double", new double[]{}).endObject());
assertResult("{'double':null}", () -> builder().startObject().field("double").value((Double) null).endObject());
assertResult("{'double':0.001}", () -> builder().startObject().field("double").value(0.001).endObject());
assertResult("{'double':[1.7976931348623157E308,4.9E-324]}", () -> builder()
.startObject()
.array("double", new double[]{Double.MAX_VALUE, Double.MIN_VALUE})
.endObject());
}
public void testFloats() throws IOException {
assertResult("{'float':null}", () -> builder().startObject().field("float", (Float) null).endObject());
assertResult("{'float':42.5}", () -> builder().startObject().field("float", Float.valueOf(42.5f)).endObject());
assertResult("{'float':1.2}", () -> builder().startObject().field("float", 1.2f).endObject());
assertResult("{'float':null}", () -> builder().startObject().array("float", (float[]) null).endObject());
assertResult("{'float':[]}", () -> builder().startObject().array("float", new float[]{}).endObject());
assertResult("{'float':null}", () -> builder().startObject().field("float").value((Float) null).endObject());
assertResult("{'float':9.9E-7}", () -> builder().startObject().field("float").value(0.00000099f).endObject());
assertResult("{'float':[42.0,43.0,45.666668]}", () -> builder()
.startObject()
.array("float", 42.0f, 43.0f, 45.66666667f)
.endObject());
assertResult("{'float':[3.4028235E38,1.4E-45]}", () -> builder()
.startObject()
.array("float", new float[]{Float.MAX_VALUE, Float.MIN_VALUE})
.endObject());
}
public void testIntegers() throws IOException {
assertResult("{'integer':null}", () -> builder().startObject().field("integer", (Integer) null).endObject());
assertResult("{'integer':42}", () -> builder().startObject().field("integer", Integer.valueOf(42)).endObject());
assertResult("{'integer':3}", () -> builder().startObject().field("integer", 3).endObject());
assertResult("{'integer':[1,3,5,7,11]}", () -> builder().startObject().array("integer", 1, 3, 5, 7, 11).endObject());
assertResult("{'integer':null}", () -> builder().startObject().array("integer", (int[]) null).endObject());
assertResult("{'integer':[]}", () -> builder().startObject().array("integer", new int[]{}).endObject());
assertResult("{'integer':null}", () -> builder().startObject().field("integer").value((Integer) null).endObject());
assertResult("{'integer':42}", () -> builder().startObject().field("integer").value(42).endObject());
assertResult("{'integer':[2147483647,-2147483648]}", () -> builder()
.startObject()
.array("integer", new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE})
.endObject());
}
public void testLongs() throws IOException {
assertResult("{'long':null}", () -> builder().startObject().field("long", (Long) null).endObject());
assertResult("{'long':42}", () -> builder().startObject().field("long", Long.valueOf(42L)).endObject());
assertResult("{'long':9223372036854775807}", () -> builder().startObject().field("long", 9_223_372_036_854_775_807L).endObject());
assertResult("{'long':[1,3,5,7,11]}", () -> builder().startObject().array("long", 1L, 3L, 5L, 7L, 11L).endObject());
assertResult("{'long':null}", () -> builder().startObject().array("long", (long[]) null).endObject());
assertResult("{'long':[]}", () -> builder().startObject().array("long", new long[]{}).endObject());
assertResult("{'long':null}", () -> builder().startObject().field("long").value((Long) null).endObject());
assertResult("{'long':42}", () -> builder().startObject().field("long").value(42).endObject());
assertResult("{'long':[2147483647,-2147483648]}", () -> builder()
.startObject()
.array("long", new long[]{Integer.MAX_VALUE, Integer.MIN_VALUE})
.endObject());
}
public void testShorts() throws IOException {
assertResult("{'short':null}", () -> builder().startObject().field("short", (Short) null).endObject());
assertResult("{'short':5000}", () -> builder().startObject().field("short", Short.valueOf((short) 5000)).endObject());
assertResult("{'short':null}", () -> builder().startObject().array("short", (short[]) null).endObject());
assertResult("{'short':[]}", () -> builder().startObject().array("short", new short[]{}).endObject());
assertResult("{'short':null}", () -> builder().startObject().field("short").value((Short) null).endObject());
assertResult("{'short':42}", () -> builder().startObject().field("short").value((short) 42).endObject());
assertResult("{'short':[1,3,5,7,11]}", () -> builder()
.startObject()
.array("short", (short) 1, (short) 3, (short) 5, (short) 7, (short) 11)
.endObject());
assertResult("{'short':[32767,-32768]}", () -> builder()
.startObject()
.array("short", new short[]{Short.MAX_VALUE, Short.MIN_VALUE})
.endObject());
}
public void testStrings() throws IOException {
assertResult("{'string':null}", () -> builder().startObject().field("string", (String) null).endObject());
assertResult("{'string':'value'}", () -> builder().startObject().field("string", "value").endObject());
assertResult("{'string':''}", () -> builder().startObject().field("string", "").endObject());
assertResult("{'string':null}", () -> builder().startObject().array("string", (String[]) null).endObject());
assertResult("{'string':[]}", () -> builder().startObject().array("string", Strings.EMPTY_ARRAY).endObject());
assertResult("{'string':null}", () -> builder().startObject().field("string").value((String) null).endObject());
assertResult("{'string':'42'}", () -> builder().startObject().field("string").value("42").endObject());
assertResult("{'string':['a','b','c','d']}", () -> builder()
.startObject()
.array("string", "a", "b", "c", "d")
.endObject());
}
public void testBinaryField() throws Exception {
assertResult("{'binary':null}", () -> builder().startObject().field("binary", (byte[]) null).endObject());
final byte[] randomBytes = randomBytes();
BytesReference bytes = builder().startObject().field("binary", randomBytes).endObject().bytes();
XContentParser parser = xcontentType().xContent().createParser(bytes);
assertSame(parser.nextToken(), Token.START_OBJECT);
assertSame(parser.nextToken(), Token.FIELD_NAME);
assertEquals(parser.currentName(), "binary");
assertTrue(parser.nextToken().isValue());
assertArrayEquals(randomBytes, parser.binaryValue());
assertSame(parser.nextToken(), Token.END_OBJECT);
assertNull(parser.nextToken());
}
public void testBinaryValue() throws Exception {
assertResult("{'binary':null}", () -> builder().startObject().field("binary").value((byte[]) null).endObject());
final byte[] randomBytes = randomBytes();
BytesReference bytes = builder().startObject().field("binary").value(randomBytes).endObject().bytes();
XContentParser parser = xcontentType().xContent().createParser(bytes);
assertSame(parser.nextToken(), Token.START_OBJECT);
assertSame(parser.nextToken(), Token.FIELD_NAME);
assertEquals(parser.currentName(), "binary");
assertTrue(parser.nextToken().isValue());
assertArrayEquals(randomBytes, parser.binaryValue());
assertSame(parser.nextToken(), Token.END_OBJECT);
assertNull(parser.nextToken());
}
public void testBinaryValueWithOffsetLength() throws Exception {
assertResult("{'binary':null}", () -> builder().startObject().field("binary").value(null, 0, 0).endObject());
final byte[] randomBytes = randomBytes();
final int offset = randomIntBetween(0, randomBytes.length);
final int length = randomIntBetween(1, Math.max(1, randomBytes.length - offset));
XContentBuilder builder = builder().startObject();
if (randomBoolean()) {
builder.field("bin", randomBytes, offset, length);
} else {
builder.field("bin").value(randomBytes, offset, length);
}
builder.endObject();
XContentParser parser = xcontentType().xContent().createParser(builder.bytes());
assertSame(parser.nextToken(), Token.START_OBJECT);
assertSame(parser.nextToken(), Token.FIELD_NAME);
assertEquals(parser.currentName(), "bin");
assertTrue(parser.nextToken().isValue());
assertArrayEquals(Arrays.copyOfRange(randomBytes, offset, offset + length), parser.binaryValue());
assertSame(parser.nextToken(), Token.END_OBJECT);
assertNull(parser.nextToken());
}
public void testBinaryUTF8() throws Exception {
assertResult("{'utf8':null}", () -> builder().startObject().utf8Field("utf8", null).endObject());
final BytesRef randomBytesRef = new BytesRef(randomBytes());
XContentBuilder builder = builder().startObject();
if (randomBoolean()) {
builder.utf8Field("utf8", randomBytesRef);
} else {
builder.field("utf8").utf8Value(randomBytesRef);
}
builder.endObject();
XContentParser parser = xcontentType().xContent().createParser(builder.bytes());
assertSame(parser.nextToken(), Token.START_OBJECT);
assertSame(parser.nextToken(), Token.FIELD_NAME);
assertEquals(parser.currentName(), "utf8");
assertTrue(parser.nextToken().isValue());
assertThat(parser.utf8Bytes().utf8ToString(), equalTo(randomBytesRef.utf8ToString()));
assertSame(parser.nextToken(), Token.END_OBJECT);
assertNull(parser.nextToken());
}
public void testText() throws Exception {
assertResult("{'text':null}", () -> builder().startObject().field("text", (Text) null).endObject());
assertResult("{'text':''}", () -> builder().startObject().field("text", new Text("")).endObject());
assertResult("{'text':'foo bar'}", () -> builder().startObject().field("text", new Text("foo bar")).endObject());
final BytesReference random = new BytesArray(randomBytes());
XContentBuilder builder = builder().startObject().field("text", new Text(random)).endObject();
XContentParser parser = xcontentType().xContent().createParser(builder.bytes());
assertSame(parser.nextToken(), Token.START_OBJECT);
assertSame(parser.nextToken(), Token.FIELD_NAME);
assertEquals(parser.currentName(), "text");
assertTrue(parser.nextToken().isValue());
assertThat(parser.utf8Bytes().utf8ToString(), equalTo(random.utf8ToString()));
assertSame(parser.nextToken(), Token.END_OBJECT);
assertNull(parser.nextToken());
}
public void testReadableInstant() throws Exception {
assertResult("{'instant':null}", () -> builder().startObject().field("instant", (ReadableInstant) null).endObject());
assertResult("{'instant':null}", () -> builder().startObject().field("instant").value((ReadableInstant) null).endObject());
final DateTime t1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC);
String expected = "{'t1':'2016-01-01T00:00:00.000Z'}";
assertResult(expected, () -> builder().startObject().field("t1", t1).endObject());
assertResult(expected, () -> builder().startObject().field("t1").value(t1).endObject());
final DateTime t2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
expected = "{'t2':'2016-12-25T07:59:42.213Z'}";
assertResult(expected, () -> builder().startObject().field("t2", t2).endObject());
assertResult(expected, () -> builder().startObject().field("t2").value(t2).endObject());
final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis());
final DateTime t3 = DateTime.now();
expected = "{'t3':'" + formatter.print(t3) + "'}";
assertResult(expected, () -> builder().startObject().field("t3", t3, formatter).endObject());
assertResult(expected, () -> builder().startObject().field("t3").value(t3, formatter).endObject());
final DateTime t4 = new DateTime(randomDateTimeZone());
expected = "{'t4':'" + formatter.print(t4) + "'}";
assertResult(expected, () -> builder().startObject().field("t4", t4, formatter).endObject());
assertResult(expected, () -> builder().startObject().field("t4").value(t4, formatter).endObject());
long date = Math.abs(randomLong() % (2 * (long) 10e11)); // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
final DateTime t5 = new DateTime(date, randomDateTimeZone());
expected = "{'t5':'" + XContentBuilder.DEFAULT_DATE_PRINTER.print(t5) + "'}";
assertResult(expected, () -> builder().startObject().field("t5", t5).endObject());
assertResult(expected, () -> builder().startObject().field("t5").value(t5).endObject());
expected = "{'t5':'" + formatter.print(t5) + "'}";
assertResult(expected, () -> builder().startObject().field("t5", t5, formatter).endObject());
assertResult(expected, () -> builder().startObject().field("t5").value(t5, formatter).endObject());
Instant i1 = new Instant(1451606400000L); // 2016-01-01T00:00:00.000Z
expected = "{'i1':'2016-01-01T00:00:00.000Z'}";
assertResult(expected, () -> builder().startObject().field("i1", i1).endObject());
assertResult(expected, () -> builder().startObject().field("i1").value(i1).endObject());
Instant i2 = new Instant(1482652782213L); // 2016-12-25T07:59:42.213Z
expected = "{'i2':'" + formatter.print(i2) + "'}";
assertResult(expected, () -> builder().startObject().field("i2", i2, formatter).endObject());
assertResult(expected, () -> builder().startObject().field("i2").value(i2, formatter).endObject());
expectNonNullFormatterException(() -> builder().startObject().field("t3", t3, null).endObject());
expectNonNullFormatterException(() -> builder().startObject().field("t3").value(t3, null).endObject());
}
public void testDate() throws Exception {
assertResult("{'date':null}", () -> builder().startObject().field("date", (Date) null).endObject());
assertResult("{'date':null}", () -> builder().startObject().field("date").value((Date) null).endObject());
final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
assertResult("{'d1':'2016-01-01T00:00:00.000Z'}", () -> builder().startObject().field("d1", d1).endObject());
assertResult("{'d1':'2016-01-01T00:00:00.000Z'}", () -> builder().startObject().field("d1").value(d1).endObject());
final Date d2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC).toDate();
assertResult("{'d2':'2016-12-25T07:59:42.213Z'}", () -> builder().startObject().field("d2", d2).endObject());
assertResult("{'d2':'2016-12-25T07:59:42.213Z'}", () -> builder().startObject().field("d2").value(d2).endObject());
final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis());
final Date d3 = DateTime.now().toDate();
String expected = "{'d3':'" + formatter.print(d3.getTime()) + "'}";
assertResult(expected, () -> builder().startObject().field("d3", d3, formatter).endObject());
assertResult(expected, () -> builder().startObject().field("d3").value(d3, formatter).endObject());
expectNonNullFormatterException(() -> builder().startObject().field("d3", d3, null).endObject());
expectNonNullFormatterException(() -> builder().startObject().field("d3").value(d3, null).endObject());
expectNonNullFormatterException(() -> builder().value(null, 1L));
}
public void testDateField() throws Exception {
final Date d = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
assertResult("{'date_in_millis':1451606400000}", () -> builder()
.startObject()
.dateField("date_in_millis", "date", d.getTime())
.endObject());
assertResult("{'date':'2016-01-01T00:00:00.000Z','date_in_millis':1451606400000}", () -> builder()
.humanReadable(true)
.startObject
().dateField("date_in_millis", "date", d.getTime())
.endObject());
}
public void testCalendar() throws Exception {
Calendar calendar = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT);
assertResult("{'calendar':'2016-01-01T00:00:00.000Z'}", () -> builder()
.startObject()
.field("calendar")
.value(calendar)
.endObject());
}
public void testGeoPoint() throws Exception {
assertResult("{'geo':null}", () -> builder().startObject().field("geo", (GeoPoint) null).endObject());
assertResult("{'geo':{'lat':52.4267578125,'lon':13.271484375}}", () -> builder()
.startObject()
. field("geo", GeoPoint.fromGeohash("u336q"))
.endObject());
assertResult("{'geo':{'lat':52.5201416015625,'lon':13.4033203125}}", () -> builder()
.startObject()
.field("geo")
.value(GeoPoint.fromGeohash("u33dc1"))
.endObject());
}
public void testLatLon() throws Exception {
final String expected = "{'latlon':{'lat':13.271484375,'lon':52.4267578125}}";
assertResult(expected, () -> builder().startObject().latlon("latlon", 13.271484375, 52.4267578125).endObject());
assertResult(expected, () -> builder().startObject().field("latlon").latlon(13.271484375, 52.4267578125).endObject());
}
public void testPath() throws Exception {
assertResult("{'path':null}", () -> builder().startObject().field("path", (Path) null).endObject());
Path path = PathUtils.get("first", "second", "third");
assertResult("{'path':'first/second/third'}", () -> builder().startObject().field("path", path).endObject());
}
public void testObjects() throws Exception {
Map<String, Object[]> objects = new HashMap<>();
objects.put("{'objects':[false,true,false]}", new Object[]{false, true, false});
objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[]{(byte) 1, (byte) 1, (byte) 2, (byte) 3, (byte) 5, (byte) 8, (byte) 13});
objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}", new Object[]{1.0d, 1.0d, 2.0d, 3.0d, 5.0d, 8.0d, 13.0d});
objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}", new Object[]{1.0f, 1.0f, 2.0f, 3.0f, 5.0f, 8.0f, 13.0f});
objects.put("{'objects':[{'lat':45.759429931640625,'lon':4.8394775390625}]}", new Object[]{GeoPoint.fromGeohash("u05kq4k")});
objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[]{1, 1, 2, 3, 5, 8, 13});
objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[]{1L, 1L, 2L, 3L, 5L, 8L, 13L});
objects.put("{'objects':[1,1,2,3,5,8]}", new Object[]{(short) 1, (short) 1, (short) 2, (short) 3, (short) 5, (short) 8});
objects.put("{'objects':['a','b','c']}", new Object[]{"a", "b", "c"});
objects.put("{'objects':['a','b','c']}", new Object[]{new Text("a"), new Text(new BytesArray("b")), new Text("c")});
objects.put("{'objects':['a/b/c','d/e']}", new Object[]{PathUtils.get("a", "b", "c"), PathUtils.get("d", "e")});
objects.put("{'objects':null}", null);
objects.put("{'objects':[null,null,null]}", new Object[]{null, null, null});
objects.put("{'objects':['OPEN','CLOSE']}", IndexMetaData.State.values());
objects.put("{'objects':[{'f1':'v1'},{'f2':'v2'}]}", new Object[]{singletonMap("f1", "v1"), singletonMap("f2", "v2")});
objects.put("{'objects':[[1,2,3],[4,5]]}", new Object[]{Arrays.asList(1, 2, 3), Arrays.asList(4, 5)});
final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
final Date d2 = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
objects.put("{'objects':['" + formatter.print(d1.getTime()) + "','" + formatter.print(d2.getTime()) + "']}", new Object[]{d1, d2});
final DateTime dt1 = DateTime.now();
final DateTime dt2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
objects.put("{'objects':['" + formatter.print(dt1) + "','2016-12-25T07:59:42.213Z']}", new Object[]{dt1, dt2});
final Calendar c1 = new DateTime(2012, 7, 7, 10, 23, DateTimeZone.UTC).toCalendar(Locale.ROOT);
final Calendar c2 = new DateTime(2014, 11, 16, 19, 36, DateTimeZone.UTC).toCalendar(Locale.ROOT);
objects.put("{'objects':['2012-07-07T10:23:00.000Z','2014-11-16T19:36:00.000Z']}", new Object[]{c1, c2});
final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject();
final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1).endObject();
objects.put("{'objects':[{'f1':'v1','f2':2,'f3':[3,4,5]},{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}]}", new Object[]{x1, x2});
for (Map.Entry<String, Object[]> o : objects.entrySet()) {
final String expected = o.getKey();
assertResult(expected, () -> builder().startObject().field("objects", o.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("objects").value(o.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("objects").values(o.getValue()).endObject());
assertResult(expected, () -> builder().startObject().array("objects", o.getValue()).endObject());
}
}
public void testObject() throws Exception {
Map<String, Object> object = new HashMap<>();
object.put("{'object':false}", Boolean.FALSE);
object.put("{'object':13}", (byte) 13);
object.put("{'object':5.0}", 5.0d);
object.put("{'object':8.0}", 8.0f);
object.put("{'object':{'lat':45.759429931640625,'lon':4.8394775390625}}", GeoPoint.fromGeohash("u05kq4k"));
object.put("{'object':3}", 3);
object.put("{'object':2}", 2L);
object.put("{'object':1}", (short) 1);
object.put("{'object':'string'}", "string");
object.put("{'object':'a'}", new Text("a"));
object.put("{'object':'b'}", new Text(new BytesArray("b")));
object.put("{'object':'a/b/c'}", PathUtils.get("a", "b", "c"));
object.put("{'object':null}", null);
object.put("{'object':'OPEN'}", IndexMetaData.State.OPEN);
object.put("{'object':'NM'}", DistanceUnit.NAUTICALMILES);
object.put("{'object':{'f1':'v1'}}", singletonMap("f1", "v1"));
object.put("{'object':{'f1':{'f2':'v2'}}}", singletonMap("f1", singletonMap("f2", "v2")));
object.put("{'object':[1,2,3]}", Arrays.asList(1, 2, 3));
final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
object.put("{'object':'" + formatter.print(d1.getTime()) + "'}", d1);
final DateTime d2 = DateTime.now();
object.put("{'object':'" + formatter.print(d2) + "'}", d2);
final Calendar c1 = new DateTime(2010, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT);
object.put("{'object':'2010-01-01T00:00:00.000Z'}", c1);
final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject();
final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1).endObject();
object.put("{'object':{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}}", x2);
for (Map.Entry<String, Object> o : object.entrySet()) {
final String expected = o.getKey();
assertResult(expected, () -> builder().humanReadable(true).startObject().field("object", o.getValue()).endObject());
assertResult(expected, () -> builder().humanReadable(true).startObject().field("object").value(o.getValue()).endObject());
}
assertResult("{'objects':[null,null,null]}", () -> builder().startObject().array("objects", null, null, null).endObject());
}
public void testToXContent() throws Exception {
assertResult("{'xcontent':null}", () -> builder().startObject().field("xcontent", (ToXContent) null).endObject());
assertResult("{'xcontent':null}", () -> builder().startObject().field("xcontent").value((ToXContent) null).endObject());
ToXContent xcontent0 = (builder, params) -> {
builder.startObject();
builder.field("field", "value");
builder.array("array", "1", "2", "3");
builder.startObject("foo");
builder.field("bar", "baz");
builder.endObject();
builder.endObject();
return builder;
};
assertResult("{'field':'value','array':['1','2','3'],'foo':{'bar':'baz'}}", () -> builder().value(xcontent0));
assertResult("{'xcontent':{'field':'value','array':['1','2','3'],'foo':{'bar':'baz'}}}", () -> builder()
.startObject()
.field("xcontent", xcontent0)
.endObject());
ToXContent xcontent1 = (builder, params) -> {
builder.startObject();
builder.field("field", "value");
builder.startObject("foo");
builder.field("bar", "baz");
builder.endObject();
builder.endObject();
return builder;
};
ToXContent xcontent2 = (builder, params) -> {
builder.startObject();
builder.field("root", xcontent0);
builder.array("childs", xcontent0, xcontent1);
builder.endObject();
return builder;
};
assertResult("{'root':{" +
"'field':'value'," +
"'array':['1','2','3']," +
"'foo':{'bar':'baz'}" +
"}," +
"'childs':[" +
"{'field':'value','array':['1','2','3'],'foo':{'bar':'baz'}}," +
"{'field':'value','foo':{'bar':'baz'}}" +
"]}", () -> builder().value(xcontent2));
}
public void testMap() throws Exception {
Map<String, Map<String, ?>> maps = new HashMap<>();
maps.put("{'map':null}", (Map) null);
maps.put("{'map':{}}", Collections.emptyMap());
maps.put("{'map':{'key':'value'}}", singletonMap("key", "value"));
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("string", "value");
innerMap.put("int", 42);
innerMap.put("long", 42L);
innerMap.put("long[]", new long[]{1L, 3L});
innerMap.put("path", PathUtils.get("path", "to", "file"));
innerMap.put("object", singletonMap("key", "value"));
maps.put("{'map':{'path':'path/to/file','string':'value','long[]':[1,3],'int':42,'long':42,'object':{'key':'value'}}}", innerMap);
for (Map.Entry<String, Map<String, ?>> m : maps.entrySet()) {
final String expected = m.getKey();
assertResult(expected, () -> builder().startObject().field("map", m.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("map").value(m.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("map").map(m.getValue()).endObject());
}
}
public void testIterable() throws Exception {
Map<String, Iterable<?>> iterables = new HashMap<>();
iterables.put("{'iter':null}", (Iterable) null);
iterables.put("{'iter':[]}", Collections.emptyList());
iterables.put("{'iter':['a','b']}", Arrays.asList("a", "b"));
iterables.put("{'iter':'path/to/file'}", PathUtils.get("path", "to", "file"));
iterables.put("{'iter':['a/b/c','c/d']}", Arrays.asList(PathUtils.get("a", "b", "c"), PathUtils.get("c", "d")));
for (Map.Entry<String, Iterable<?>> i : iterables.entrySet()) {
final String expected = i.getKey();
assertResult(expected, () -> builder().startObject().field("iter", i.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("iter").value(i.getValue()).endObject());
}
}
public void testUnknownObject() throws Exception {
Map<String, Object> objects = new HashMap<>();
objects.put("{'obj':50.63}", DistanceUnit.METERS.fromMeters(50.63));
objects.put("{'obj':'MINUTES'}", TimeUnit.MINUTES);
objects.put("{'obj':'class org.elasticsearch.common.xcontent.BaseXContentTestCase'}", BaseXContentTestCase.class);
for (Map.Entry<String, ?> o : objects.entrySet()) {
final String expected = o.getKey();
assertResult(expected, () -> builder().startObject().field("obj", o.getValue()).endObject());
assertResult(expected, () -> builder().startObject().field("obj").value(o.getValue()).endObject());
}
}
public void testBasics() throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
@ -56,7 +690,7 @@ public abstract class BaseXContentTestCase extends ESTestCase {
generator.writeNumber(2L);
}
});
assertEquals(e.getMessage(), "unclosed object or array found");
assertEquals(e.getMessage(), "Unclosed object or array found");
}
public void testMissingEndArray() throws IOException {
@ -67,11 +701,11 @@ public abstract class BaseXContentTestCase extends ESTestCase {
generator.writeNumber(2L);
}
});
assertEquals(e.getMessage(), "unclosed object or array found");
assertEquals(e.getMessage(), "Unclosed object or array found");
}
public void testRawField() throws Exception {
for (boolean useStream : new boolean[] {false, true}) {
for (boolean useStream : new boolean[]{false, true}) {
for (XContentType xcontentType : XContentType.values()) {
doTestRawField(xcontentType.xContent(), useStream);
}
@ -182,4 +816,76 @@ public abstract class BaseXContentTestCase extends ESTestCase {
assertEquals("bar", map.get("foo"));
assertEquals(bigInteger, map.get("bigint"));
}
public void testEnsureNameNotNull() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> XContentBuilder.ensureNameNotNull(null));
assertThat(e.getMessage(), containsString("Field name cannot be null"));
}
public void testFormatterNameNotNull() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> XContentBuilder.ensureFormatterNotNull(null));
assertThat(e.getMessage(), containsString("DateTimeFormatter cannot be null"));
}
public void testEnsureNotNull() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> XContentBuilder.ensureNotNull(null, "message"));
assertThat(e.getMessage(), containsString("message"));
XContentBuilder.ensureNotNull("foo", "No exception must be thrown");
}
private static void expectUnclosedException(ThrowingRunnable runnable) {
IllegalStateException e = expectThrows(IllegalStateException.class, runnable);
assertThat(e.getMessage(), containsString("Failed to close the XContentBuilder"));
assertThat(e.getCause(), allOf(notNullValue(), instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), containsString("Unclosed object or array found"));
}
private static void expectValueException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("expecting a value"));
}
private static void expectFieldException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("expecting field name"));
}
private static void expectNonNullFieldException(ThrowingRunnable runnable) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, runnable);
assertThat(e.getMessage(), containsString("Field name cannot be null"));
}
private static void expectNonNullFormatterException(ThrowingRunnable runnable) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, runnable);
assertThat(e.getMessage(), containsString("DateTimeFormatter cannot be null"));
}
private static void expectObjectException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("Current context not Object"));
}
private static void expectArrayException(ThrowingRunnable runnable) {
JsonGenerationException e = expectThrows(JsonGenerationException.class, runnable);
assertThat(e.getMessage(), containsString("Current context not Array"));
}
public static Matcher<String> equalToJson(String json) {
return Matchers.equalTo(json.replace("'", "\""));
}
private static void assertResult(String expected, Builder builder) throws IOException {
// Build the XContentBuilder, convert its bytes to JSON and check it matches
assertThat(XContentHelper.convertToJson(builder.build().bytes(), randomBoolean()), equalToJson(expected));
}
private static byte[] randomBytes() throws Exception {
return randomUnicodeOfLength(scaledRandomIntBetween(10, 1000)).getBytes("UTF-8");
}
@FunctionalInterface
private interface Builder {
XContentBuilder build() throws IOException;
}
}

View File

@ -173,9 +173,9 @@ public class XContentBuilderTests extends ESTestCase {
public void testDateTypesConversion() throws Exception {
Date date = new Date();
String expectedDate = XContentBuilder.defaultDatePrinter.print(date.getTime());
String expectedDate = XContentBuilder.DEFAULT_DATE_PRINTER.print(date.getTime());
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.ROOT);
String expectedCalendar = XContentBuilder.defaultDatePrinter.print(calendar.getTimeInMillis());
String expectedCalendar = XContentBuilder.DEFAULT_DATE_PRINTER.print(calendar.getTimeInMillis());
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject().field("date", date).endObject();
assertThat(builder.string(), equalTo("{\"date\":\"" + expectedDate + "\"}"));
@ -339,17 +339,17 @@ public class XContentBuilderTests extends ESTestCase {
builder.map(Collections.singletonMap(null, "test"));
fail("write map should have failed");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("field name cannot be null"));
assertThat(e.getMessage(), equalTo("Field name cannot be null"));
}
}
public void testWriteMapValueWithNullKeys() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
try {
builder.value(Collections.singletonMap(null, "test"));
builder.map(Collections.singletonMap(null, "test"));
fail("write map should have failed");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("field name cannot be null"));
assertThat(e.getMessage(), equalTo("Field name cannot be null"));
}
}
@ -360,7 +360,7 @@ public class XContentBuilderTests extends ESTestCase {
builder.field("map", Collections.singletonMap(null, "test"));
fail("write map should have failed");
} catch(IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("field name cannot be null"));
assertThat(e.getMessage(), equalTo("Field name cannot be null"));
}
}
@ -371,8 +371,8 @@ public class XContentBuilderTests extends ESTestCase {
builder.field("foo", true);
}
});
assertThat(e.getMessage(), equalTo("failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("unclosed object or array found"));
assertThat(e.getMessage(), equalTo("Failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("Unclosed object or array found"));
}
public void testMissingEndArray() throws IOException {
@ -384,7 +384,7 @@ public class XContentBuilderTests extends ESTestCase {
builder.value(1);
}
});
assertThat(e.getMessage(), equalTo("failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("unclosed object or array found"));
assertThat(e.getMessage(), equalTo("Failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("Unclosed object or array found"));
}
}

View File

@ -48,8 +48,10 @@ public class JsonVsCborTests extends ESTestCase {
xsonGen.writeStringField("test", "value");
jsonGen.writeStringField("test", "value");
xsonGen.writeArrayFieldStart("arr");
jsonGen.writeArrayFieldStart("arr");
xsonGen.writeFieldName("arr");
xsonGen.writeStartArray();
jsonGen.writeFieldName("arr");
jsonGen.writeStartArray();
xsonGen.writeNumber(1);
jsonGen.writeNumber(1);
xsonGen.writeNull();

View File

@ -48,8 +48,10 @@ public class JsonVsSmileTests extends ESTestCase {
xsonGen.writeStringField("test", "value");
jsonGen.writeStringField("test", "value");
xsonGen.writeArrayFieldStart("arr");
jsonGen.writeArrayFieldStart("arr");
xsonGen.writeFieldName("arr");
xsonGen.writeStartArray();
jsonGen.writeFieldName("arr");
jsonGen.writeStartArray();
xsonGen.writeNumber(1);
jsonGen.writeNumber(1);
xsonGen.writeNull();

View File

@ -139,7 +139,7 @@ public class XContentMapValuesTests extends ESTestCase {
// lists
builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1").field("test", "value1", "value2").endObject()
.startObject("path1").array("test", "value1", "value2").endObject()
.endObject();
try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(builder.string())) {

View File

@ -264,10 +264,10 @@ public class GetActionIT extends ESIntegTestCase {
assertThat(response.isExists(), equalTo(false));
client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()).get();
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
client().prepareIndex("test", "type2", "1")
.setSource(jsonBuilder().startObject().field("field", "1", "2").endObject()).get();
.setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
response = client().prepareGet("test", "type1", "1").setFields("field").get();
assertThat(response.isExists(), equalTo(true));

View File

@ -37,13 +37,6 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.mapper.CompletionFieldMapper;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.test.ESSingleNodeTestCase;
import java.io.IOException;
@ -285,7 +278,7 @@ public class CompletionFieldMapperTests extends ESSingleNodeTestCase {
.field("weight", 4)
.endObject()
.startObject()
.field("input", "suggestion4", "suggestion5", "suggestion6")
.array("input", "suggestion4", "suggestion5", "suggestion6")
.field("weight", 5)
.endObject()
.endArray()

View File

@ -28,16 +28,7 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.LegacyLongFieldMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.TextFieldMapper;
import org.elasticsearch.test.ESSingleNodeTestCase;
import java.util.Arrays;
@ -46,7 +37,6 @@ import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;

View File

@ -28,11 +28,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.test.InternalSettingsPlugin;
@ -87,7 +82,7 @@ public class SourceFieldMapperTests extends ESSingleNodeTestCase {
public void testIncludes() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_source").field("includes", new String[]{"path1*"}).endObject()
.startObject("_source").array("includes", new String[]{"path1*"}).endObject()
.endObject().endObject().string();
DocumentMapper documentMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -108,7 +103,7 @@ public class SourceFieldMapperTests extends ESSingleNodeTestCase {
public void testExcludes() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_source").field("excludes", new String[]{"path1*"}).endObject()
.startObject("_source").array("excludes", new String[]{"path1*"}).endObject()
.endObject().endObject().string();
DocumentMapper documentMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));

View File

@ -208,7 +208,7 @@ public class TextFieldMapperTests extends ESSingleNodeTestCase {
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", new String[] {"a", "b"})
.array("field", new String[] {"a", "b"})
.endObject()
.bytes());
@ -247,7 +247,7 @@ public class TextFieldMapperTests extends ESSingleNodeTestCase {
ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("field", new String[] {"a", "b"})
.array("field", new String[] {"a", "b"})
.endObject()
.bytes());

View File

@ -108,7 +108,7 @@ public class SimpleMgetIT extends ESIntegTestCase {
public void testThatSourceFilteringIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
BytesReference sourceBytesRef = jsonBuilder().startObject()
.field("field", "1", "2")
.array("field", "1", "2")
.startObject("included").field("field", "should be seen").field("hidden_field", "should not be seen").endObject()
.field("excluded", "should not be seen")
.endObject().bytes();

View File

@ -78,7 +78,7 @@ public class BooleanTermsIT extends ESIntegTestCase {
builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder()
.startObject()
.field(SINGLE_VALUED_FIELD_NAME, singleValue)
.field(MULTI_VALUED_FIELD_NAME, multiValue)
.array(MULTI_VALUED_FIELD_NAME, multiValue)
.endObject());
}
indexRandom(true, builders);

View File

@ -468,7 +468,7 @@ public class NestedIT extends ESIntegTestCase {
client().prepareIndex("idx4", "product", "1").setSource(jsonBuilder().startObject()
.field("name", "product1")
.field("categories", "1", "2", "3", "4")
.array("categories", "1", "2", "3", "4")
.startArray("property")
.startObject().field("id", 1).endObject()
.startObject().field("id", 2).endObject()
@ -477,7 +477,7 @@ public class NestedIT extends ESIntegTestCase {
.endObject()).get();
client().prepareIndex("idx4", "product", "2").setSource(jsonBuilder().startObject()
.field("name", "product2")
.field("categories", "1", "2")
.array("categories", "1", "2")
.startArray("property")
.startObject().field("id", 1).endObject()
.startObject().field("id", 5).endObject()

View File

@ -134,11 +134,11 @@ public class CardinalityIT extends ESIntegTestCase {
builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder()
.startObject()
.field("str_value", "s" + i)
.field("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
.array("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
.field("l_value", i)
.field("l_values", new int[] {i * 2, i * 2 + 1})
.array("l_values", new int[] {i * 2, i * 2 + 1})
.field("d_value", i)
.field("d_values", new double[]{i * 2, i * 2 + 1})
.array("d_values", new double[]{i * 2, i * 2 + 1})
.endObject());
}
indexRandom(true, builders);

View File

@ -1537,7 +1537,7 @@ public class HighlighterSearchIT extends ESIntegTestCase {
.addMapping("type1", "tags", "type=text"));
ensureGreen();
client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("tags",
.setSource(jsonBuilder().startObject().array("tags",
"this is a really long tag i would like to highlight",
"here is another one that is very long tag and has the tag token near the end").endObject()).get();
refresh();

View File

@ -230,7 +230,7 @@ public class GeoDistanceIT extends ESIntegTestCase {
.actionGet();
client().prepareIndex("test", "type1", "3")
.setSource(jsonBuilder().startObject().field("names", "Times Square", "Tribeca").startArray("locations")
.setSource(jsonBuilder().startObject().array("names", "Times Square", "Tribeca").startArray("locations")
// to NY: 5.286 km
.startObject().field("lat", 40.759011).field("lon", -73.9844722).endObject()
// to NY: 0.4621 km
@ -238,7 +238,7 @@ public class GeoDistanceIT extends ESIntegTestCase {
.execute().actionGet();
client().prepareIndex("test", "type1", "4")
.setSource(jsonBuilder().startObject().field("names", "Wall Street", "Soho").startArray("locations")
.setSource(jsonBuilder().startObject().array("names", "Wall Street", "Soho").startArray("locations")
// to NY: 1.055 km
.startObject().field("lat", 40.7051157).field("lon", -74.0088305).endObject()
// to NY: 1.258 km
@ -246,7 +246,7 @@ public class GeoDistanceIT extends ESIntegTestCase {
.execute().actionGet();
client().prepareIndex("test", "type1", "5")
.setSource(jsonBuilder().startObject().field("names", "Greenwich Village", "Brooklyn").startArray("locations")
.setSource(jsonBuilder().startObject().array("names", "Greenwich Village", "Brooklyn").startArray("locations")
// to NY: 2.029 km
.startObject().field("lat", 40.731033).field("lon", -73.9962255).endObject()
// to NY: 8.572 km
@ -355,14 +355,14 @@ public class GeoDistanceIT extends ESIntegTestCase {
ensureGreen();
client().prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("names", "Times Square", "Tribeca").startArray("locations")
.setSource(jsonBuilder().startObject().array("names", "Times Square", "Tribeca").startArray("locations")
// to NY: 5.286 km
.startObject().field("lat", 40.759011).field("lon", -73.9844722).endObject()
// to NY: 0.4621 km
.startObject().field("lat", 40.718266).field("lon", -74.007819).endObject().endArray().endObject())
.execute().actionGet();
client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("names", "Wall Street", "Soho").endObject())
client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().array("names", "Wall Street", "Soho").endObject())
.execute().actionGet();
refresh();

View File

@ -25,7 +25,6 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptPlugin;
@ -251,9 +250,9 @@ public class SimpleSortIT extends ESIntegTestCase {
.setSource(jsonBuilder()
.startObject()
.field("ord", i)
.field("svalue", new String[]{"" + i, "" + (i + 1), "" + (i + 2)})
.field("lvalue", new long[]{i, i + 1, i + 2})
.field("dvalue", new double[]{i, i + 1, i + 2})
.array("svalue", new String[]{"" + i, "" + (i + 1), "" + (i + 2)})
.array("lvalue", new long[]{i, i + 1, i + 2})
.array("dvalue", new double[]{i, i + 1, i + 2})
.startObject("gvalue")
.field("lat", (double) i + 1)
.field("lon", (double) i)

View File

@ -99,7 +99,7 @@ public class CompletionSuggestSearch2xIT extends ESIntegTestCase {
client().prepareIndex(INDEX, TYPE, "" + i)
.setSource(jsonBuilder()
.startObject().startObject(FIELD)
.field("input", input[i])
.array("input", input[i])
.endObject()
.endObject()
)
@ -941,7 +941,7 @@ public class CompletionSuggestSearch2xIT extends ESIntegTestCase {
builders[i] = client().prepareIndex(INDEX, TYPE, "" + i)
.setSource(jsonBuilder()
.startObject().startObject(FIELD)
.field("input", input[i])
.array("input", input[i])
.field("output", surface[i])
.startObject("payload").field("id", i).endObject()
.field("weight", 1) // WE FORCEFULLY INDEX A BOGUS WEIGHT
@ -955,7 +955,7 @@ public class CompletionSuggestSearch2xIT extends ESIntegTestCase {
builders[i] = client().prepareIndex(INDEX, TYPE, "n" + i)
.setSource(jsonBuilder()
.startObject().startObject(FIELD)
.field("input", input[i])
.array("input", input[i])
.field("output", surface[i])
.startObject("payload").field("id", i).endObject()
.field("weight", weight[i])

View File

@ -190,7 +190,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
.startObject("context")
.startObject("location")
.field("type", "geo")
.field("precision", precision)
.array("precision", precision)
.endObject()
.endObject()
.endObject().endObject()
@ -209,7 +209,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
.startObject("context")
.startObject("location")
.field("type", "geo")
.field("precision", precision)
.array("precision", precision)
.endObject()
.endObject()
.endObject().endObject()
@ -314,7 +314,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
{ "pizza - treptow", "pizza", "food" } };
for (int i = 0; i < locations.length; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", input[i])
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).array("input", input[i])
.startObject("context").field("st", locations[i]).endObject().field("payload", locations[i])
.endObject().endObject();
client().prepareIndex(INDEX, TYPE, "" + i).setSource(source).execute().actionGet();
@ -343,7 +343,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
.addMapping(TYPE, createMapping(TYPE, ContextBuilder.category("st"))));
for (int i = 0; i < HEROS.length; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", HEROS[i])
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).array("input", HEROS[i])
.startObject("context").field("st", i%3).endObject()
.startObject("payload").field("group", i % 3).field("id", i).endObject()
.endObject().endObject();
@ -376,12 +376,12 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
XContentBuilder doc1 = jsonBuilder();
doc1.startObject().startObject("suggest_field")
.field("input", "backpack_red")
.startObject("context").field("color", "red", "all_colors").endObject()
.startObject("context").array("color", "red", "all_colors").endObject()
.endObject().endObject();
XContentBuilder doc2 = jsonBuilder();
doc2.startObject().startObject("suggest_field")
.field("input", "backpack_green")
.startObject("context").field("color", "green", "all_colors").endObject()
.startObject("context").array("color", "green", "all_colors").endObject()
.endObject().endObject();
client().prepareIndex(INDEX, TYPE, "1")
@ -451,7 +451,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
client().prepareIndex(INDEX, TYPE, "" + i)
.setSource(
jsonBuilder().startObject().field("category", Integer.toString(i % 3)).startObject(FIELD)
.field("input", HEROS[i])
.array("input", HEROS[i])
.startObject("context").endObject().field("payload", Integer.toString(i % 3))
.endObject().endObject()).execute().actionGet();
}
@ -508,7 +508,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
client().prepareIndex(INDEX, TYPE, "" + i)
.setSource(
jsonBuilder().startObject().startArray("category").value(Integer.toString(i % 3)).value("other").endArray()
.startObject(FIELD).field("input", HEROS[i]).startObject("context").endObject()
.startObject(FIELD).array("input", HEROS[i]).startObject("context").endObject()
.field("payload", Integer.toString(i % 3)).endObject().endObject()).execute().actionGet();
}
@ -535,7 +535,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
client().prepareIndex(INDEX, TYPE, "" + i)
.setSource(
jsonBuilder().startObject().field("categoryA").value("" + (char) ('0' + (i % 3))).field("categoryB")
.value("" + (char) ('A' + (i % 3))).startObject(FIELD).field("input", HEROS[i])
.value("" + (char) ('A' + (i % 3))).startObject(FIELD).array("input", HEROS[i])
.startObject("context").endObject().field("payload", ((char) ('0' + (i % 3))) + "" + (char) ('A' + (i % 3)))
.endObject().endObject()).execute().actionGet();
}
@ -561,7 +561,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
for (int i = 0; i < HEROS.length; i++) {
String source = jsonBuilder().startObject().field("categoryA", "" + (char) ('0' + (i % 3)))
.field("categoryB", "" + (char) ('a' + (i % 3))).startObject(FIELD).field("input", HEROS[i])
.field("categoryB", "" + (char) ('a' + (i % 3))).startObject(FIELD).array("input", HEROS[i])
.startObject("context").endObject().startObject("payload").field("categoryA", "" + (char) ('0' + (i % 3)))
.field("categoryB", "" + (char) ('a' + (i % 3))).endObject().endObject().endObject().string();
client().prepareIndex(INDEX, TYPE, "" + i).setSource(source).execute().actionGet();
@ -599,7 +599,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
String type = types[i % types.length];
client().prepareIndex(INDEX, type, "" + i)
.setSource(
jsonBuilder().startObject().startObject(FIELD).field("input", HEROS[i])
jsonBuilder().startObject().startObject(FIELD).array("input", HEROS[i])
.startObject("context").endObject().field("payload", type).endObject().endObject()).execute().actionGet();
}

View File

@ -77,7 +77,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.field("weight", 4)
.endObject()
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.field("weight", 5)
.endObject()
.endArray()
@ -107,7 +107,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.startObject()
.startArray("completion")
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.startObject("contexts")
.field("ctx", "ctx1")
.endObject()
@ -139,7 +139,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder()
.startObject()
.startObject("completion")
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.startObject("contexts")
.array("ctx", "ctx1", "ctx2", "ctx3")
.endObject()
@ -175,7 +175,7 @@ public class CategoryContextMappingTests extends ESSingleNodeTestCase {
.startObject()
.startArray("completion")
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.field("weight", 5)
.startObject("contexts")
.array("ctx", "ctx1", "ctx2", "ctx3")

View File

@ -77,7 +77,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.field("weight", 4)
.endObject()
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.field("weight", 5)
.endObject()
.endArray()
@ -108,7 +108,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.startObject()
.startArray("completion")
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.startObject("contexts")
.startObject("ctx")
.field("lat", 43.6624803)
@ -143,7 +143,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder()
.startObject()
.startObject("completion")
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.startObject("contexts")
.startArray("ctx")
.startObject()
@ -188,7 +188,7 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
.startObject()
.startArray("completion")
.startObject()
.field("input", "suggestion5", "suggestion6", "suggestion7")
.array("input", "suggestion5", "suggestion6", "suggestion7")
.field("weight", 5)
.startObject("contexts")
.array("loc1", "ezs42e44yx96")

View File

@ -76,11 +76,11 @@ public class SimpleThreadPoolIT extends ESIntegTestCase {
builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder()
.startObject()
.field("str_value", "s" + i)
.field("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
.array("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
.field("l_value", i)
.field("l_values", new int[]{i * 2, i * 2 + 1})
.array("l_values", new int[]{i * 2, i * 2 + 1})
.field("d_value", i)
.field("d_values", new double[]{i * 2, i * 2 + 1})
.array("d_values", new double[]{i * 2, i * 2 + 1})
.endObject());
}
indexRandom(true, builders);

View File

@ -22,8 +22,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;
import org.elasticsearch.test.rest.yaml.Stash;
import java.io.IOException;
import java.util.HashMap;