Search in sources :

Example 66 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class DocumentParserTests method testParseToJsonAndParse.

public void testParseToJsonAndParse() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/simple/test-mapping.json");
    DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    DocumentMapper docMapper = parser.parse("person", new CompressedXContent(mapping));
    String builtMapping = docMapper.mappingSource().string();
    // reparse it
    DocumentMapper builtDocMapper = parser.parse("person", new CompressedXContent(builtMapping));
    BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json"));
    Document doc = builtDocMapper.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 67 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class DocumentParserTests method testDynamicStrictDottedFieldNameLong.

public void testDynamicStrictDottedFieldNameLong() throws Exception {
    DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").field("dynamic", "strict").endObject().endObject().string();
    DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
    BytesReference bytes = XContentFactory.jsonBuilder().startObject().field("foo.bar.baz", 0).endObject().bytes();
    StrictDynamicMappingException exception = expectThrows(StrictDynamicMappingException.class, () -> mapper.parse("test", "type", "1", bytes));
    assertEquals("mapping set to strict, dynamic introduction of [foo] within [type] is not allowed", exception.getMessage());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 68 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class GetFieldTests method testToAndFromXContent.

public void testToAndFromXContent() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetField, GetField> tuple = randomGetField(xContentType);
    GetField getField = tuple.v1();
    GetField expectedGetField = tuple.v2();
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(getField, xContentType, humanReadable);
    //test that we can parse what we print out
    GetField parsedGetField;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        //we need to move to the next token, the start object one that we manually added is not expected
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedGetField = GetField.fromXContent(parser);
        assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertEquals(expectedGetField, parsedGetField);
    BytesReference finalBytes = toXContent(parsedGetField, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 69 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class GetResultTests method testToXContentEmbedded.

public void testToXContentEmbedded() throws IOException {
    Map<String, GetField> fields = new HashMap<>();
    fields.put("foo", new GetField("foo", singletonList("bar")));
    fields.put("baz", new GetField("baz", Arrays.asList("baz_0", "baz_1")));
    GetResult getResult = new GetResult("index", "type", "id", 2, true, new BytesArray("{\"foo\":\"bar\",\"baz\":[\"baz_0\",\"baz_1\"]}"), fields);
    BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON, false);
    assertEquals("{\"found\":true,\"_source\":{\"foo\":\"bar\",\"baz\":[\"baz_0\",\"baz_1\"]}," + "\"fields\":{\"foo\":[\"bar\"],\"baz\":[\"baz_0\",\"baz_1\"]}}", originalBytes.utf8ToString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) GetFieldTests.randomGetField(org.elasticsearch.index.get.GetFieldTests.randomGetField) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap)

Example 70 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class GetResultTests method testToAndFromXContentEmbedded.

public void testToAndFromXContentEmbedded() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
    GetResult getResult = tuple.v1();
    // We don't expect to retrieve the index/type/id of the GetResult because they are not rendered
    // by the toXContentEmbedded method.
    GetResult expectedGetResult = new GetResult(null, null, null, -1, tuple.v2().isExists(), tuple.v2().sourceRef(), tuple.v2().getFields());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContentEmbedded(getResult, xContentType, humanReadable);
    // Test that we can parse the result of toXContentEmbedded()
    GetResult parsedEmbeddedGetResult;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsedEmbeddedGetResult = GetResult.fromXContentEmbedded(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(expectedGetResult, parsedEmbeddedGetResult);
    //print the parsed object out and test that the output is the same as the original output
    BytesReference finalBytes = toXContentEmbedded(parsedEmbeddedGetResult, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
    //check that the source stays unchanged, no shuffling of keys nor anything like that
    assertEquals(expectedGetResult.sourceAsString(), parsedEmbeddedGetResult.sourceAsString());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

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