Search in sources :

Example 1 with GetStoredScriptResponse

use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project OpenSearch by opensearch-project.

the class SnapshotCustomPluginStateIT method testIncludeGlobalState.

public void testIncludeGlobalState() throws Exception {
    createRepository("test-repo", "fs");
    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("_doc", XContentFactory.jsonBuilder().startObject().startObject("_doc").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 = BytesReference.bytes(jsonBuilder().startObject().field("description", "my_pipeline").startArray("processors").startObject().startObject("test").endObject().endObject().endArray().endObject());
        assertAcked(clusterAdmin().preparePutPipeline("barbaz", pipelineSource, XContentType.JSON).get());
    }
    if (testScript) {
        logger.info("-->  creating test script");
        assertAcked(clusterAdmin().preparePutStoredScript().setId("foobar").setContent(new BytesArray("{\"script\": { \"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"1\"} }"), XContentType.JSON));
    }
    logger.info("--> snapshot without global state");
    CreateSnapshotResponse createSnapshotResponse = clusterAdmin().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(getSnapshot("test-repo", "test-snap-no-global-state").state(), equalTo(SnapshotState.SUCCESS));
    SnapshotsStatusResponse snapshotsStatusResponse = clusterAdmin().prepareSnapshotStatus("test-repo").addSnapshots("test-snap-no-global-state").get();
    assertThat(snapshotsStatusResponse.getSnapshots().size(), equalTo(1));
    SnapshotStatus snapshotStatus = snapshotsStatusResponse.getSnapshots().get(0);
    assertThat(snapshotStatus.includeGlobalState(), equalTo(false));
    logger.info("--> snapshot with global state");
    createSnapshotResponse = clusterAdmin().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(getSnapshot("test-repo", "test-snap-with-global-state").state(), equalTo(SnapshotState.SUCCESS));
    snapshotsStatusResponse = clusterAdmin().prepareSnapshotStatus("test-repo").addSnapshots("test-snap-with-global-state").get();
    assertThat(snapshotsStatusResponse.getSnapshots().size(), equalTo(1));
    snapshotStatus = snapshotsStatusResponse.getSnapshots().get(0);
    assertThat(snapshotStatus.includeGlobalState(), equalTo(true));
    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(clusterAdmin().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        logger.info("-->  delete test script");
        assertAcked(clusterAdmin().prepareDeleteStoredScript("foobar").get());
    }
    logger.info("--> try restoring cluster state from snapshot without global state");
    RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().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 = clusterAdmin().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 = clusterAdmin().prepareGetPipeline("barbaz").get();
        assertTrue(getPipelineResponse.isFound());
    }
    if (testScript) {
        logger.info("--> check that script is restored");
        GetStoredScriptResponse getStoredScriptResponse = clusterAdmin().prepareGetStoredScript("foobar").get();
        assertNotNull(getStoredScriptResponse.getSource());
    }
    createIndexWithRandomDocs("test-idx", 100);
    logger.info("--> snapshot without global state but with indices");
    createSnapshotResponse = clusterAdmin().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(getSnapshot("test-repo", "test-snap-no-global-state-with-index").state(), equalTo(SnapshotState.SUCCESS));
    logger.info("-->  delete global state and index ");
    cluster().wipeIndices("test-idx");
    if (testTemplate) {
        cluster().wipeTemplates("test-template");
    }
    if (testPipeline) {
        assertAcked(clusterAdmin().deletePipeline(new DeletePipelineRequest("barbaz")).get());
    }
    if (testScript) {
        assertAcked(clusterAdmin().prepareDeleteStoredScript("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 = clusterAdmin().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(clusterAdmin().prepareGetPipeline("barbaz").get().isFound());
    assertNull(clusterAdmin().prepareGetStoredScript("foobar").get().getSource());
    assertDocCount("test-idx", 100L);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) BytesArray(org.opensearch.common.bytes.BytesArray) SnapshotStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) GetPipelineResponse(org.opensearch.action.ingest.GetPipelineResponse) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 2 with GetStoredScriptResponse

use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project OpenSearch by opensearch-project.

the class StoredScriptsDocumentationIT method testGetStoredScript.

@SuppressWarnings("unused")
public void testGetStoredScript() throws Exception {
    RestHighLevelClient client = highLevelClient();
    final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
    putStoredScript("calculate-score", scriptSource);
    {
        // tag::get-stored-script-request
        // <1>
        GetStoredScriptRequest request = new GetStoredScriptRequest("calculate-score");
        // end::get-stored-script-request
        // tag::get-stored-script-request-masterTimeout
        // <1>
        request.masterNodeTimeout(TimeValue.timeValueSeconds(50));
        // <2>
        request.masterNodeTimeout("50s");
        // end::get-stored-script-request-masterTimeout
        // tag::get-stored-script-execute
        GetStoredScriptResponse getResponse = client.getScript(request, RequestOptions.DEFAULT);
        // end::get-stored-script-execute
        // tag::get-stored-script-response
        // <1>
        StoredScriptSource storedScriptSource = getResponse.getSource();
        // <2>
        String lang = storedScriptSource.getLang();
        // <3>
        String source = storedScriptSource.getSource();
        // <4>
        Map<String, String> options = storedScriptSource.getOptions();
        // end::get-stored-script-response
        assertThat(storedScriptSource, equalTo(scriptSource));
        // tag::get-stored-script-execute-listener
        ActionListener<GetStoredScriptResponse> listener = new ActionListener<GetStoredScriptResponse>() {

            @Override
            public void onResponse(GetStoredScriptResponse response) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-stored-script-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::get-stored-script-execute-async
        // <1>
        client.getScriptAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-stored-script-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) IOException(java.io.IOException) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 3 with GetStoredScriptResponse

use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project OpenSearch by opensearch-project.

the class StoredScriptsIT method testGetStoredScript.

public void testGetStoredScript() throws Exception {
    final StoredScriptSource scriptSource = new StoredScriptSource("painless", "Math.log(_score * 2) + params.my_modifier", Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
    PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
    assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
    GetStoredScriptRequest getRequest = new GetStoredScriptRequest("calculate-score");
    getRequest.masterNodeTimeout("50s");
    GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript, highLevelClient()::getScriptAsync);
    assertThat(getResponse.getSource(), equalTo(scriptSource));
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) StoredScriptSource(org.opensearch.script.StoredScriptSource) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 4 with GetStoredScriptResponse

use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project OpenSearch by opensearch-project.

the class SearchTemplateIT method testIndexedTemplateOverwrite.

// Relates to #10397
public void testIndexedTemplateOverwrite() throws Exception {
    createIndex("testindex");
    ensureGreen("testindex");
    client().prepareIndex("testindex").setId("1").setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()).get();
    client().admin().indices().prepareRefresh().get();
    int iterations = randomIntBetween(2, 11);
    String query = "{" + "  \"script\": {" + "    \"lang\": \"mustache\"," + "    \"source\": {" + "      \"query\": {" + "        \"match_phrase_prefix\": {" + "            \"searchtext\": {" + "                \"query\": \"{{P_Keyword1}}\"," + "                \"slop\": {{slop}}" + "            }" + "        }" + "      }" + "    }" + "  }" + "}";
    for (int i = 1; i < iterations; i++) {
        assertAcked(client().admin().cluster().preparePutStoredScript().setId("git01").setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(-1))), XContentType.JSON));
        GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("git01").get();
        assertNotNull(getResponse.getSource());
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("P_Keyword1", "dev");
        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
        assertThat(e.getMessage(), containsString("No negative slop allowed"));
        assertAcked(client().admin().cluster().preparePutStoredScript().setId("git01").setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(0))), XContentType.JSON));
        SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
        assertHitCount(searchResponse.getResponse(), 1);
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) BytesArray(org.opensearch.common.bytes.BytesArray) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Example 5 with GetStoredScriptResponse

use of org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project OpenSearch by opensearch-project.

the class SearchTemplateIT method testIndexedTemplateClient.

public void testIndexedTemplateClient() throws Exception {
    assertAcked(client().admin().cluster().preparePutStoredScript().setId("testTemplate").setContent(new BytesArray("{" + "  \"script\": {" + "    \"lang\": \"mustache\"," + "    \"source\": {" + "      \"query\": {" + "        \"match\": {" + "            \"theField\": \"{{fieldParam}}\"" + "        }" + "      }" + "    }" + "  }" + "}"), XContentType.JSON));
    GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get();
    assertNotNull(getResponse.getSource());
    BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
    bulkRequestBuilder.add(client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test").setId("2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test").setId("3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test").setId("4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test").setId("5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
    bulkRequestBuilder.get();
    client().admin().indices().prepareRefresh().get();
    Map<String, Object> templateParams = new HashMap<>();
    templateParams.put("fieldParam", "foo");
    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test")).setScript("testTemplate").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
    assertHitCount(searchResponse.getResponse(), 4);
    assertAcked(client().admin().cluster().prepareDeleteStoredScript("testTemplate"));
    getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get();
    assertNull(getResponse.getSource());
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) BytesArray(org.opensearch.common.bytes.BytesArray) HashMap(java.util.HashMap) BulkRequestBuilder(org.opensearch.action.bulk.BulkRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)

Aggregations

GetStoredScriptResponse (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse)5 BytesArray (org.opensearch.common.bytes.BytesArray)4 HashMap (java.util.HashMap)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 StoredScriptSource (org.opensearch.script.StoredScriptSource)2 IOException (java.io.IOException)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 SnapshotStatus (org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus)1 SnapshotsStatusResponse (org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse)1 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)1 GetIndexTemplatesResponse (org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse)1 BulkRequestBuilder (org.opensearch.action.bulk.BulkRequestBuilder)1 DeletePipelineRequest (org.opensearch.action.ingest.DeletePipelineRequest)1