diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/OpenSearchIndex.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/OpenSearchIndex.java index 6e5b68e440..9ed8adf3ee 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/OpenSearchIndex.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/OpenSearchIndex.java @@ -47,7 +47,12 @@ public class OpenSearchIndex implements Table { /** * The cached mapping of field and type in index. */ - private Map cachedFieldTypes = null; + private Map cachedFieldOpenSearchTypes = null; + + /** + * The cached ExprType of fields. + */ + private Map cachedFieldTypes = null; /** * The cached max result window setting of index. @@ -75,7 +80,6 @@ public void create(Map schema) { mappings.put("properties", properties); for (Map.Entry colType : schema.entrySet()) { - //properties.put(colType.getKey(), OpenSearchDataType.getOpenSearchType(colType.getValue())); properties.put(colType.getKey(), colType.getValue().legacyTypeName().toLowerCase()); } client.createIndex(indexName.toString(), mappings); @@ -93,14 +97,18 @@ public void create(Map schema) { */ @Override public Map getFieldTypes() { + if (cachedFieldOpenSearchTypes == null) { + cachedFieldOpenSearchTypes = new OpenSearchDescribeIndexRequest(client, indexName) + .getFieldTypes(); + } if (cachedFieldTypes == null) { - cachedFieldTypes = new OpenSearchDescribeIndexRequest(client, indexName).getFieldTypes(); + cachedFieldTypes = OpenSearchDataType.traverseAndFlatten(cachedFieldOpenSearchTypes) + .entrySet().stream().collect( + LinkedHashMap::new, + (map, item) -> map.put(item.getKey(), item.getValue().getExprType()), + Map::putAll); } - return OpenSearchDataType.traverseAndFlatten(cachedFieldTypes).entrySet().stream() - .collect( - LinkedHashMap::new, - (map, item) -> map.put(item.getKey(), item.getValue().getExprType()), - Map::putAll); + return cachedFieldTypes; } /** @@ -108,10 +116,11 @@ public Map getFieldTypes() { * @return A complete map between field names and their types. */ public Map getFieldOpenSearchTypes() { - if (cachedFieldTypes == null) { - cachedFieldTypes = new OpenSearchDescribeIndexRequest(client, indexName).getFieldTypes(); + if (cachedFieldOpenSearchTypes == null) { + cachedFieldOpenSearchTypes = new OpenSearchDescribeIndexRequest(client, indexName) + .getFieldTypes(); } - return cachedFieldTypes; + return cachedFieldOpenSearchTypes; } /**