diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java index d6550964f9732..12a2fc6c93032 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java @@ -1216,6 +1216,8 @@ public void testPutDataFrameAnalyticsConfig() throws Exception { .setAnalysis(OutlierDetection.createDefault()) .build(); + createIndex("put-test-source-index", defaultMappingForTest()); + PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); @@ -1243,6 +1245,8 @@ public void testGetDataFrameAnalyticsConfig_SingleConfig() throws Exception { .setAnalysis(OutlierDetection.createDefault()) .build(); + createIndex("get-test-source-index", defaultMappingForTest()); + PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); @@ -1256,6 +1260,8 @@ public void testGetDataFrameAnalyticsConfig_SingleConfig() throws Exception { } public void testGetDataFrameAnalyticsConfig_MultipleConfigs() throws Exception { + createIndex("get-test-source-index", defaultMappingForTest()); + MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); String configIdPrefix = "get-test-config-"; int numberOfConfigs = 10; @@ -1461,6 +1467,8 @@ public void testDeleteDataFrameAnalyticsConfig() throws Exception { .setAnalysis(OutlierDetection.createDefault()) .build(); + createIndex("delete-test-source-index", defaultMappingForTest()); + GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configId + "*"), machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java index 0203a3c855d14..f33926582e3ea 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java @@ -2802,6 +2802,8 @@ public void onFailure(Exception e) { } public void testGetDataFrameAnalytics() throws Exception { + createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()); + RestHighLevelClient client = highLevelClient(); client.machineLearning().putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(DF_ANALYTICS_CONFIG), RequestOptions.DEFAULT); { @@ -2849,6 +2851,8 @@ public void onFailure(Exception e) { } public void testGetDataFrameAnalyticsStats() throws Exception { + createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()); + RestHighLevelClient client = highLevelClient(); client.machineLearning().putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(DF_ANALYTICS_CONFIG), RequestOptions.DEFAULT); { @@ -2897,6 +2901,8 @@ public void onFailure(Exception e) { } public void testPutDataFrameAnalytics() throws Exception { + createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()); + RestHighLevelClient client = highLevelClient(); { // tag::put-data-frame-analytics-query-config @@ -2988,6 +2994,8 @@ public void onFailure(Exception e) { } public void testDeleteDataFrameAnalytics() throws Exception { + createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()); + RestHighLevelClient client = highLevelClient(); client.machineLearning().putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(DF_ANALYTICS_CONFIG), RequestOptions.DEFAULT); { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java index 98f1bdeb19189..3bc435336f062 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java @@ -13,11 +13,15 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.indices.InvalidIndexNameException; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; +import java.util.Locale; import java.util.Objects; +import static org.elasticsearch.cluster.metadata.MetaDataCreateIndexService.validateIndexOrAliasName; + public class DataFrameAnalyticsDest implements Writeable, ToXContentObject { public static final ParseField INDEX = new ParseField("index"); @@ -90,4 +94,13 @@ public String getIndex() { public String getResultsField() { return resultsField; } + + public void validate() { + if (index != null) { + validateIndexOrAliasName(index, InvalidIndexNameException::new); + if (index.toLowerCase(Locale.ROOT).equals(index) == false) { + throw new InvalidIndexNameException(index, "dest.index must be lowercase"); + } + } + } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java index 7332687723805..bf8ce4c8a99b0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDestTests.java @@ -7,10 +7,14 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.indices.InvalidIndexNameException; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; +import static org.hamcrest.Matchers.equalTo; + public class DataFrameAnalyticsDestTests extends AbstractSerializingTestCase { @Override @@ -33,4 +37,19 @@ public static DataFrameAnalyticsDest createRandom() { protected Writeable.Reader instanceReader() { return DataFrameAnalyticsDest::new; } + + public void testValidate_GivenIndexWithFunkyChars() { + expectThrows(InvalidIndexNameException.class, () -> new DataFrameAnalyticsDest("