|
10 | 10 | import org.apache.http.entity.ContentType;
|
11 | 11 | import org.apache.http.entity.StringEntity;
|
12 | 12 | import org.elasticsearch.client.Request;
|
| 13 | +import org.elasticsearch.client.RequestOptions; |
13 | 14 | import org.elasticsearch.client.Response;
|
14 | 15 | import org.elasticsearch.client.ResponseException;
|
| 16 | +import org.elasticsearch.client.WarningsHandler; |
15 | 17 | import org.elasticsearch.common.Strings;
|
16 | 18 | import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
17 | 19 | import org.elasticsearch.xcontent.XContentBuilder;
|
|
29 | 31 |
|
30 | 32 | import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
|
31 | 33 | import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
|
| 34 | +import static org.hamcrest.Matchers.allOf; |
32 | 35 | import static org.hamcrest.Matchers.containsString;
|
| 36 | +import static org.hamcrest.Matchers.empty; |
33 | 37 | import static org.hamcrest.Matchers.equalTo;
|
34 | 38 | import static org.hamcrest.Matchers.hasEntry;
|
35 | 39 | import static org.hamcrest.Matchers.hasSize;
|
@@ -949,6 +953,59 @@ public void testPreviewTransformWithPipeline() throws Exception {
|
949 | 953 | });
|
950 | 954 | }
|
951 | 955 |
|
| 956 | + @SuppressWarnings("unchecked") |
| 957 | + public void testPreviewTransformWithPipelineScript() throws Exception { |
| 958 | + String pipelineId = "my-preview-pivot-pipeline-script"; |
| 959 | + Request pipelineRequest = new Request("PUT", "/_ingest/pipeline/" + pipelineId); |
| 960 | + pipelineRequest.setJsonEntity( |
| 961 | + "{\n" |
| 962 | + + " \"description\" : \"my pivot preview pipeline\",\n" |
| 963 | + + " \"processors\" : [\n" |
| 964 | + + " {\n" |
| 965 | + + " \"script\" : {\n" |
| 966 | + + " \"lang\": \"painless\",\n" |
| 967 | + + " \"source\": \"ctx._id = ctx['non']['existing'];\"\n" |
| 968 | + + " }\n" |
| 969 | + + " }\n" |
| 970 | + + " ]\n" |
| 971 | + + "}" |
| 972 | + ); |
| 973 | + client().performRequest(pipelineRequest); |
| 974 | + |
| 975 | + setupDataAccessRole(DATA_ACCESS_ROLE, REVIEWS_INDEX_NAME); |
| 976 | + final Request createPreviewRequest = createRequestWithAuth("POST", getTransformEndpoint() + "_preview", null); |
| 977 | + createPreviewRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE)); |
| 978 | + |
| 979 | + String config = "{ \"source\": {\"index\":\"" |
| 980 | + + REVIEWS_INDEX_NAME |
| 981 | + + "\"} ," |
| 982 | + + "\"dest\": {\"pipeline\": \"" |
| 983 | + + pipelineId |
| 984 | + + "\"}," |
| 985 | + + " \"pivot\": {" |
| 986 | + + " \"group_by\": {" |
| 987 | + + " \"user.id\": {\"terms\": { \"field\": \"user_id\" }}," |
| 988 | + + " \"by_day\": {\"date_histogram\": {\"fixed_interval\": \"1d\",\"field\":\"timestamp\"}}}," |
| 989 | + + " \"aggregations\": {" |
| 990 | + + " \"user.avg_rating\": {" |
| 991 | + + " \"avg\": {" |
| 992 | + + " \"field\": \"stars\"" |
| 993 | + + " } } } }" |
| 994 | + + "}"; |
| 995 | + createPreviewRequest.setJsonEntity(config); |
| 996 | + |
| 997 | + Response createPreviewResponse = client().performRequest(createPreviewRequest); |
| 998 | + Map<String, Object> previewTransformResponse = entityAsMap(createPreviewResponse); |
| 999 | + List<Map<String, Object>> preview = (List<Map<String, Object>>) previewTransformResponse.get("preview"); |
| 1000 | + // Pipeline failed for all the docs so the preview is empty |
| 1001 | + assertThat(preview, is(empty())); |
| 1002 | + assertThat(createPreviewResponse.getWarnings(), is(not(empty()))); |
| 1003 | + assertThat( |
| 1004 | + createPreviewResponse.getWarnings().get(createPreviewResponse.getWarnings().size() - 1), |
| 1005 | + allOf(containsString("Pipeline returned 100 errors, first error:"), containsString("type=script_exception")) |
| 1006 | + ); |
| 1007 | + } |
| 1008 | + |
952 | 1009 | public void testPivotWithMaxOnDateField() throws Exception {
|
953 | 1010 | String transformId = "simple_date_histogram_pivot_with_max_time";
|
954 | 1011 | String transformIndex = "pivot_reviews_via_date_histogram_with_max_time";
|
|
0 commit comments