Skip to content

Commit

Permalink
Mhange validation logic to raise exception when list type has null or…
Browse files Browse the repository at this point in the history
… empty string

Signed-off-by: Zan Niu <zaniu@amazon.com>
  • Loading branch information
zane-neo committed Oct 18, 2022
1 parent 6d3f042 commit 117249c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void validateEmbeddingConfiguration(Map<String, Object> fieldMap) {
.stream()
.anyMatch(x -> StringUtils.isBlank(x.getKey()) || Objects.isNull(x.getValue()) || StringUtils.isBlank(x.getValue().toString()))) {
throw new IllegalArgumentException(
"Unable to create the TextEmbedding processor as field_map is null or empty."
"Unable to create the TextEmbedding processor as field_map has invalid key or value"
);
}
}
Expand Down Expand Up @@ -90,12 +90,12 @@ void appendVectorFieldsToDocument(IngestDocument ingestDocument, Map<String, Obj
}

@SuppressWarnings({ "unchecked" })
private List<String> createInferenceList(Map<String, Object> knnMap) {
private List<String> createInferenceList(Map<String, Object> knnKeyMap) {
List<String> texts = new ArrayList<>();
knnMap.entrySet().stream().filter(knnMapEntry -> knnMapEntry.getValue() != null).forEach(knnMapEntry -> {
knnKeyMap.entrySet().stream().filter(knnMapEntry -> knnMapEntry.getValue() != null).forEach(knnMapEntry -> {
Object sourceValue = knnMapEntry.getValue();
if (sourceValue instanceof List) {
((List<String>) sourceValue).stream().filter(StringUtils::isNotBlank).forEach(texts::add);
texts.addAll(((List<String>) sourceValue));
} else if (sourceValue instanceof Map) {
createInferenceListForMapTypeInput(sourceValue, texts);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

package org.opensearch.neuralsearch.processor.factory;

import static org.opensearch.ingest.ConfigurationUtils.readOptionalMap;
import static org.opensearch.ingest.ConfigurationUtils.readStringProperty;
import static org.opensearch.neuralsearch.processor.TextEmbeddingProcessor.*;

import java.util.Map;

import org.opensearch.ingest.Processor;
import org.opensearch.neuralsearch.ml.MLCommonsClientAccessor;
import org.opensearch.neuralsearch.processor.TextEmbeddingProcessor;

import java.util.Map;

import static org.opensearch.ingest.ConfigurationUtils.readMap;
import static org.opensearch.ingest.ConfigurationUtils.readStringProperty;
import static org.opensearch.neuralsearch.processor.TextEmbeddingProcessor.*;

public class TextEmbeddingProcessorFactory implements Processor.Factory {

private final MLCommonsClientAccessor clientAccessor;
Expand All @@ -31,7 +31,7 @@ public TextEmbeddingProcessor create(
Map<String, Object> config
) throws Exception {
String modelId = readStringProperty(TYPE, processorTag, config, MODEL_ID_FIELD);
Map<String, Object> filedMap = readOptionalMap(TYPE, processorTag, config, FIELD_MAP_FIELD);
Map<String, Object> filedMap = readMap(TYPE, processorTag, config, FIELD_MAP_FIELD);
return new TextEmbeddingProcessor(processorTag, description, modelId, filedMap, clientAccessor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.opensearch.OpenSearchParseException;
import org.opensearch.ingest.IngestDocument;
import org.opensearch.ingest.Processor;
import org.opensearch.neuralsearch.ml.MLCommonsClientAccessor;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void testTextEmbeddingProcessConstructor_whenConfigMapError_throwIllegalA
try {
FACTORY.create(registry, PROCESSOR_TAG, DESCRIPTION, config);
} catch (IllegalArgumentException e) {
assertEquals("Unable to create the TextEmbedding processor as field_map is null or empty.", e.getMessage());
assertEquals("Unable to create the TextEmbedding processor as field_map has invalid key or value", e.getMessage());
}
}

Expand All @@ -61,8 +62,8 @@ public void testTextEmbeddingProcessConstructor_whenConfigMapEmpty_throwIllegalA
config.put(TextEmbeddingProcessor.MODEL_ID_FIELD, "mockModelId");
try {
FACTORY.create(registry, PROCESSOR_TAG, DESCRIPTION, config);
} catch (IllegalArgumentException e) {
assertEquals("Unable to create the TextEmbedding processor as field_map is null or empty.", e.getMessage());
} catch (OpenSearchParseException e) {
assertEquals("[field_map] required property is missing", e.getMessage());
}
}

Expand Down

0 comments on commit 117249c

Please sign in to comment.