Search in sources :

Example 96 with BytesArray

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

the class BulkProcessorIT method indexDocs.

private static MultiGetRequestBuilder indexDocs(Client client, BulkProcessor processor, int numDocs) throws Exception {
    MultiGetRequestBuilder multiGetRequestBuilder = client.prepareMultiGet();
    for (int i = 1; i <= numDocs; i++) {
        if (randomBoolean()) {
            processor.add(new IndexRequest("test", "test", Integer.toString(i)).source(Requests.INDEX_CONTENT_TYPE, "field", randomRealisticUnicodeOfLengthBetween(1, 30)));
        } else {
            final String source = "{ \"index\":{\"_index\":\"test\",\"_type\":\"test\",\"_id\":\"" + Integer.toString(i) + "\"} }\n" + JsonXContent.contentBuilder().startObject().field("field", randomRealisticUnicodeOfLengthBetween(1, 30)).endObject().string() + "\n";
            processor.add(new BytesArray(source), null, null, XContentType.JSON);
        }
        multiGetRequestBuilder.add("test", "test", Integer.toString(i));
    }
    return multiGetRequestBuilder;
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 97 with BytesArray

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

the class BulkRequestTests method testSimpleBulk1.

public void testSimpleBulk1() throws Exception {
    String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk.json");
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
    assertThat(bulkRequest.numberOfActions(), equalTo(3));
    assertThat(((IndexRequest) bulkRequest.requests().get(0)).source(), equalTo(new BytesArray("{ \"field1\" : \"value1\" }")));
    assertThat(bulkRequest.requests().get(1), instanceOf(DeleteRequest.class));
    assertThat(((IndexRequest) bulkRequest.requests().get(2)).source(), equalTo(new BytesArray("{ \"field1\" : \"value3\" }")));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Matchers.containsString(org.hamcrest.Matchers.containsString) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 98 with BytesArray

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

the class BulkRequestTests method testBulkAllowExplicitIndex.

public void testBulkAllowExplicitIndex() throws Exception {
    String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk.json");
    try {
        new BulkRequest().add(new BytesArray(bulkAction.getBytes(StandardCharsets.UTF_8)), null, null, false, XContentType.JSON);
        fail();
    } catch (Exception e) {
    }
    bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk5.json");
    new BulkRequest().add(new BytesArray(bulkAction.getBytes(StandardCharsets.UTF_8)), "test", null, false, XContentType.JSON);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Matchers.containsString(org.hamcrest.Matchers.containsString) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 99 with BytesArray

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

the class BulkRequestTests method testSimpleBulkWithCarriageReturn.

public void testSimpleBulkWithCarriageReturn() throws Exception {
    String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\"} }\r\n{ \"field1\" : \"value1\" }\r\n";
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
    assertThat(bulkRequest.numberOfActions(), equalTo(1));
    assertThat(((IndexRequest) bulkRequest.requests().get(0)).source(), equalTo(new BytesArray("{ \"field1\" : \"value1\" }")));
    Map<String, Object> sourceMap = XContentHelper.convertToMap(((IndexRequest) bulkRequest.requests().get(0)).source(), false, XContentType.JSON).v2();
    assertEquals("value1", sourceMap.get("field1"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 100 with BytesArray

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

the class PutIndexTemplateRequestTests method testPutIndexTemplateRequestSerializationXContentBwc.

public void testPutIndexTemplateRequestSerializationXContentBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("ADwDAANmb28IdGVtcGxhdGUAAAAAAAABA2Jhcg8tLS0KZm9vOiAiYmFyIgoAAAAAAAAAAAAAAAA=");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2, Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        PutIndexTemplateRequest request = new PutIndexTemplateRequest();
        request.readFrom(in);
        String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string();
        assertNotEquals(mapping, request.mappings().get("bar"));
        assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar"));
        assertEquals("foo", request.name());
        assertEquals("template", request.patterns().get(0));
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput)

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