|
@@ -17,21 +17,21 @@ package org.elasticsearch.client.documentation;/*
|
|
|
* under the License.
|
|
|
*/
|
|
|
|
|
|
-import org.apache.http.util.EntityUtils;
|
|
|
import org.elasticsearch.action.ActionListener;
|
|
|
import org.elasticsearch.action.LatchedActionListener;
|
|
|
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
|
|
|
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
|
|
|
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
|
|
|
+import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
|
|
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
|
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
|
|
|
-import org.elasticsearch.client.Request;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
-import org.elasticsearch.client.Response;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
-import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.bytes.BytesArray;
|
|
|
+import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.unit.TimeValue;
|
|
|
-import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.script.Script;
|
|
|
import org.elasticsearch.script.StoredScriptSource;
|
|
@@ -42,7 +42,8 @@ import java.util.Map;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
|
|
+import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
|
|
|
+import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
|
/**
|
|
@@ -187,14 +188,124 @@ public class StoredScriptsDocumentationIT extends ESRestHighLevelClientTestCase
|
|
|
assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
}
|
|
|
|
|
|
+ public void testPutScript() throws Exception {
|
|
|
+ RestHighLevelClient client = highLevelClient();
|
|
|
+
|
|
|
+ {
|
|
|
+ // tag::put-stored-script-request
|
|
|
+ PutStoredScriptRequest request = new PutStoredScriptRequest();
|
|
|
+ request.id("id"); // <1>
|
|
|
+ request.content(new BytesArray(
|
|
|
+ "{\n" +
|
|
|
+ "\"script\": {\n" +
|
|
|
+ "\"lang\": \"painless\",\n" +
|
|
|
+ "\"source\": \"Math.log(_score * 2) + params.multiplier\"" +
|
|
|
+ "}\n" +
|
|
|
+ "}\n"
|
|
|
+ ), XContentType.JSON); // <2>
|
|
|
+ // end::put-stored-script-request
|
|
|
+
|
|
|
+ // tag::put-stored-script-context
|
|
|
+ request.context("context"); // <1>
|
|
|
+ // end::put-stored-script-context
|
|
|
+
|
|
|
+ // tag::put-stored-script-timeout
|
|
|
+ request.timeout(TimeValue.timeValueMinutes(2)); // <1>
|
|
|
+ request.timeout("2m"); // <2>
|
|
|
+ // end::put-stored-script-timeout
|
|
|
+
|
|
|
+ // tag::put-stored-script-masterTimeout
|
|
|
+ request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1>
|
|
|
+ request.masterNodeTimeout("1m"); // <2>
|
|
|
+ // end::put-stored-script-masterTimeout
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ PutStoredScriptRequest request = new PutStoredScriptRequest();
|
|
|
+ request.id("id");
|
|
|
+
|
|
|
+ // tag::put-stored-script-content-painless
|
|
|
+ XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
+ builder.startObject();
|
|
|
+ {
|
|
|
+ builder.startObject("script");
|
|
|
+ {
|
|
|
+ builder.field("lang", "painless");
|
|
|
+ builder.field("source", "Math.log(_score * 2) + params.multiplier");
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ request.content(BytesReference.bytes(builder), XContentType.JSON); // <1>
|
|
|
+ // end::put-stored-script-content-painless
|
|
|
+
|
|
|
+
|
|
|
+ // tag::put-stored-script-execute
|
|
|
+ AcknowledgedResponse putStoredScriptResponse = client.putScript(request, RequestOptions.DEFAULT);
|
|
|
+ // end::put-stored-script-execute
|
|
|
+
|
|
|
+ // tag::put-stored-script-response
|
|
|
+ boolean acknowledged = putStoredScriptResponse.isAcknowledged(); // <1>
|
|
|
+ // end::put-stored-script-response
|
|
|
+
|
|
|
+ assertTrue(acknowledged);
|
|
|
+
|
|
|
+ // tag::put-stored-script-execute-listener
|
|
|
+ ActionListener<AcknowledgedResponse> listener =
|
|
|
+ new ActionListener<AcknowledgedResponse>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(AcknowledgedResponse response) {
|
|
|
+ // <1>
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ // <2>
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // end::put-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::put-stored-script-execute-async
|
|
|
+ client.putScriptAsync(request, RequestOptions.DEFAULT, listener); // <1>
|
|
|
+ // end::put-stored-script-execute-async
|
|
|
+
|
|
|
+ assertTrue(latch.await(30L, TimeUnit.SECONDS));
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ PutStoredScriptRequest request = new PutStoredScriptRequest();
|
|
|
+ request.id("id");
|
|
|
+
|
|
|
+ // tag::put-stored-script-content-mustache
|
|
|
+ XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
+ builder.startObject();
|
|
|
+ {
|
|
|
+ builder.startObject("script");
|
|
|
+ {
|
|
|
+ builder.field("lang", "mustache");
|
|
|
+ builder.field("source", "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}");
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ }
|
|
|
+ builder.endObject();
|
|
|
+ request.content(BytesReference.bytes(builder), XContentType.JSON); // <1>
|
|
|
+ // end::put-stored-script-content-mustache
|
|
|
+
|
|
|
+ client.putScript(request, RequestOptions.DEFAULT);
|
|
|
+
|
|
|
+ Map<String, Object> script = getAsMap("/_scripts/id");
|
|
|
+ assertThat(extractValue("script.lang", script), equalTo("mustache"));
|
|
|
+ assertThat(extractValue("script.source", script), equalTo("{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void putStoredScript(String id, StoredScriptSource scriptSource) throws IOException {
|
|
|
- final String script = Strings.toString(scriptSource.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS));
|
|
|
- // TODO: change to HighLevel PutStoredScriptRequest when it will be ready
|
|
|
- // so far - using low-level REST API
|
|
|
- Request request = new Request("PUT", "/_scripts/" + id);
|
|
|
- request.setJsonEntity("{\"script\":" + script + "}");
|
|
|
- Response putResponse = adminClient().performRequest(request);
|
|
|
- assertEquals(putResponse.getStatusLine().getReasonPhrase(), 200, putResponse.getStatusLine().getStatusCode());
|
|
|
- assertEquals("{\"acknowledged\":true}", EntityUtils.toString(putResponse.getEntity()));
|
|
|
+ PutStoredScriptRequest request =
|
|
|
+ new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
|
|
|
+ assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
|
|
|
}
|
|
|
}
|