use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse 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));
}
use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project elasticsearch by elastic.
the class RestGetSearchTemplateAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
String id = request.param("id");
GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, Script.DEFAULT_TEMPLATE_LANG);
return channel -> client.admin().cluster().getStoredScript(getRequest, new RestBuilderListener<GetStoredScriptResponse>(channel) {
@Override
public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.field(_ID_PARSE_FIELD.getPreferredName(), id);
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), Script.DEFAULT_TEMPLATE_LANG);
StoredScriptSource source = response.getSource();
boolean found = source != null;
builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
if (found) {
builder.field(StoredScriptSource.TEMPLATE_PARSE_FIELD.getPreferredName(), source.getCode());
}
builder.endObject();
return new BytesRestResponse(found ? RestStatus.OK : RestStatus.NOT_FOUND, builder);
}
});
}
use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project elasticsearch by elastic.
the class SearchTemplateIT method testIndexedTemplateClient.
public void testIndexedTemplateClient() throws Exception {
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("testTemplate").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("testTemplate").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "testTemplate").get();
assertNotNull(getResponse.getSource());
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "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").types("type")).setScript("testTemplate").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
assertHitCount(searchResponse.getResponse(), 4);
assertAcked(client().admin().cluster().prepareDeleteStoredScript(MustacheScriptEngineService.NAME, "testTemplate"));
getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "testTemplate").get();
assertNull(getResponse.getSource());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("/template_index/mustache/1000").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
assertThat(e.getMessage(), containsString("illegal stored script format [/template_index/mustache/1000] use only <id>"));
}
use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project elasticsearch by elastic.
the class SearchTemplateIT method testIndexedTemplateOverwrite.
// Relates to #10397
public void testIndexedTemplateOverwrite() throws Exception {
createIndex("testindex");
ensureGreen("testindex");
client().prepareIndex("testindex", "test", "1").setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()).get();
client().admin().indices().prepareRefresh().get();
int iterations = randomIntBetween(2, 11);
for (int i = 1; i < iterations; i++) {
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"template\":{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"ooophrase_prefix\"}}}}}"), XContentType.JSON));
GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript(MustacheScriptEngineService.NAME, "git01").get();
assertNotNull(getResponse.getSource());
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("P_Keyword1", "dev");
ParsingException e = expectThrows(ParsingException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
assertThat(e.getMessage(), containsString("[match] query does not support type ooophrase_prefix"));
assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("git01").setContent(new BytesArray("{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + "\"type\": \"phrase_prefix\"}}}}"), XContentType.JSON));
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex").types("test")).setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
assertHitCount(searchResponse.getResponse(), 1);
assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]");
}
}
use of org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse in project elasticsearch by elastic.
the class RestGetStoredScriptAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
String id;
String lang;
// name ordering issues in the handlers' paths.
if (request.param("id") == null) {
id = request.param("lang");
;
lang = null;
} else {
id = request.param("id");
lang = request.param("lang");
}
if (lang != null) {
deprecationLogger.deprecated("specifying lang [" + lang + "] as part of the url path is deprecated");
}
GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id, lang);
return channel -> client.admin().cluster().getStoredScript(getRequest, new RestBuilderListener<GetStoredScriptResponse>(channel) {
@Override
public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.field(_ID_PARSE_FIELD.getPreferredName(), id);
if (lang != null) {
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), lang);
}
StoredScriptSource source = response.getSource();
boolean found = source != null;
builder.field(FOUND_PARSE_FIELD.getPreferredName(), found);
if (found) {
if (lang == null) {
builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
if (source.getOptions().isEmpty() == false) {
builder.field(StoredScriptSource.OPTIONS_PARSE_FIELD.getPreferredName(), source.getOptions());
}
builder.endObject();
} else {
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
}
}
builder.endObject();
return new BytesRestResponse(found ? RestStatus.OK : RestStatus.NOT_FOUND, builder);
}
});
}
Aggregations