Search in sources :

Example 56 with BytesArray

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

the class BinaryFieldMapperTests method testStoredValue.

public void testStoredValue() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "binary").field("store", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    // case 1: a simple binary value
    final byte[] binaryValue1 = new byte[100];
    binaryValue1[56] = 1;
    // case 2: a value that looks compressed: this used to fail in 1.x
    BytesStreamOutput out = new BytesStreamOutput();
    try (StreamOutput compressed = CompressorFactory.COMPRESSOR.streamOutput(out)) {
        new BytesArray(binaryValue1).writeTo(compressed);
    }
    final byte[] binaryValue2 = BytesReference.toBytes(out.bytes());
    assertTrue(CompressorFactory.isCompressed(new BytesArray(binaryValue2)));
    for (byte[] value : Arrays.asList(binaryValue1, binaryValue2)) {
        ParsedDocument doc = mapper.parse("test", "type", "id", XContentFactory.jsonBuilder().startObject().field("field", value).endObject().bytes());
        BytesRef indexedValue = doc.rootDoc().getBinaryValue("field");
        assertEquals(new BytesRef(value), indexedValue);
        FieldMapper fieldMapper = mapper.mappers().smartNameFieldMapper("field");
        Object originalValue = fieldMapper.fieldType().valueForDisplay(indexedValue);
        assertEquals(new BytesArray(value), originalValue);
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 57 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray 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 58 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray 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 59 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray 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 60 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray 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

BytesArray (org.elasticsearch.common.bytes.BytesArray)203 BytesReference (org.elasticsearch.common.bytes.BytesReference)36 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)29 StreamInput (org.elasticsearch.common.io.stream.StreamInput)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)21 HashMap (java.util.HashMap)17 BytesRef (org.apache.lucene.util.BytesRef)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)14 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)13 ArrayList (java.util.ArrayList)12 TopDocs (org.apache.lucene.search.TopDocs)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 Document (org.elasticsearch.index.mapper.ParseContext.Document)12 Index (org.elasticsearch.index.Index)11 Map (java.util.Map)10 IndexableField (org.apache.lucene.index.IndexableField)10 IndexService (org.elasticsearch.index.IndexService)10