use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class CopyToMapperTests method testCopyToStrictDynamicInnerObjectParsing.
public void testCopyToStrictDynamicInnerObjectParsing() throws Exception {
String mapping = jsonBuilder().startObject().startObject("type1").field("dynamic", "strict").startObject("properties").startObject("copy_test").field("type", "text").field("copy_to", "very.inner.field").endObject().endObject().endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
BytesReference json = jsonBuilder().startObject().field("copy_test", "foo").endObject().bytes();
try {
docMapper.parse("test", "type1", "1", json).rootDoc();
fail();
} catch (MapperParsingException ex) {
assertThat(ex.getMessage(), startsWith("mapping set to strict, dynamic introduction of [very] within [type1] is not allowed"));
}
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class DocumentParserTests method testDynamicFieldsStartingAndEndingWithDot.
public void testDynamicFieldsStartingAndEndingWithDot() throws Exception {
BytesReference bytes = XContentFactory.jsonBuilder().startObject().startArray("top.").startObject().startArray("foo.").startObject().field("thing", "bah").endObject().endArray().endObject().endArray().endObject().bytes();
client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get();
bytes = XContentFactory.jsonBuilder().startObject().startArray("top.").startObject().startArray("foo.").startObject().startObject("bar.").startObject("aoeu").field("a", 1).field("b", 2).endObject().endObject().endObject().endArray().endObject().endArray().endObject().bytes();
try {
client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get();
fail("should have failed to dynamically introduce a double-dot field");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("object field starting or ending with a [.] makes object resolution ambiguous: [top..foo..bar]"));
}
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class DocumentParserTests method testDynamicDottedFieldNameLongWithExistingParent.
public void testDynamicDottedFieldNameLongWithExistingParent() throws Exception {
DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "object").endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
BytesReference bytes = XContentFactory.jsonBuilder().startObject().field("foo.bar.baz", 0).endObject().bytes();
ParsedDocument doc = mapper.parse("test", "type", "1", bytes);
assertEquals(2, doc.rootDoc().getFields("foo.bar.baz").length);
Mapper fooMapper = doc.dynamicMappingsUpdate().root().getMapper("foo");
assertNotNull(fooMapper);
assertThat(fooMapper, instanceOf(ObjectMapper.class));
Mapper barMapper = ((ObjectMapper) fooMapper).getMapper("bar");
assertNotNull(barMapper);
assertThat(barMapper, instanceOf(ObjectMapper.class));
Mapper bazMapper = ((ObjectMapper) barMapper).getMapper("baz");
assertNotNull(bazMapper);
assertThat(bazMapper, instanceOf(NumberFieldMapper.class));
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class DocumentParserTests method testSimpleParser.
public void testSimpleParser() throws Exception {
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/simple/test-mapping.json");
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
assertThat((String) docMapper.meta().get("param1"), equalTo("value1"));
BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json"));
Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
assertThat(doc.get(docMapper.uidMapper().fieldType().name()), equalTo(Uid.createUid("person", "1")));
assertThat(doc.get(docMapper.mappers().getMapper("name.first").fieldType().name()), equalTo("shay"));
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class DocumentParserTests method testNoDocumentSent.
public void testNoDocumentSent() throws Exception {
IndexService indexService = createIndex("test");
DocumentMapper docMapper = new DocumentMapper.Builder(new RootObjectMapper.Builder("person").add(new ObjectMapper.Builder("name").add(new TextFieldMapper.Builder("first").store(true).index(false))), indexService.mapperService()).build(indexService.mapperService());
BytesReference json = new BytesArray("".getBytes(StandardCharsets.UTF_8));
try {
docMapper.parse("test", "person", "1", json).rootDoc();
fail("this point is never reached");
} catch (MapperParsingException e) {
assertThat(e.getMessage(), equalTo("failed to parse, document is empty"));
}
}
Aggregations