Disable XContent auto closing of object and arrays

This commit is contained in:
Tanguy Leroux 2016-05-24 13:37:32 +02:00
parent de6b4f35b1
commit bdee8c2632
42 changed files with 308 additions and 209 deletions

View File

@ -192,6 +192,7 @@ public class ListTasksResponse extends BaseTasksResponse implements ToXContent {
builder.endObject(); builder.endObject();
builder.endObject(); builder.endObject();
} }
builder.endObject();
} else if ("parents".equals(groupBy)) { } else if ("parents".equals(groupBy)) {
builder.startObject("tasks"); builder.startObject("tasks");
for (TaskGroup group : getTaskGroups()) { for (TaskGroup group : getTaskGroups()) {

View File

@ -781,7 +781,7 @@ public final class XContentBuilder implements BytesStream, Releasable {
try { try {
generator.close(); generator.close();
} catch (IOException e) { } catch (IOException e) {
// ignore throw new IllegalStateException("failed to close the XContentBuilder", e);
} }
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.cbor; package org.elasticsearch.common.xcontent.cbor;
import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory; import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -50,6 +51,8 @@ public class CborXContent implements XContent {
static { static {
cborFactory = new CBORFactory(); cborFactory = new CBORFactory();
cborFactory.configure(CBORFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now... cborFactory.configure(CBORFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
// Do not automatically close unclosed objects/arrays in com.fasterxml.jackson.dataformat.cbor.CBORGenerator#close() method
cborFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
cborXContent = new CborXContent(); cborXContent = new CborXContent();
} }

View File

@ -73,6 +73,8 @@ public class JsonXContent implements XContent {
jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true); jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true); jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
jsonFactory.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now... jsonFactory.configure(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
// Do not automatically close unclosed objects/arrays in com.fasterxml.jackson.core.json.UTF8JsonGenerator#close() method
jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
jsonXContent = new JsonXContent(); jsonXContent = new JsonXContent();
} }

View File

@ -389,6 +389,10 @@ public class JsonXContentGenerator implements XContentGenerator {
if (generator.isClosed()) { if (generator.isClosed()) {
return; return;
} }
JsonStreamContext context = generator.getOutputContext();
if ((context != null) && (context.inRoot() == false)) {
throw new IOException("unclosed object or array found");
}
if (writeLineFeedAtEnd) { if (writeLineFeedAtEnd) {
flush(); flush();
generator.writeRaw(LF); generator.writeRaw(LF);

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.smile; package org.elasticsearch.common.xcontent.smile;
import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.smile.SmileGenerator; import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -51,6 +52,8 @@ public class SmileXContent implements XContent {
smileFactory = new SmileFactory(); smileFactory = new SmileFactory();
smileFactory.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, false); // for now, this is an overhead, might make sense for web sockets smileFactory.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, false); // for now, this is an overhead, might make sense for web sockets
smileFactory.configure(SmileFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now... smileFactory.configure(SmileFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW, false); // this trips on many mappings now...
// Do not automatically close unclosed objects/arrays in com.fasterxml.jackson.dataformat.smile.SmileGenerator#close() method
smileFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false);
smileXContent = new SmileXContent(); smileXContent = new SmileXContent();
} }

View File

@ -99,7 +99,9 @@ public class RestFieldStatsAction extends BaseRestHandler {
for (Map.Entry<String, String> entry : response.getConflicts().entrySet()) { for (Map.Entry<String, String> entry : response.getConflicts().entrySet()) {
builder.field(entry.getKey(), entry.getValue()); builder.field(entry.getKey(), entry.getValue());
} }
builder.endObject();
} }
builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder); return new BytesRestResponse(RestStatus.OK, builder);
} }
}); });

View File

@ -94,6 +94,7 @@ public final class ScriptMetaData implements MetaData.Custom {
// because the parsers current location is already beyond the beginning we need to add a START_OBJECT: // because the parsers current location is already beyond the beginning we need to add a START_OBJECT:
builder.startObject(); builder.startObject();
builder.copyCurrentStructure(parser); builder.copyCurrentStructure(parser);
builder.endObject();
break; break;
} }
return builder.string(); return builder.string();

View File

@ -27,6 +27,8 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
public abstract class BaseXContentTestCase extends ESTestCase { public abstract class BaseXContentTestCase extends ESTestCase {
public abstract XContentType xcontentType(); public abstract XContentType xcontentType();
@ -41,6 +43,29 @@ public abstract class BaseXContentTestCase extends ESTestCase {
assertEquals(xcontentType(), XContentFactory.xContentType(data)); assertEquals(xcontentType(), XContentFactory.xContentType(data));
} }
public void testMissingEndObject() throws IOException {
IOException e = expectThrows(IOException.class, () -> {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
generator.writeStartObject();
generator.writeFieldName("foo");
generator.writeNumber(2L);
}
});
assertEquals(e.getMessage(), "unclosed object or array found");
}
public void testMissingEndArray() throws IOException {
IOException e = expectThrows(IOException.class, () -> {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
generator.writeStartArray();
generator.writeNumber(2L);
}
});
assertEquals(e.getMessage(), "unclosed object or array found");
}
public void testRawField() throws Exception { public void testRawField() throws Exception {
for (boolean useStream : new boolean[] {false, true}) { for (boolean useStream : new boolean[] {false, true}) {
for (XContentType xcontentType : XContentType.values()) { for (XContentType xcontentType : XContentType.values()) {

View File

@ -363,4 +363,28 @@ public class XContentBuilderTests extends ESTestCase {
assertThat(e.getMessage(), equalTo("field name cannot be null")); assertThat(e.getMessage(), equalTo("field name cannot be null"));
} }
} }
public void testMissingEndObject() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
try (XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()))) {
builder.startObject();
builder.field("foo", true);
}
});
assertThat(e.getMessage(), equalTo("failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("unclosed object or array found"));
}
public void testMissingEndArray() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
try (XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()))) {
builder.startObject();
builder.startArray("foo");
builder.value(0);
builder.value(1);
}
});
assertThat(e.getMessage(), equalTo("failed to close the XContentBuilder"));
assertThat(e.getCause().getMessage(), equalTo("unclosed object or array found"));
}
} }

View File

@ -627,7 +627,7 @@ public class GetActionIT extends ESIntegTestCase {
.startObject("field4").field("type", "text").field("store", true) .startObject("field4").field("type", "text").field("store", true)
.endObject().endObject() .endObject().endObject()
.endObject().endObject() .endObject().endObject()
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject())); .endObject().endObject().endObject()));
BytesReference source = jsonBuilder().startObject() BytesReference source = jsonBuilder().startObject()

View File

@ -109,7 +109,7 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
.field("type", "object") .field("type", "object")
.field("dynamic", true) .field("dynamic", true)
.startObject("properties") .startObject("properties")
.endObject().endObject().endObject().endObject().string(); .endObject().endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping)); DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
BytesReference bytes = XContentFactory.jsonBuilder() BytesReference bytes = XContentFactory.jsonBuilder()
.startObject().startObject("foo") .startObject().startObject("foo")
@ -129,7 +129,7 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
.field("type", "object") .field("type", "object")
.field("dynamic", true) .field("dynamic", true)
.startObject("properties") .startObject("properties")
.endObject().endObject().endObject().endObject().string(); .endObject().endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping)); DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
BytesReference bytes = XContentFactory.jsonBuilder() BytesReference bytes = XContentFactory.jsonBuilder()
.startObject().startObject("foo").startObject("bar") .startObject().startObject("foo").startObject("bar")
@ -148,7 +148,7 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
.startObject("foo") .startObject("foo")
.field("type", "object") .field("type", "object")
.startObject("properties") .startObject("properties")
.endObject().endObject().endObject().endObject().string(); .endObject().endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping)); DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
BytesReference bytes = XContentFactory.jsonBuilder() BytesReference bytes = XContentFactory.jsonBuilder()
.startObject().startObject("foo") .startObject().startObject("foo")

View File

@ -65,6 +65,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
.startObject() .startObject()
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
@ -85,6 +86,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
.startObject() .startObject()
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
@ -102,27 +104,21 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
try { StrictDynamicMappingException e = expectThrows(StrictDynamicMappingException.class, () -> defaultMapper.parse("test", "type", "1", jsonBuilder()
defaultMapper.parse("test", "type", "1", jsonBuilder() .startObject()
.startObject() .field("field1", "value1")
.field("field1", "value1") .field("field2", "value2")
.field("field2", "value2") .endObject()
.bytes()); .bytes()));
fail(); assertThat(e.getMessage(), equalTo("mapping set to strict, dynamic introduction of [field2] within [type] is not allowed"));
} catch (StrictDynamicMappingException e) {
// all is well
}
try { e = expectThrows(StrictDynamicMappingException.class, () -> defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
.startObject() .startObject()
.field("field1", "value1") .field("field1", "value1")
.field("field2", (String) null) .field("field2", (String) null)
.bytes()); .endObject()
fail(); .bytes()));
} catch (StrictDynamicMappingException e) { assertThat(e.getMessage(), equalTo("mapping set to strict, dynamic introduction of [field2] within [type] is not allowed"));
// all is well
}
} }
public void testDynamicFalseWithInnerObjectButDynamicSetOnRoot() throws IOException { public void testDynamicFalseWithInnerObjectButDynamicSetOnRoot() throws IOException {
@ -142,6 +138,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject() .endObject()
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("obj1.field1"), equalTo("value1")); assertThat(doc.rootDoc().get("obj1.field1"), equalTo("value1"));
@ -160,17 +157,15 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
try { StrictDynamicMappingException e = expectThrows(StrictDynamicMappingException.class, () ->
defaultMapper.parse("test", "type", "1", jsonBuilder() defaultMapper.parse("test", "type", "1", jsonBuilder()
.startObject().startObject("obj1") .startObject().startObject("obj1")
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject() .endObject()
.bytes()); .endObject()
fail(); .bytes()));
} catch (StrictDynamicMappingException e) { assertThat(e.getMessage(), equalTo("mapping set to strict, dynamic introduction of [field2] within [obj1] is not allowed"));
// all is well
}
} }
public void testDynamicMappingOnEmptyString() throws Exception { public void testDynamicMappingOnEmptyString() throws Exception {
@ -220,7 +215,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
DocumentMapperParser parser = indexService.mapperService().documentMapperParser(); DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("foo").field("type", "text").endObject().endObject() .startObject("properties").startObject("foo").field("type", "text").endObject().endObject()
.endObject().string(); .endObject().endObject().string();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
Mapper update = parse(mapper, parser, XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()); Mapper update = parse(mapper, parser, XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject());
@ -263,7 +258,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
// every new field introduction runs in linear time with the total number of fields // every new field introduction runs in linear time with the total number of fields
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("foo").field("type", "text").endObject().endObject() .startObject("properties").startObject("foo").field("type", "text").endObject().endObject()
.endObject().string(); .endObject().endObject().string();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
assertEquals(mapping, serialize(mapper)); assertEquals(mapping, serialize(mapper));
@ -283,7 +278,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject().endObject().string(), serialize(update)); .endObject().endObject().endObject().string(), serialize(update));
} }
public void testIntroduceTwoFields() throws Exception { public void testIntroduceTwoFields() throws Exception {
@ -318,7 +313,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject().endObject().string(), serialize(update)); .endObject().endObject().endObject().string(), serialize(update));
} }
public void testObject() throws Exception { public void testObject() throws Exception {

View File

@ -37,6 +37,7 @@ import java.util.Collection;
import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
/** /**
*/ */
@ -59,6 +60,7 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.startObject("float_field").field("type", "float").startObject("norms").field("enabled", true).endObject().endObject() .startObject("float_field").field("type", "float").startObject("norms").field("enabled", true).endObject().endObject()
.startObject("long_field").field("type", "long").startObject("norms").field("enabled", true).endObject().endObject() .startObject("long_field").field("type", "long").startObject("norms").field("enabled", true).endObject().endObject()
.startObject("short_field").field("type", "short").startObject("norms").field("enabled", true).endObject().endObject() .startObject("short_field").field("type", "short").startObject("norms").field("enabled", true).endObject().endObject()
.endObject().endObject().endObject()
.string(); .string();
DocumentMapper docMapper = createIndex("test", BW_SETTINGS).mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test", BW_SETTINGS).mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
@ -71,6 +73,7 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.startObject("float_field").field("boost", 7.0).field("value", 40.0).endObject() .startObject("float_field").field("boost", 7.0).field("value", 40.0).endObject()
.startObject("long_field").field("boost", 8.0).field("value", 50).endObject() .startObject("long_field").field("boost", 8.0).field("value", 50).endObject()
.startObject("short_field").field("boost", 9.0).field("value", 60).endObject() .startObject("short_field").field("boost", 9.0).field("value", 60).endObject()
.endObject()
.bytes(); .bytes();
Document doc = docMapper.parse("test", "person", "1", json).rootDoc(); Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
@ -109,6 +112,7 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.startObject("float_field").field("type", "float").field("boost", "7.0").endObject() .startObject("float_field").field("type", "float").field("boost", "7.0").endObject()
.startObject("long_field").field("type", "long").field("boost", "8.0").endObject() .startObject("long_field").field("type", "long").field("boost", "8.0").endObject()
.startObject("short_field").field("type", "short").field("boost", "9.0").endObject() .startObject("short_field").field("type", "short").field("boost", "9.0").endObject()
.endObject().endObject().endObject()
.string(); .string();
{ {
@ -122,6 +126,7 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.field("float_field", 40.0) .field("float_field", 40.0)
.field("long_field", 50) .field("long_field", 50)
.field("short_field", 60) .field("short_field", 60)
.endObject()
.bytes(); .bytes();
Document doc = docMapper.parse("test", "person", "1", json).rootDoc(); Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
@ -161,6 +166,7 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.field("float_field", 40.0) .field("float_field", 40.0)
.field("long_field", 50) .field("long_field", 50)
.field("short_field", 60) .field("short_field", 60)
.endObject()
.bytes(); .bytes();
Document doc = docMapper.parse("test", "person", "1", json).rootDoc(); Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
@ -200,79 +206,80 @@ public class FieldLevelBoostTests extends ESSingleNodeTestCase {
.startObject("float_field").field("type", "float").startObject("norms").field("enabled", true).endObject().endObject() .startObject("float_field").field("type", "float").startObject("norms").field("enabled", true).endObject().endObject()
.startObject("long_field").field("type", "long").startObject("norms").field("enabled", true).endObject().endObject() .startObject("long_field").field("type", "long").startObject("norms").field("enabled", true).endObject().endObject()
.startObject("short_field").field("type", "short").startObject("norms").field("enabled", true).endObject().endObject() .startObject("short_field").field("type", "short").startObject("norms").field("enabled", true).endObject().endObject()
.endObject().endObject().endObject()
.string(); .string();
DocumentMapper docMapper = createIndex("test", BW_SETTINGS).mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test", BW_SETTINGS).mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("str_field").field("foo", "bar") .startObject("str_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("int_field").field("foo", "bar") .startObject("int_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("byte_field").field("foo", "bar") .startObject("byte_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("date_field").field("foo", "bar") .startObject("date_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("double_field").field("foo", "bar") .startObject("double_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("float_field").field("foo", "bar") .startObject("float_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("long_field").field("foo", "bar") .startObject("long_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
try { try {
docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject() docMapper.parse("test", "person", "1", XContentFactory.jsonBuilder().startObject()
.startObject("short_field").field("foo", "bar") .startObject("short_field").field("foo", "bar")
.endObject().bytes()).rootDoc(); .endObject().endObject().bytes()).rootDoc();
fail(); fail();
} catch (MapperParsingException ex) { } catch (Exception ex) {
// Expected assertThat(ex, instanceOf(MapperParsingException.class));
} }
} }

View File

@ -57,6 +57,7 @@ public class CompoundTypesTests extends ESSingleNodeTestCase {
.startObject() .startObject()
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
@ -67,6 +68,7 @@ public class CompoundTypesTests extends ESSingleNodeTestCase {
.startObject() .startObject()
.startObject("field1").field("value", "value1").field("boost", 2.0f).endObject() .startObject("field1").field("value", "value1").field("boost", 2.0f).endObject()
.field("field2", "value2") .field("field2", "value2")
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field1"), equalTo("value1"));
@ -77,6 +79,7 @@ public class CompoundTypesTests extends ESSingleNodeTestCase {
.startObject() .startObject()
.field("field1", "value1") .field("field1", "value1")
.field("field2", "value2") .field("field2", "value2")
.endObject()
.bytes()); .bytes());
assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field1"), equalTo("value1"));

View File

@ -106,7 +106,8 @@ public class CopyToMapperIntegrationIT extends ESIntegTestCase {
.field("copy_to", "{name}_raw").endObject() .field("copy_to", "{name}_raw").endObject()
.endObject().endObject() .endObject().endObject()
.endArray(); .endArray()
.endObject().endObject();
} }
} }

View File

@ -241,6 +241,7 @@ public class MultiFieldTests extends ESSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
MapperService mapperService = createIndex("test").mapperService(); MapperService mapperService = createIndex("test").mapperService();

View File

@ -117,7 +117,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").startObject("properties") .startObject("nested1").field("type", "nested").startObject("properties")
.startObject("nested2").field("type", "nested") .startObject("nested2").field("type", "nested")
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -168,7 +168,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").startObject("properties") .startObject("nested1").field("type", "nested").startObject("properties")
.startObject("nested2").field("type", "nested").field("include_in_parent", true) .startObject("nested2").field("type", "nested").field("include_in_parent", true)
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -219,7 +219,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").field("include_in_parent", true).startObject("properties") .startObject("nested1").field("type", "nested").field("include_in_parent", true).startObject("properties")
.startObject("nested2").field("type", "nested").field("include_in_parent", true) .startObject("nested2").field("type", "nested").field("include_in_parent", true)
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -270,7 +270,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").startObject("properties") .startObject("nested1").field("type", "nested").startObject("properties")
.startObject("nested2").field("type", "nested").field("include_in_root", true) .startObject("nested2").field("type", "nested").field("include_in_root", true)
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -321,7 +321,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("nested1").field("type", "nested").field("dynamic", "strict").startObject("properties") .startObject("nested1").field("type", "nested").field("dynamic", "strict").startObject("properties")
.startObject("field1").field("type", "text") .startObject("field1").field("type", "text")
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
@ -355,7 +355,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
return XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties") return XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties")
.startObject("nested1").field("type", "nested").startObject("properties") .startObject("nested1").field("type", "nested").startObject("properties")
.startObject("nested2").field("type", "nested") .startObject("nested2").field("type", "nested")
.endObject().endObject() .endObject().endObject().endObject()
.endObject().endObject().endObject().string(); .endObject().endObject().endObject().string();
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
@ -390,7 +390,7 @@ public class NestedMappingTests extends ESSingleNodeTestCase {
mapperService.merge("type2", new CompressedXContent(mapping.apply("type2")), MergeReason.MAPPING_UPDATE, false); mapperService.merge("type2", new CompressedXContent(mapping.apply("type2")), MergeReason.MAPPING_UPDATE, false);
// adding new fields from different type is not ok // adding new fields from different type is not ok
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type3").startObject("properties").startObject("nested3") String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type3").startObject("properties").startObject("nested3")
.field("type", "nested").startObject("properties").endObject().endObject().endObject().endObject().string(); .field("type", "nested").startObject("properties").endObject().endObject().endObject().endObject().endObject().string();
try { try {
mapperService.merge("type3", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE, false); mapperService.merge("type3", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE, false);
fail("Expected IllegalArgumentException"); fail("Expected IllegalArgumentException");

View File

@ -125,7 +125,7 @@ public class SimpleMapperTests extends ESSingleNodeTestCase {
DocumentMapperParser mapperParser = indexService.mapperService().documentMapperParser(); DocumentMapperParser mapperParser = indexService.mapperService().documentMapperParser();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties")
.startObject("foo.bar").field("type", "text").endObject() .startObject("foo.bar").field("type", "text").endObject()
.endObject().endObject().string(); .endObject().endObject().endObject().string();
try { try {
mapperParser.parse("type", new CompressedXContent(mapping)); mapperParser.parse("type", new CompressedXContent(mapping));
fail("Mapping parse should have failed"); fail("Mapping parse should have failed");

View File

@ -136,6 +136,7 @@ public class StringFieldMapperPositionIncrementGapTests extends ESSingleNodeTest
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("string"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("string");
mapping.field("type", "string"); mapping.field("type", "string");
mapping.field("position_increment_gap", positionIncrementGap); mapping.field("position_increment_gap", positionIncrementGap);
mapping.endObject().endObject().endObject();
client().admin().indices().prepareCreate("test") client().admin().indices().prepareCreate("test")
.setSettings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_3_0).build()) .setSettings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_3_0).build())
.addMapping("test", mapping) .addMapping("test", mapping)
@ -151,6 +152,7 @@ public class StringFieldMapperPositionIncrementGapTests extends ESSingleNodeTest
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("string"); XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("string");
mapping.field("type", "string"); mapping.field("type", "string");
mapping.field("analyzer", analyzer); mapping.field("analyzer", analyzer);
mapping.endObject().endObject().endObject();
client().admin().indices().prepareCreate("test") client().admin().indices().prepareCreate("test")
.addMapping("test", mapping) .addMapping("test", mapping)
.setSettings(settings) .setSettings(settings)

View File

@ -69,7 +69,7 @@ public class TimestampMappingTests extends ESSingleNodeTestCase {
} }
public void testSimpleDisabled() throws Exception { public void testSimpleDisabled() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string(); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
BytesReference source = XContentFactory.jsonBuilder() BytesReference source = XContentFactory.jsonBuilder()
.startObject() .startObject()
@ -104,8 +104,8 @@ public class TimestampMappingTests extends ESSingleNodeTestCase {
version = randomVersion(random()); version = randomVersion(random());
} while (version.before(Version.V_2_0_0_beta1)); } while (version.before(Version.V_2_0_0_beta1));
for (String mapping : Arrays.asList( for (String mapping : Arrays.asList(
XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string(), XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string(),
XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_timestamp").endObject().endObject().string())) { XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_timestamp").endObject().endObject().endObject().string())) {
DocumentMapper docMapper = createIndex("test", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build()).mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build()).mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
assertThat(docMapper.timestampFieldMapper().enabled(), equalTo(TimestampFieldMapper.Defaults.ENABLED.enabled)); assertThat(docMapper.timestampFieldMapper().enabled(), equalTo(TimestampFieldMapper.Defaults.ENABLED.enabled));
assertThat(docMapper.timestampFieldMapper().fieldType().stored(), equalTo(version.onOrAfter(Version.V_2_0_0_beta1))); assertThat(docMapper.timestampFieldMapper().fieldType().stored(), equalTo(version.onOrAfter(Version.V_2_0_0_beta1)));

View File

@ -44,7 +44,7 @@ import static org.hamcrest.Matchers.notNullValue;
public class TTLMappingTests extends ESSingleNodeTestCase { public class TTLMappingTests extends ESSingleNodeTestCase {
public void testSimpleDisabled() throws Exception { public void testSimpleDisabled() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string(); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
BytesReference source = XContentFactory.jsonBuilder() BytesReference source = XContentFactory.jsonBuilder()
.startObject() .startObject()
@ -74,7 +74,7 @@ public class TTLMappingTests extends ESSingleNodeTestCase {
} }
public void testDefaultValues() throws Exception { public void testDefaultValues() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().string(); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping)); DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
assertThat(docMapper.TTLFieldMapper().enabled(), equalTo(TTLFieldMapper.Defaults.ENABLED_STATE.enabled)); assertThat(docMapper.TTLFieldMapper().enabled(), equalTo(TTLFieldMapper.Defaults.ENABLED_STATE.enabled));
assertThat(docMapper.TTLFieldMapper().fieldType().stored(), equalTo(TTLFieldMapper.Defaults.TTL_FIELD_TYPE.stored())); assertThat(docMapper.TTLFieldMapper().fieldType().stored(), equalTo(TTLFieldMapper.Defaults.TTL_FIELD_TYPE.stored()));

View File

@ -125,7 +125,7 @@ public class UpdateMappingOnClusterIT extends ESIntegTestCase {
} }
public void testDocValuesInvalidMappingOnUpdate() throws Exception { public void testDocValuesInvalidMappingOnUpdate() throws Exception {
String mapping = jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("text").field("type", "text").endObject().endObject().endObject().string(); String mapping = jsonBuilder().startObject().startObject(TYPE).startObject("properties").startObject("text").field("type", "text").endObject().endObject().endObject().endObject().string();
prepareCreate(INDEX).addMapping(TYPE, mapping).get(); prepareCreate(INDEX).addMapping(TYPE, mapping).get();
String mappingUpdate = jsonBuilder().startObject().startObject(TYPE).startObject("_all").startObject("fielddata").field("format", "doc_values").endObject().endObject().endObject().endObject().string(); String mappingUpdate = jsonBuilder().startObject().startObject(TYPE).startObject("_all").startObject("fielddata").field("format", "doc_values").endObject().endObject().endObject().endObject().string();
GetMappingsResponse mappingsBeforeUpdateResponse = client().admin().indices().prepareGetMappings(INDEX).addTypes(TYPE).get(); GetMappingsResponse mappingsBeforeUpdateResponse = client().admin().indices().prepareGetMappings(INDEX).addTypes(TYPE).get();

View File

@ -121,6 +121,7 @@ public class MoreLikeThisQueryBuilderTests extends AbstractQueryTestCase<MoreLik
for (String field : randomFields) { for (String field : randomFields) {
doc.field(field, randomAsciiOfLength(10)); doc.field(field, randomAsciiOfLength(10));
} }
doc.endObject();
} catch (IOException e) { } catch (IOException e) {
throw new ElasticsearchException("Unable to generate random artificial doc!"); throw new ElasticsearchException("Unable to generate random artificial doc!");
} }

View File

@ -240,6 +240,7 @@ public class SimilarityTests extends ESSingleNodeTestCase {
.field("type", "text") .field("type", "text")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject().string(); .endObject().string();
Settings settings = Settings.builder() Settings settings = Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_2_0)) .put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_2_0))

View File

@ -225,25 +225,26 @@ public class AggregatorParsingTests extends ESTestCase {
public void testTwoAggs() throws Exception { public void testTwoAggs() throws Exception {
String source = JsonXContent.contentBuilder() String source = JsonXContent.contentBuilder()
.startObject() .startObject()
.startObject("by_date") .startObject("by_date")
.startObject("date_histogram") .startObject("date_histogram")
.field("field", "timestamp") .field("field", "timestamp")
.field("interval", "month") .field("interval", "month")
.endObject() .endObject()
.startObject("aggs") .startObject("aggs")
.startObject("tag_count") .startObject("tag_count")
.startObject("cardinality") .startObject("cardinality")
.field("field", "tag") .field("field", "tag")
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.startObject("aggs") // 2nd "aggs": illegal .startObject("aggs") // 2nd "aggs": illegal
.startObject("tag_count2") .startObject("tag_count2")
.startObject("cardinality") .startObject("cardinality")
.field("field", "tag") .field("field", "tag")
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject().string(); .endObject().string();
try { try {
XContentParser parser = XContentFactory.xContent(source).createParser(source); XContentParser parser = XContentFactory.xContent(source).createParser(source);
@ -274,14 +275,15 @@ public class AggregatorParsingTests extends ESTestCase {
String source = JsonXContent.contentBuilder() String source = JsonXContent.contentBuilder()
.startObject() .startObject()
.startObject(name) .startObject(name)
.startObject("filter") .startObject("filter")
.startObject("range") .startObject("range")
.startObject("stock") .startObject("stock")
.field("gt", 0) .field("gt", 0)
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject().string(); .endObject().string();
try { try {
XContentParser parser = XContentFactory.xContent(source).createParser(source); XContentParser parser = XContentFactory.xContent(source).createParser(source);
@ -323,19 +325,20 @@ public class AggregatorParsingTests extends ESTestCase {
public void testMissingName() throws Exception { public void testMissingName() throws Exception {
String source = JsonXContent.contentBuilder() String source = JsonXContent.contentBuilder()
.startObject() .startObject()
.startObject("by_date") .startObject("by_date")
.startObject("date_histogram") .startObject("date_histogram")
.field("field", "timestamp") .field("field", "timestamp")
.field("interval", "month") .field("interval", "month")
.endObject() .endObject()
.startObject("aggs") .startObject("aggs")
// the aggregation name is missing // the aggregation name is missing
//.startObject("tag_count") //.startObject("tag_count")
.startObject("cardinality") .startObject("cardinality")
.field("field", "tag") .field("field", "tag")
.endObject() .endObject()
//.endObject() //.endObject()
.endObject() .endObject()
.endObject()
.endObject().string(); .endObject().string();
try { try {
XContentParser parser = XContentFactory.xContent(source).createParser(source); XContentParser parser = XContentFactory.xContent(source).createParser(source);
@ -351,19 +354,20 @@ public class AggregatorParsingTests extends ESTestCase {
public void testMissingType() throws Exception { public void testMissingType() throws Exception {
String source = JsonXContent.contentBuilder() String source = JsonXContent.contentBuilder()
.startObject() .startObject()
.startObject("by_date") .startObject("by_date")
.startObject("date_histogram") .startObject("date_histogram")
.field("field", "timestamp") .field("field", "timestamp")
.field("interval", "month") .field("interval", "month")
.endObject() .endObject()
.startObject("aggs") .startObject("aggs")
.startObject("tag_count") .startObject("tag_count")
// the aggregation type is missing // the aggregation type is missing
//.startObject("cardinality") //.startObject("cardinality")
.field("field", "tag") .field("field", "tag")
//.endObject() //.endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject().string(); .endObject().string();
try { try {
XContentParser parser = XContentFactory.xContent(source).createParser(source); XContentParser parser = XContentFactory.xContent(source).createParser(source);

View File

@ -40,6 +40,7 @@ public class ParentIdAggIT extends ESIntegTestCase {
.startObject("_parent") .startObject("_parent")
.field("type", "parenttype") .field("type", "parenttype")
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
assertAcked(prepareCreate("testidx").addMapping("childtype", mapping)); assertAcked(prepareCreate("testidx").addMapping("childtype", mapping));
client().prepareIndex("testidx", "childtype").setSource(jsonBuilder().startObject().field("num", 1).endObject()).setParent("p1").get(); client().prepareIndex("testidx", "childtype").setSource(jsonBuilder().startObject().field("num", 1).endObject()).setParent("p1").get();

View File

@ -199,6 +199,7 @@ public class HighlighterSearchIT extends ESIntegTestCase {
.field("type", "text") .field("type", "text")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject()) .endObject())
.setSettings(Settings.builder() .setSettings(Settings.builder()
.put(indexSettings()) .put(indexSettings())
@ -828,7 +829,7 @@ public class HighlighterSearchIT extends ESIntegTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject())); .endObject().endObject().endObject()));
ensureGreen(); ensureGreen();
index("test", "type1", "1", index("test", "type1", "1",

View File

@ -179,7 +179,7 @@ public class MoreLikeThisIT extends ESIntegTestCase {
.endObject().endObject().string(); .endObject().endObject().string();
client().admin().indices().prepareCreate("foo").addMapping("bar", mapping).execute().actionGet(); client().admin().indices().prepareCreate("foo").addMapping("bar", mapping).execute().actionGet();
client().prepareIndex("foo", "bar", "1") client().prepareIndex("foo", "bar", "1")
.setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject()) .setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject())
.execute().actionGet(); .execute().actionGet();
client().admin().indices().prepareRefresh("foo").execute().actionGet(); client().admin().indices().prepareRefresh("foo").execute().actionGet();
assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN)); assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
@ -204,7 +204,7 @@ public class MoreLikeThisIT extends ESIntegTestCase {
ensureGreen(); ensureGreen();
client().prepareIndex("foo", "bar", "1") client().prepareIndex("foo", "bar", "1")
.setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject()) .setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject())
.setRouting("2") .setRouting("2")
.execute().actionGet(); .execute().actionGet();
client().admin().indices().prepareRefresh("foo").execute().actionGet(); client().admin().indices().prepareRefresh("foo").execute().actionGet();
@ -227,7 +227,7 @@ public class MoreLikeThisIT extends ESIntegTestCase {
ensureGreen(); ensureGreen();
client().prepareIndex("foo", "bar", "1") client().prepareIndex("foo", "bar", "1")
.setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject()) .setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject())
.setRouting("4000") .setRouting("4000")
.execute().actionGet(); .execute().actionGet();
client().admin().indices().prepareRefresh("foo").execute().actionGet(); client().admin().indices().prepareRefresh("foo").execute().actionGet();
@ -522,17 +522,6 @@ public class MoreLikeThisIT extends ESIntegTestCase {
assertSearchResponse(response); assertSearchResponse(response);
assertHitCount(response, 0); assertHitCount(response, 0);
logger.info("Checking when document is malformed ...");
XContentBuilder malformedDoc = jsonBuilder().startObject();
mltQuery = moreLikeThisQuery(null, new Item[] {new Item("test", "type1", malformedDoc)})
.minTermFreq(0)
.minDocFreq(0)
.minimumShouldMatch("0%");
response = client().prepareSearch("test").setTypes("type1")
.setQuery(mltQuery).get();
assertSearchResponse(response);
assertHitCount(response, 0);
logger.info("Checking the document matches otherwise ..."); logger.info("Checking the document matches otherwise ...");
XContentBuilder normalDoc = jsonBuilder() XContentBuilder normalDoc = jsonBuilder()
.startObject() .startObject()

View File

@ -1711,7 +1711,8 @@ public class SearchQueryIT extends ESIntegTestCase {
public void testAllDisabledButQueried() throws Exception { public void testAllDisabledButQueried() throws Exception {
createIndex("myindex"); createIndex("myindex");
assertAcked(client().admin().indices().preparePutMapping("myindex").setType("mytype").setSource( assertAcked(client().admin().indices().preparePutMapping("myindex").setType("mytype").setSource(
jsonBuilder().startObject().startObject("mytype").startObject("_all").field("enabled", false))); jsonBuilder().startObject().startObject("mytype").startObject("_all").field("enabled", false)
.endObject().endObject().endObject()));
client().prepareIndex("myindex", "mytype").setId("1").setSource("bar", "foo").setRefresh(true).get(); client().prepareIndex("myindex", "mytype").setId("1").setSource("bar", "foo").setRefresh(true).get();
SearchResponse response = client().prepareSearch("myindex").setQuery(matchQuery("_all", "foo")).get(); SearchResponse response = client().prepareSearch("myindex").setQuery(matchQuery("_all", "foo")).get();
assertNoFailures(response); assertNoFailures(response);

View File

@ -193,6 +193,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
.field("type", "geo") .field("type", "geo")
.field("precision", precision) .field("precision", precision)
.endObject() .endObject()
.endObject()
.endObject().endObject() .endObject().endObject()
.endObject().endObject(); .endObject().endObject();
@ -212,6 +213,7 @@ public class ContextSuggestSearch2xIT extends ESIntegTestCase {
.field("type", "geo") .field("type", "geo")
.field("precision", precision) .field("precision", precision)
.endObject() .endObject()
.endObject()
.endObject().endObject() .endObject().endObject()
.endObject().endObject(); .endObject().endObject();
assertAcked(client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(mapping.string()).get()); assertAcked(client().admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(mapping.string()).get());

View File

@ -142,21 +142,22 @@ public class GeoContextMappingTests extends ESSingleNodeTestCase {
MappedFieldType completionFieldType = fieldMapper.fieldType(); MappedFieldType completionFieldType = fieldMapper.fieldType();
ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder() ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder()
.startObject() .startObject()
.startObject("completion") .startObject("completion")
.field("input", "suggestion5", "suggestion6", "suggestion7") .field("input", "suggestion5", "suggestion6", "suggestion7")
.startObject("contexts") .startObject("contexts")
.startArray("ctx") .startArray("ctx")
.startObject() .startObject()
.field("lat", 43.6624803) .field("lat", 43.6624803)
.field("lon", -79.3863353) .field("lon", -79.3863353)
.endObject() .endObject()
.startObject() .startObject()
.field("lat", 43.6624718) .field("lat", 43.6624718)
.field("lon", -79.3873227) .field("lon", -79.3873227)
.endObject() .endObject()
.endArray() .endArray()
.endObject() .endObject()
.field("weight", 5) .field("weight", 5)
.endObject()
.endObject() .endObject()
.bytes()); .bytes());
IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name()); IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());

View File

@ -47,6 +47,7 @@ public class SimilarityIT extends ESIntegTestCase {
.startObject("field2") .startObject("field2")
.field("similarity", "classic") .field("similarity", "classic")
.field("type", "text") .field("type", "text")
.endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()) .endObject())

View File

@ -102,7 +102,7 @@ public class SimpleTimestampIT extends ESIntegTestCase {
assertTimestampMappingEnabled(index, type, true); assertTimestampMappingEnabled(index, type, true);
// update some field in the mapping // update some field in the mapping
XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject(); XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject().endObject();
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get(); PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get();
assertAcked(putMappingResponse); assertAcked(putMappingResponse);

View File

@ -214,7 +214,7 @@ public class SimpleTTLIT extends ESIntegTestCase {
assertTTLMappingEnabled(index, type); assertTTLMappingEnabled(index, type);
// update some field in the mapping // update some field in the mapping
XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject(); XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject().endObject();
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get(); PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get();
assertAcked(putMappingResponse); assertAcked(putMappingResponse);

View File

@ -184,25 +184,27 @@ public class EquivalenceTests extends ESIntegTestCase {
final IntHashSet valuesSet = new IntHashSet(); final IntHashSet valuesSet = new IntHashSet();
cluster().wipeIndices("idx"); cluster().wipeIndices("idx");
prepareCreate("idx") prepareCreate("idx")
.addMapping("type", jsonBuilder().startObject() .addMapping("type", jsonBuilder()
.startObject("type") .startObject()
.startObject("properties") .startObject("type")
.startObject("string_values") .startObject("properties")
.field("type", "keyword") .startObject("string_values")
.startObject("fields") .field("type", "keyword")
.startObject("doc_values") .startObject("fields")
.field("type", "keyword") .startObject("doc_values")
.field("index", false) .field("type", "keyword")
.field("index", false)
.endObject()
.endObject()
.endObject()
.startObject("long_values")
.field("type", "long")
.endObject()
.startObject("double_values")
.field("type", "double")
.endObject()
.endObject()
.endObject() .endObject()
.endObject()
.endObject()
.startObject("long_values")
.field("type", "long")
.endObject()
.startObject("double_values")
.field("type", "double")
.endObject()
.endObject()
.endObject()).execute().actionGet(); .endObject()).execute().actionGet();
List<IndexRequestBuilder> indexingRequests = new ArrayList<>(); List<IndexRequestBuilder> indexingRequests = new ArrayList<>();

View File

@ -484,15 +484,32 @@ public class SearchFieldsTests extends ESIntegTestCase {
public void testGetFieldsComplexField() throws Exception { public void testGetFieldsComplexField() throws Exception {
client().admin().indices().prepareCreate("my-index") client().admin().indices().prepareCreate("my-index")
.setSettings(Settings.builder().put("index.refresh_interval", -1)) .setSettings(Settings.builder().put("index.refresh_interval", -1))
.addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties") .addMapping("my-type2", jsonBuilder()
.startObject("field1").field("type", "object").startObject("properties") .startObject()
.startObject("field2").field("type", "object").startObject("properties") .startObject("my-type2")
.startObject("field3").field("type", "object").startObject("properties") .startObject("properties")
.startObject("field4").field("type", "text").field("store", true) .startObject("field1")
.endObject().endObject() .field("type", "object")
.endObject().endObject() .startObject("properties")
.endObject().endObject() .startObject("field2")
.endObject().endObject().endObject()) .field("type", "object")
.startObject("properties")
.startObject("field3")
.field("type", "object")
.startObject("properties")
.startObject("field4")
.field("type", "text")
.field("store", true)
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject())
.get(); .get();
BytesReference source = jsonBuilder().startObject() BytesReference source = jsonBuilder().startObject()

View File

@ -235,7 +235,7 @@ public class MultiPercolatorIT extends ESIntegTestCase {
} }
client().prepareIndex(INDEX_NAME, "type", "1") client().prepareIndex(INDEX_NAME, "type", "1")
.setSource(jsonBuilder().startObject().field("field", "a")) .setSource(jsonBuilder().startObject().field("field", "a").endObject())
.execute().actionGet(); .execute().actionGet();
refresh(); refresh();

View File

@ -1489,6 +1489,7 @@ public class PercolatorIT extends ESIntegTestCase {
XContentBuilder doc = jsonBuilder(); XContentBuilder doc = jsonBuilder();
doc.startObject(); doc.startObject();
doc.field("some_unnested_field", "value"); doc.field("some_unnested_field", "value");
doc.endObject();
PercolateResponse response = preparePercolate(client()).setPercolateDoc(new PercolateSourceBuilder.DocBuilder().setDoc(doc)).setIndices(INDEX_NAME).setDocumentType("company").get(); PercolateResponse response = preparePercolate(client()).setPercolateDoc(new PercolateSourceBuilder.DocBuilder().setDoc(doc)).setIndices(INDEX_NAME).setDocumentType("company").get();
assertNoFailures(response); assertNoFailures(response);
} }

View File

@ -61,32 +61,32 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
); );
client().prepareIndex("test", "queries", "1") client().prepareIndex("test", "queries", "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()) .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.get(); .get();
client().prepareIndex("test", "queries", "2") client().prepareIndex("test", "queries", "2")
.setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "value")).endObject()) .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "value")).endObject())
.get(); .get();
client().prepareIndex("test", "queries", "3") client().prepareIndex("test", "queries", "3")
.setSource(jsonBuilder().startObject().field("query", boolQuery() .setSource(jsonBuilder().startObject().field("query", boolQuery()
.must(matchQuery("field1", "value")) .must(matchQuery("field1", "value"))
.must(matchQuery("field2", "value")) .must(matchQuery("field2", "value"))
).endObject()).get(); ).endObject()).get();
client().admin().indices().prepareRefresh().get(); client().admin().indices().prepareRefresh().get();
BytesReference source = jsonBuilder().startObject().endObject().bytes(); BytesReference source = jsonBuilder().startObject().endObject().bytes();
logger.info("percolating empty doc"); logger.info("percolating empty doc");
SearchResponse response = client().prepareSearch() SearchResponse response = client().prepareSearch()
.setQuery(new PercolateQueryBuilder("query", "type", source)) .setQuery(new PercolateQueryBuilder("query", "type", source))
.get(); .get();
assertHitCount(response, 1); assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
source = jsonBuilder().startObject().field("field1", "value").endObject().bytes(); source = jsonBuilder().startObject().field("field1", "value").endObject().bytes();
logger.info("percolating doc with 1 field"); logger.info("percolating doc with 1 field");
response = client().prepareSearch() response = client().prepareSearch()
.setQuery(new PercolateQueryBuilder("query", "type", source)) .setQuery(new PercolateQueryBuilder("query", "type", source))
.addSort("_uid", SortOrder.ASC) .addSort("_uid", SortOrder.ASC)
.get(); .get();
assertHitCount(response, 2); assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(1).getId(), equalTo("2")); assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
@ -94,9 +94,9 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
source = jsonBuilder().startObject().field("field1", "value").field("field2", "value").endObject().bytes(); source = jsonBuilder().startObject().field("field1", "value").field("field2", "value").endObject().bytes();
logger.info("percolating doc with 2 fields"); logger.info("percolating doc with 2 fields");
response = client().prepareSearch() response = client().prepareSearch()
.setQuery(new PercolateQueryBuilder("query", "type", source)) .setQuery(new PercolateQueryBuilder("query", "type", source))
.addSort("_uid", SortOrder.ASC) .addSort("_uid", SortOrder.ASC)
.get(); .get();
assertHitCount(response, 3); assertHitCount(response, 3);
assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(1).getId(), equalTo("2")); assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
@ -314,6 +314,7 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject().endObject()) .endObject().endObject())
); );
} }
@ -327,12 +328,13 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
.addMapping("doc_type", "field", "type=keyword") .addMapping("doc_type", "field", "type=keyword")
.addMapping("query_type", jsonBuilder().startObject().startObject("query_type").startObject("properties") .addMapping("query_type", jsonBuilder().startObject().startObject("query_type").startObject("properties")
.startObject("object_field") .startObject("object_field")
.field("type", "object") .field("type", "object")
.startObject("properties") .startObject("properties")
.startObject(queryFieldName) .startObject(queryFieldName)
.field("type", "percolator") .field("type", "percolator")
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject() .endObject()
.endObject().endObject()) .endObject().endObject())
); );
@ -344,7 +346,7 @@ public class PercolatorQuerySearchIT extends ESSingleNodeTestCase {
client().prepareIndex("test2", "query_type", "1") client().prepareIndex("test2", "query_type", "1")
.setSource(jsonBuilder().startObject().startObject("object_field") .setSource(jsonBuilder().startObject().startObject("object_field")
.field(queryFieldName, matchQuery("field", "value")) .field(queryFieldName, matchQuery("field", "value"))
.endObject().endObject()) .endObject().endObject())
.get(); .get();
client().admin().indices().prepareRefresh().get(); client().admin().indices().prepareRefresh().get();

View File

@ -113,6 +113,7 @@ public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase {
.endObject() .endObject()
.endObject() .endObject()
.endObject() .endObject()
.endObject()
.endObject(); .endObject();
byte[] mapping = mappingBuilder.bytes().toBytes(); byte[] mapping = mappingBuilder.bytes().toBytes();

View File

@ -56,7 +56,7 @@ public class SizeMappingIT extends ESIntegTestCase {
assertSizeMappingEnabled(index, type, true); assertSizeMappingEnabled(index, type, true);
// update some field in the mapping // update some field in the mapping
XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject(); XContentBuilder updateMappingBuilder = jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text").endObject().endObject().endObject();
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get(); PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setType(type).setSource(updateMappingBuilder).get();
assertAcked(putMappingResponse); assertAcked(putMappingResponse);