From 4c6934bf3dacc6403068a280cc9ab7643ff1fd91 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:06:24 +0800 Subject: [PATCH] [Backport 2.x] Fix CreateAnomalyDetectorTool doesn't support data streams and alias (#425) * Fix CreateAnomalyDetectorTool doesn't support data streams and alias (#423) * Fix CreateAnomalyDetectorTool doesn't support data streams and alias Signed-off-by: gaobinlong * Refine the code comment Signed-off-by: gaobinlong --------- Signed-off-by: gaobinlong (cherry picked from commit 988a68a3492e34a84d9aaea88b3eb7b206573e85) Signed-off-by: github-actions[bot] * Backport/backport 423 to 2.x (#428) * Fix test failure due to external change (#427) Signed-off-by: gaobinlong * Fix CreateAnomalyDetectorTool doesn't support data streams and alias (#423) * Fix CreateAnomalyDetectorTool doesn't support data streams and alias Signed-off-by: gaobinlong * Refine the code comment Signed-off-by: gaobinlong --------- Signed-off-by: gaobinlong (cherry picked from commit 988a68a3492e34a84d9aaea88b3eb7b206573e85) Signed-off-by: github-actions[bot] --------- Signed-off-by: gaobinlong Signed-off-by: github-actions[bot] Co-authored-by: gaobinlong Co-authored-by: github-actions[bot] --------- Signed-off-by: gaobinlong Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: zane-neo Co-authored-by: gaobinlong --- .../agent/tools/CreateAnomalyDetectorTool.java | 14 +++++--------- .../tools/CreateAnomalyDetectorToolTests.java | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/opensearch/agent/tools/CreateAnomalyDetectorTool.java b/src/main/java/org/opensearch/agent/tools/CreateAnomalyDetectorTool.java index 71fc77b5..60892940 100644 --- a/src/main/java/org/opensearch/agent/tools/CreateAnomalyDetectorTool.java +++ b/src/main/java/org/opensearch/agent/tools/CreateAnomalyDetectorTool.java @@ -186,13 +186,9 @@ public void run(Map parameters, ActionListener listener) throw new IllegalArgumentException("No mapping found for the index: " + indexName); } - MappingMetadata mappingMetadata; - // when the index name is wildcard pattern, we fetch the mappings of the first index - if (indexName.contains("*")) { - mappingMetadata = mappings.get((String) mappings.keySet().toArray()[0]); - } else { - mappingMetadata = mappings.get(indexName); - } + // when the index name is a wildcard pattern, a data stream, or an alias, we fetch the mappings of the first index + String firstIndexName = (String) mappings.keySet().toArray()[0]; + MappingMetadata mappingMetadata = mappings.get(firstIndexName); Map mappingSource = (Map) mappingMetadata.getSourceAsMap().get("properties"); if (Objects.isNull(mappingSource)) { @@ -224,7 +220,7 @@ public void run(Map parameters, ActionListener listener) .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); // construct the prompt - String prompt = constructPrompt(filteredMapping, indexName); + String prompt = constructPrompt(filteredMapping, firstIndexName); RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet .builder() .parameters(Collections.singletonMap("prompt", prompt)) @@ -278,7 +274,7 @@ public void run(Map parameters, ActionListener listener) Map result = ImmutableMap .of( OUTPUT_KEY_INDEX, - indexName, + firstIndexName, OUTPUT_KEY_CATEGORY_FIELD, categoryField, OUTPUT_KEY_AGGREGATION_FIELD, diff --git a/src/test/java/org/opensearch/agent/tools/CreateAnomalyDetectorToolTests.java b/src/test/java/org/opensearch/agent/tools/CreateAnomalyDetectorToolTests.java index 322ac4ae..30ca722d 100644 --- a/src/test/java/org/opensearch/agent/tools/CreateAnomalyDetectorToolTests.java +++ b/src/test/java/org/opensearch/agent/tools/CreateAnomalyDetectorToolTests.java @@ -71,7 +71,7 @@ public class CreateAnomalyDetectorToolTests { "{\"index\":\"http_logs\",\"categoryField\":\"\",\"aggregationField\":\"response,responseLatency\",\"aggregationMethod\":\"count,avg\",\"dateFields\":\"date\"}"; private String mockedResultForIndexPattern = - "{\"index\":\"http_logs*\",\"categoryField\":\"\",\"aggregationField\":\"response,responseLatency\",\"aggregationMethod\":\"count,avg\",\"dateFields\":\"date\"}"; + "{\"index\":\"http_logs\",\"categoryField\":\"\",\"aggregationField\":\"response,responseLatency\",\"aggregationMethod\":\"count,avg\",\"dateFields\":\"date\"}"; @Before public void setup() {