Search in sources :

Example 81 with BytesReference

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"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 82 with BytesReference

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]"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 83 with BytesReference

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));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 84 with BytesReference

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"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 85 with BytesReference

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"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18