From fcde2a558cebb9d664ecdc56d4d29b8d8b8a2ce3 Mon Sep 17 00:00:00 2001 From: xinyual Date: Sat, 25 Nov 2023 22:17:56 +0800 Subject: [PATCH] allow zero index Signed-off-by: xinyual --- .../opensearch/ml/engine/tools/PPLTool.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/PPLTool.java b/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/PPLTool.java index 6c4887dc11..fcaad98d87 100644 --- a/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/PPLTool.java +++ b/ml-algorithms/src/main/java/org/opensearch/ml/engine/tools/PPLTool.java @@ -214,19 +214,29 @@ private String constructTableInfo(SearchHit [] searchHits, Map mappingSource = (Map) mappingMetadata.getSourceAsMap().get("properties"); Map fieldsToType = new HashMap<>(); extractNamesTypes(mappingSource, fieldsToType, ""); - - SearchHit hit = searchHits[0]; - Map sampleSource = hit.getSourceAsMap(); - Map fieldsToSample = new HashMap<>(); - for (String key : fieldsToType.keySet()) { - fieldsToSample.put(key, ""); - } - extractSamples(sampleSource, fieldsToSample, ""); StringJoiner tableInfoJoiner = new StringJoiner("\n"); - for (String key : fieldsToType.keySet()) { - String line = "- " + key + ": " + fieldsToType.get(key) + " (" + fieldsToSample.get(key) + ")"; - tableInfoJoiner.add(line); + if (searchHits.length > 0) { + SearchHit hit = searchHits[0]; + Map sampleSource = hit.getSourceAsMap(); + Map fieldsToSample = new HashMap<>(); + for (String key : fieldsToType.keySet()) { + fieldsToSample.put(key, ""); + } + extractSamples(sampleSource, fieldsToSample, ""); + + + for (String key : fieldsToType.keySet()) { + String line = "- " + key + ": " + fieldsToType.get(key) + " (" + fieldsToSample.get(key) + ")"; + tableInfoJoiner.add(line); + } + } + else + { + for (String key : fieldsToType.keySet()) { + String line = "- " + key + ": " + fieldsToType.get(key); + tableInfoJoiner.add(line); + } } String tableInfo = tableInfoJoiner.toString();