Search in sources :

Example 31 with BytesArray

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

the class PutIndexTemplateRequestTests method testPutIndexTemplateRequestSerializationXContent.

public void testPutIndexTemplateRequestSerializationXContent() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest("foo");
    String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string();
    request.patterns(Collections.singletonList("foo"));
    request.mapping("bar", mapping, XContentType.YAML);
    assertNotEquals(mapping, request.mappings().get("bar"));
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar"));
    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
    PutIndexTemplateRequest serialized = new PutIndexTemplateRequest();
    serialized.readFrom(in);
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), serialized.mappings().get("bar"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 32 with BytesArray

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

the class BulkWithUpdatesIT method testBulkUpdateDocAsUpsertWithParent.

/*
    Test for https://github.com/elastic/elasticsearch/issues/3444
     */
public void testBulkUpdateDocAsUpsertWithParent() throws Exception {
    client().admin().indices().prepareCreate("test").addMapping("parent", "{\"parent\":{}}", XContentType.JSON).addMapping("child", "{\"child\": {\"_parent\": {\"type\": \"parent\"}}}", XContentType.JSON).execute().actionGet();
    ensureGreen();
    BulkRequestBuilder builder = client().prepareBulk();
    // It's important to use JSON parsing here and request objects: issue 3444 is related to incomplete option parsing
    byte[] addParent = new BytesArray("{" + "  \"index\" : {" + "    \"_index\" : \"test\"," + "    \"_type\"  : \"parent\"," + "    \"_id\"    : \"parent1\"" + "  }" + "}" + "\n" + "{" + "  \"field1\" : \"value1\"" + "}" + "\n").array();
    byte[] addChild = new BytesArray("{" + "  \"update\" : {" + "    \"_index\" : \"test\"," + "    \"_type\"  : \"child\"," + "    \"_id\"    : \"child1\"," + "    \"parent\" : \"parent1\"" + "  }" + "}" + "\n" + "{" + "  \"doc\" : {" + "    \"field1\" : \"value1\"" + "  }," + "  \"doc_as_upsert\" : \"true\"" + "}" + "\n").array();
    builder.add(addParent, 0, addParent.length, XContentType.JSON);
    builder.add(addChild, 0, addChild.length, XContentType.JSON);
    BulkResponse bulkResponse = builder.get();
    assertThat(bulkResponse.getItems().length, equalTo(2));
    assertThat(bulkResponse.getItems()[0].isFailed(), equalTo(false));
    assertThat(bulkResponse.getItems()[1].isFailed(), equalTo(false));
    client().admin().indices().prepareRefresh("test").get();
    //we check that the _parent field was set on the child document by using the has parent query
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.hasParentQuery("parent", QueryBuilders.matchAllQuery(), false)).get();
    assertNoFailures(searchResponse);
    assertSearchHits(searchResponse, "child1");
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 33 with BytesArray

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

the class GetFieldMappingsResponseTests method testSerialization.

public void testSerialization() throws IOException {
    Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>();
    FieldMappingMetaData fieldMappingMetaData = new FieldMappingMetaData("my field", new BytesArray("{}"));
    mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetaData)));
    GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        response.writeTo(out);
        GetFieldMappingsResponse serialized = new GetFieldMappingsResponse();
        try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
            serialized.readFrom(in);
            FieldMappingMetaData metaData = serialized.fieldMappings("index", "type", "field");
            assertNotNull(metaData);
            assertEquals(new BytesArray("{}"), metaData.getSource());
        }
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) FieldMappingMetaData(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse.FieldMappingMetaData) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) HashMap(java.util.HashMap) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 34 with BytesArray

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

the class SharedClusterSnapshotRestoreIT method testIncludeGlobalState.

public void testIncludeGlobalState() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path location = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", location)));
    boolean testTemplate = randomBoolean();
    boolean testPipeline = randomBoolean();
    // At least something should be stored
    boolean testScript = (testTemplate == false && testPipeline == false) || randomBoolean();
    if (testTemplate) {
        logger.info("-->  creating test template");
        assertThat(client.admin().indices().preparePutTemplate("test-template").setPatterns(Collections.singletonList("te*")).addMapping("test-mapping", XContentFactory.jsonBuilder().startObject().startObject("test-mapping").startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject()).get().isAcknowledged(), equalTo(true));
    }
    if (testPipeline) {
        logger.info("-->  creating test pipeline");
        BytesReference pipelineSource = jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject().bytes();
        assertAcked(client().admin().cluster().preparePutPipeline("barbaz", pipelineSource, XContentType.JSON).get());
    }
    if (testScript) {
        logger.info("-->  creating test script");
        assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MockScriptEngine.NAME).setId("foobar").setContent(new BytesArray("{\"script\":\"1\"}"), XContentType.JSON));
    }
    logger.info("--> snapshot without global state");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-no-global-state").setIndices().setIncludeGlobalState(false).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-no-global-state").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> snapshot with global state");
    createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-with-global-state").setIndices().setIncludeGlobalState(true).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(0));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-with-global-state").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    if (testTemplate) {
        logger.info("-->  delete test template");
        cluster().wipeTemplates("test-template");
        GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
        assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    }
    if (testPipeline) {
        logger.info("-->  delete test pipeline");
        assertAcked(client().admin().cluster().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        logger.info("-->  delete test script");
        assertAcked(client().admin().cluster().prepareDeleteStoredScript(MockScriptEngine.NAME, "foobar").get());
    }
    logger.info("--> try restoring cluster state from snapshot without global state");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
    logger.info("--> check that template wasn't restored");
    GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    logger.info("--> restore cluster state");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-with-global-state").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
    if (testTemplate) {
        logger.info("--> check that template is restored");
        getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
        assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
    }
    if (testPipeline) {
        logger.info("--> check that pipeline is restored");
        GetPipelineResponse getPipelineResponse = client().admin().cluster().prepareGetPipeline("barbaz").get();
        assertTrue(getPipelineResponse.isFound());
    }
    if (testScript) {
        logger.info("--> check that script is restored");
        GetStoredScriptResponse getStoredScriptResponse = client().admin().cluster().prepareGetStoredScript(MockScriptEngine.NAME, "foobar").get();
        assertNotNull(getStoredScriptResponse.getSource());
    }
    createIndex("test-idx");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> snapshot without global state but with indices");
    createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-no-global-state-with-index").setIndices("test-idx").setIncludeGlobalState(false).setWaitForCompletion(true).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-no-global-state-with-index").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("-->  delete global state and index ");
    cluster().wipeIndices("test-idx");
    if (testTemplate) {
        cluster().wipeTemplates("test-template");
    }
    if (testPipeline) {
        assertAcked(client().admin().cluster().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        assertAcked(client().admin().cluster().prepareDeleteStoredScript(MockScriptEngine.NAME, "foobar").get());
    }
    getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    logger.info("--> try restoring index and cluster state from snapshot without global state");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-no-global-state-with-index").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
    logger.info("--> check that global state wasn't restored but index was");
    getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    assertFalse(client().admin().cluster().prepareGetPipeline("barbaz").get().isFound());
    assertNull(client().admin().cluster().prepareGetStoredScript(MockScriptEngine.NAME, "foobar").get().getSource());
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
}
Also used : Path(java.nio.file.Path) BytesReference(org.elasticsearch.common.bytes.BytesReference) DeletePipelineRequest(org.elasticsearch.action.ingest.DeletePipelineRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) Client(org.elasticsearch.client.Client) GetPipelineResponse(org.elasticsearch.action.ingest.GetPipelineResponse) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) GetStoredScriptResponse(org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 35 with BytesArray

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

the class BlobStoreFormatIT method randomCorruption.

protected void randomCorruption(BlobContainer blobContainer, String blobName) throws IOException {
    byte[] buffer = new byte[(int) blobContainer.listBlobsByPrefix(blobName).get(blobName).length()];
    long originalChecksum = checksum(buffer);
    try (InputStream inputStream = blobContainer.readBlob(blobName)) {
        Streams.readFully(inputStream, buffer);
    }
    do {
        int location = randomIntBetween(0, buffer.length - 1);
        buffer[location] = (byte) (buffer[location] ^ 42);
    } while (originalChecksum == checksum(buffer));
    // delete original before writing new blob
    blobContainer.deleteBlob(blobName);
    BytesArray bytesArray = new BytesArray(buffer);
    try (StreamInput stream = bytesArray.streamInput()) {
        blobContainer.writeBlob(blobName, stream, bytesArray.length());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) InputStream(java.io.InputStream) 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