diff --git a/src/main/java/org/opensearch/agent/tools/PPLTool.java b/src/main/java/org/opensearch/agent/tools/PPLTool.java index 5cc2b23a..cec1114d 100644 --- a/src/main/java/org/opensearch/agent/tools/PPLTool.java +++ b/src/main/java/org/opensearch/agent/tools/PPLTool.java @@ -148,9 +148,12 @@ public void run(Map parameters, ActionListener listener) GetMappingsRequest getMappingsRequest = buildGetMappingRequest(indexName); client.admin().indices().getMappings(getMappingsRequest, ActionListener.wrap(getMappingsResponse -> { Map mappings = getMappingsResponse.getMappings(); + if (mappings.size() == 0) { + throw new IllegalArgumentException("No matching mapping with index name: " + indexName); + } client.search(searchRequest, ActionListener.wrap(searchResponse -> { SearchHit[] searchHits = searchResponse.getHits().getHits(); - String tableInfo = constructTableInfo(searchHits, mappings, indexName); + String tableInfo = constructTableInfo(searchHits, mappings); String prompt = constructPrompt(tableInfo, question, indexName); RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet .builder() @@ -286,9 +289,9 @@ private GetMappingsRequest buildGetMappingRequest(String indexName) { return getMappingsRequest; } - private String constructTableInfo(SearchHit[] searchHits, Map mappings, String indexName) - throws PrivilegedActionException { - MappingMetadata mappingMetadata = mappings.get(indexName); + private String constructTableInfo(SearchHit[] searchHits, Map mappings) throws PrivilegedActionException { + String firstIndexName = (String) mappings.keySet().toArray()[0]; + MappingMetadata mappingMetadata = mappings.get(firstIndexName); Map mappingSource = (Map) mappingMetadata.getSourceAsMap().get("properties"); Map fieldsToType = new HashMap<>(); extractNamesTypes(mappingSource, fieldsToType, "");