@@ -48,34 +48,7 @@ public void cleanup() {
48
48
49
49
public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows () throws Exception {
50
50
initialize ("regression_single_numeric_feature_and_mixed_data_set" );
51
-
52
- { // Index 350 rows, 300 of them being training rows.
53
- client ().admin ().indices ().prepareCreate (sourceIndex )
54
- .addMapping ("_doc" , NUMERICAL_FEATURE_FIELD , "type=double" , DEPENDENT_VARIABLE_FIELD , "type=double" )
55
- .get ();
56
-
57
- BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ()
58
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
59
- for (int i = 0 ; i < 300 ; i ++) {
60
- Double field = NUMERICAL_FEATURE_VALUES .get (i % 3 );
61
- Double value = DEPENDENT_VARIABLE_VALUES .get (i % 3 );
62
-
63
- IndexRequest indexRequest = new IndexRequest (sourceIndex )
64
- .source (NUMERICAL_FEATURE_FIELD , field , DEPENDENT_VARIABLE_FIELD , value );
65
- bulkRequestBuilder .add (indexRequest );
66
- }
67
- for (int i = 300 ; i < 350 ; i ++) {
68
- Double field = NUMERICAL_FEATURE_VALUES .get (i % 3 );
69
-
70
- IndexRequest indexRequest = new IndexRequest (sourceIndex )
71
- .source (NUMERICAL_FEATURE_FIELD , field );
72
- bulkRequestBuilder .add (indexRequest );
73
- }
74
- BulkResponse bulkResponse = bulkRequestBuilder .get ();
75
- if (bulkResponse .hasFailures ()) {
76
- fail ("Failed to index data: " + bulkResponse .buildFailureMessage ());
77
- }
78
- }
51
+ indexData (sourceIndex , 300 , 50 );
79
52
80
53
DataFrameAnalyticsConfig config = buildAnalytics (jobId , sourceIndex , destIndex , null , new Regression (DEPENDENT_VARIABLE_FIELD ));
81
54
registerAnalytics (config );
@@ -119,23 +92,7 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws
119
92
120
93
public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred () throws Exception {
121
94
initialize ("regression_only_training_data_and_training_percent_is_100" );
122
-
123
- { // Index 350 rows, all of them being training rows.
124
- BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ()
125
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
126
- for (int i = 0 ; i < 350 ; i ++) {
127
- Double field = NUMERICAL_FEATURE_VALUES .get (i % 3 );
128
- Double value = DEPENDENT_VARIABLE_VALUES .get (i % 3 );
129
-
130
- IndexRequest indexRequest = new IndexRequest (sourceIndex )
131
- .source (NUMERICAL_FEATURE_FIELD , field , DEPENDENT_VARIABLE_FIELD , value );
132
- bulkRequestBuilder .add (indexRequest );
133
- }
134
- BulkResponse bulkResponse = bulkRequestBuilder .get ();
135
- if (bulkResponse .hasFailures ()) {
136
- fail ("Failed to index data: " + bulkResponse .buildFailureMessage ());
137
- }
138
- }
95
+ indexData (sourceIndex , 350 , 0 );
139
96
140
97
DataFrameAnalyticsConfig config = buildAnalytics (jobId , sourceIndex , destIndex , null , new Regression (DEPENDENT_VARIABLE_FIELD ));
141
98
registerAnalytics (config );
@@ -171,23 +128,7 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Excepti
171
128
172
129
public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty () throws Exception {
173
130
initialize ("regression_only_training_data_and_training_percent_is_50" );
174
-
175
- { // Index 350 rows, all of them being training rows.
176
- BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ()
177
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
178
- for (int i = 0 ; i < 350 ; i ++) {
179
- Double field = NUMERICAL_FEATURE_VALUES .get (i % 3 );
180
- Double value = DEPENDENT_VARIABLE_VALUES .get (i % 3 );
181
-
182
- IndexRequest indexRequest = new IndexRequest (sourceIndex )
183
- .source (NUMERICAL_FEATURE_FIELD , field , DEPENDENT_VARIABLE_FIELD , value );
184
- bulkRequestBuilder .add (indexRequest );
185
- }
186
- BulkResponse bulkResponse = bulkRequestBuilder .get ();
187
- if (bulkResponse .hasFailures ()) {
188
- fail ("Failed to index data: " + bulkResponse .buildFailureMessage ());
189
- }
190
- }
131
+ indexData (sourceIndex , 350 , 0 );
191
132
192
133
DataFrameAnalyticsConfig config =
193
134
buildAnalytics (
@@ -239,21 +180,7 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception
239
180
@ AwaitsFix (bugUrl ="https://github.com/elastic/elasticsearch/issues/47612" )
240
181
public void testStopAndRestart () throws Exception {
241
182
initialize ("regression_stop_and_restart" );
242
-
243
- BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ()
244
- .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
245
- for (int i = 0 ; i < 350 ; i ++) {
246
- Double field = NUMERICAL_FEATURE_VALUES .get (i % 3 );
247
- Double value = DEPENDENT_VARIABLE_VALUES .get (i % 3 );
248
-
249
- IndexRequest indexRequest = new IndexRequest (sourceIndex )
250
- .source ("feature" , field , "variable" , value );
251
- bulkRequestBuilder .add (indexRequest );
252
- }
253
- BulkResponse bulkResponse = bulkRequestBuilder .get ();
254
- if (bulkResponse .hasFailures ()) {
255
- fail ("Failed to index data: " + bulkResponse .buildFailureMessage ());
256
- }
183
+ indexData (sourceIndex , 350 , 0 );
257
184
258
185
DataFrameAnalyticsConfig config = buildAnalytics (jobId , sourceIndex , destIndex , null , new Regression (DEPENDENT_VARIABLE_FIELD ));
259
186
registerAnalytics (config );
@@ -306,6 +233,31 @@ private void initialize(String jobId) {
306
233
this .destIndex = sourceIndex + "_results" ;
307
234
}
308
235
236
+ private static void indexData (String sourceIndex , int numTrainingRows , int numNonTrainingRows ) {
237
+ client ().admin ().indices ().prepareCreate (sourceIndex )
238
+ .addMapping ("_doc" , NUMERICAL_FEATURE_FIELD , "type=double" , DEPENDENT_VARIABLE_FIELD , "type=double" )
239
+ .get ();
240
+
241
+ BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ()
242
+ .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE );
243
+ for (int i = 0 ; i < numTrainingRows ; i ++) {
244
+ List <Object > source = Arrays .asList (
245
+ NUMERICAL_FEATURE_FIELD , NUMERICAL_FEATURE_VALUES .get (i % NUMERICAL_FEATURE_VALUES .size ()),
246
+ DEPENDENT_VARIABLE_FIELD , DEPENDENT_VARIABLE_VALUES .get (i % DEPENDENT_VARIABLE_VALUES .size ()));
247
+ IndexRequest indexRequest = new IndexRequest (sourceIndex ).source (source .toArray ());
248
+ bulkRequestBuilder .add (indexRequest );
249
+ }
250
+ for (int i = numTrainingRows ; i < numTrainingRows + numNonTrainingRows ; i ++) {
251
+ List <Object > source = Arrays .asList (NUMERICAL_FEATURE_FIELD , NUMERICAL_FEATURE_VALUES .get (i % NUMERICAL_FEATURE_VALUES .size ()));
252
+ IndexRequest indexRequest = new IndexRequest (sourceIndex ).source (source .toArray ());
253
+ bulkRequestBuilder .add (indexRequest );
254
+ }
255
+ BulkResponse bulkResponse = bulkRequestBuilder .get ();
256
+ if (bulkResponse .hasFailures ()) {
257
+ fail ("Failed to index data: " + bulkResponse .buildFailureMessage ());
258
+ }
259
+ }
260
+
309
261
private static Map <String , Object > getDestDoc (DataFrameAnalyticsConfig config , SearchHit hit ) {
310
262
GetResponse destDocGetResponse = client ().prepareGet ().setIndex (config .getDest ().getIndex ()).setId (hit .getId ()).get ();
311
263
assertThat (destDocGetResponse .isExists (), is (true ));
0 commit comments