diff --git a/src/main/java/org/opensearch/neuralsearch/processor/TextChunkingProcessor.java b/src/main/java/org/opensearch/neuralsearch/processor/TextChunkingProcessor.java index 5ecaf1e10..7aaf53f00 100644 --- a/src/main/java/org/opensearch/neuralsearch/processor/TextChunkingProcessor.java +++ b/src/main/java/org/opensearch/neuralsearch/processor/TextChunkingProcessor.java @@ -101,15 +101,16 @@ private void validateAndParseAlgorithmMap(Map algorithmMap) { Set supportedChunkers = ChunkerFactory.getAllChunkers(); if (!supportedChunkers.contains(algorithmKey)) { throw new IllegalArgumentException( - "Unable to create the processor as chunker algorithm [" - + algorithmKey - + "] is not supported. Supported chunkers types are " - + supportedChunkers + String.format( + "Unable to create the processor as chunker algorithm [%s] is not supported. Supported chunkers types are [%s]", + algorithmKey, + supportedChunkers + ) ); } if (!(algorithmValue instanceof Map)) { throw new IllegalArgumentException( - "Unable to create the processor as [" + algorithmKey + "] parameters cannot be cast to [" + Map.class.getName() + "]" + String.format("Unable to create the processor as [%s] parameters cannot be cast to [%s]", algorithmKey, Map.class.getName()) ); } Map chunkerParameters = (Map) algorithmValue; @@ -121,12 +122,12 @@ private void validateAndParseAlgorithmMap(Map algorithmMap) { String maxChunkLimitString = chunkerParameters.get(MAX_CHUNK_LIMIT_FIELD).toString(); if (!(NumberUtils.isParsable(maxChunkLimitString))) { throw new IllegalArgumentException( - "Parameter [" + MAX_CHUNK_LIMIT_FIELD + "] cannot be cast to [" + Number.class.getName() + "]" + String.format("Parameter [%s] cannot be cast to [%s]", MAX_CHUNK_LIMIT_FIELD, Number.class.getName()) ); } int maxChunkLimit = NumberUtils.createInteger(maxChunkLimitString); if (maxChunkLimit <= 0 && maxChunkLimit != DEFAULT_MAX_CHUNK_LIMIT) { - throw new IllegalArgumentException("Parameter [" + MAX_CHUNK_LIMIT_FIELD + "] must be a positive integer"); + throw new IllegalArgumentException(String.format("Parameter [%s] must be a positive integer", MAX_CHUNK_LIMIT_FIELD)); } this.maxChunkLimit = maxChunkLimit; } else { @@ -154,11 +155,11 @@ private int chunkString(String content, List result, Map chunkCount += contentResult.size(); if (maxChunkLimit != DEFAULT_MAX_CHUNK_LIMIT && chunkCount > maxChunkLimit) { throw new IllegalArgumentException( - "Unable to create the processor as the number of chunks [" - + chunkCount - + "] exceeds the maximum chunk limit [" - + maxChunkLimit - + "]" + String.format( + "Unable to create the processor as the number of chunks [%s] exceeds the maximum chunk limit [%s]", + chunkCount, + maxChunkLimit + ) ); } result.addAll(contentResult); @@ -226,7 +227,9 @@ private void validateFieldsValue(IngestDocument ingestDocument) { if (sourceValue instanceof List || sourceValue instanceof Map) { validateNestedTypeValue(sourceKey, sourceValue, 1); } else if (!(sourceValue instanceof String)) { - throw new IllegalArgumentException("field [" + sourceKey + "] is neither string nor nested type, cannot process it"); + throw new IllegalArgumentException( + String.format("field [%s] is neither string nor nested type, cannot process it", sourceKey) + ); } } } @@ -235,7 +238,7 @@ private void validateFieldsValue(IngestDocument ingestDocument) { @SuppressWarnings({ "rawtypes", "unchecked" }) private void validateNestedTypeValue(String sourceKey, Object sourceValue, int maxDepth) { if (maxDepth > MapperService.INDEX_MAPPING_DEPTH_LIMIT_SETTING.get(environment.settings())) { - throw new IllegalArgumentException("map type field [" + sourceKey + "] reached max depth limit, cannot process it"); + throw new IllegalArgumentException(String.format("map type field [%s] reached max depth limit, cannot process it", sourceKey)); } else if (sourceValue instanceof List) { validateListTypeValue(sourceKey, sourceValue, maxDepth); } else if (sourceValue instanceof Map) { @@ -244,7 +247,7 @@ private void validateNestedTypeValue(String sourceKey, Object sourceValue, int m .filter(Objects::nonNull) .forEach(x -> validateNestedTypeValue(sourceKey, x, maxDepth + 1)); } else if (!(sourceValue instanceof String)) { - throw new IllegalArgumentException("map type field [" + sourceKey + "] has non-string type, cannot process it"); + throw new IllegalArgumentException(String.format("map type field [%s] has non-string type, cannot process it", sourceKey)); } } @@ -254,9 +257,11 @@ private void validateListTypeValue(String sourceKey, Object sourceValue, int max if (value instanceof Map) { validateNestedTypeValue(sourceKey, value, maxDepth + 1); } else if (value == null) { - throw new IllegalArgumentException("list type field [" + sourceKey + "] has null, cannot process it"); + throw new IllegalArgumentException(String.format("list type field [%s] has null, cannot process it", sourceKey)); } else if (!(value instanceof String)) { - throw new IllegalArgumentException("list type field [" + sourceKey + "] has non string value, cannot process it"); + throw new IllegalArgumentException( + String.format("list type field [%s] has non-string value, cannot process it", sourceKey) + ); } } } diff --git a/src/test/java/org/opensearch/neuralsearch/processor/TextChunkingProcessorTests.java b/src/test/java/org/opensearch/neuralsearch/processor/TextChunkingProcessorTests.java index 40b9729df..83c57aa82 100644 --- a/src/test/java/org/opensearch/neuralsearch/processor/TextChunkingProcessorTests.java +++ b/src/test/java/org/opensearch/neuralsearch/processor/TextChunkingProcessorTests.java @@ -453,7 +453,7 @@ public void testExecute_withFixedTokenLength_andSourceDataListHybridType_thenFai () -> processor.execute(ingestDocument) ); assertEquals( - "list type field [" + INPUT_FIELD + "] has non string value, cannot process it", + "list type field [" + INPUT_FIELD + "] has non-string value, cannot process it", illegalArgumentException.getMessage() ); }