@@ -122,11 +122,20 @@ private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List<
122
122
}
123
123
124
124
private AnomalyDetector createIndexAndGetAnomalyDetector (String indexName , List <Feature > features , boolean useDateNanos )
125
- throws IOException {
125
+ throws IOException {
126
+ return createIndexAndGetAnomalyDetector (indexName , features , useDateNanos , false );
127
+ }
128
+
129
+ private AnomalyDetector createIndexAndGetAnomalyDetector (String indexName , List <Feature > features , boolean useDateNanos ,
130
+ boolean useFlattenResultIndex ) throws IOException {
126
131
TestHelpers .createIndexWithTimeField (client (), indexName , TIME_FIELD , useDateNanos );
127
132
String testIndexData = "{\" keyword-field\" : \" field-1\" , \" ip-field\" : \" 1.2.3.4\" , \" timestamp\" : 1}" ;
128
133
TestHelpers .ingestDataToIndex (client (), indexName , TestHelpers .toHttpEntity (testIndexData ));
129
- AnomalyDetector detector = TestHelpers .randomAnomalyDetector (TIME_FIELD , indexName , features );
134
+
135
+ AnomalyDetector detector = useFlattenResultIndex
136
+ ? TestHelpers .randomAnomalyDetectorWithFlattenResultIndex (TIME_FIELD , indexName , features )
137
+ : TestHelpers .randomAnomalyDetector (TIME_FIELD , indexName , features );
138
+
130
139
return detector ;
131
140
}
132
141
@@ -180,6 +189,45 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
180
189
);
181
190
}
182
191
192
+ public void testCreateAnomalyDetectorWithFlattenedResultIndex () throws Exception {
193
+ AnomalyDetector detector = createIndexAndGetAnomalyDetector (INDEX_NAME ,
194
+ ImmutableList .of (TestHelpers .randomFeature (true )), false , true );
195
+
196
+ // test behavior when AD is disabled
197
+ updateClusterSettings (ADEnabledSetting .AD_ENABLED , false );
198
+ Exception ex = expectThrows (
199
+ ResponseException .class ,
200
+ () -> TestHelpers
201
+ .makeRequest (
202
+ client (),
203
+ "POST" ,
204
+ TestHelpers .AD_BASE_DETECTORS_URI ,
205
+ ImmutableMap .of (),
206
+ TestHelpers .toHttpEntity (detector ),
207
+ null
208
+ )
209
+ );
210
+ assertThat (ex .getMessage (), containsString (ADCommonMessages .DISABLED_ERR_MSG ));
211
+
212
+ // test behavior when AD is enabled
213
+ updateClusterSettings (ADEnabledSetting .AD_ENABLED , true );
214
+ Response response = TestHelpers
215
+ .makeRequest (client (), "POST" , TestHelpers .AD_BASE_DETECTORS_URI , ImmutableMap .of (), TestHelpers .toHttpEntity (detector ), null );
216
+ assertEquals ("Create anomaly detector with flattened result index failed" , RestStatus .CREATED , TestHelpers .restStatus (response ));
217
+ Map <String , Object > responseMap = entityAsMap (response );
218
+ String id = (String ) responseMap .get ("_id" );
219
+ int version = (int ) responseMap .get ("_version" );
220
+ assertNotEquals ("response is missing Id" , AnomalyDetector .NO_ID , id );
221
+ assertTrue ("incorrect version" , version > 0 );
222
+ // ensure the flattened result index was created
223
+ String expectedFlattenedIndex = String .format (
224
+ "opensearch-ad-plugin-result-test_flattened_%s" ,
225
+ id .toLowerCase (Locale .ROOT )
226
+ );
227
+ boolean indexExists = indexExists (expectedFlattenedIndex );
228
+ assertTrue (indexExists );
229
+ }
230
+
183
231
public void testCreateAnomalyDetector () throws Exception {
184
232
AnomalyDetector detector = createIndexAndGetAnomalyDetector (INDEX_NAME );
185
233
updateClusterSettings (ADEnabledSetting .AD_ENABLED , false );
0 commit comments