|
7 | 7 | import static org.opensearch.ml.processor.InferenceProcessorAttributes.*;
|
8 | 8 |
|
9 | 9 | import java.io.IOException;
|
10 |
| -import java.util.ArrayList; |
11 | 10 | import java.util.Collection;
|
12 | 11 | import java.util.HashMap;
|
13 | 12 | import java.util.HashSet;
|
|
37 | 36 | import org.opensearch.script.ScriptService;
|
38 | 37 | import org.opensearch.script.TemplateScript;
|
39 | 38 |
|
40 |
| -import com.jayway.jsonpath.Configuration; |
41 | 39 | import com.jayway.jsonpath.JsonPath;
|
42 |
| -import com.jayway.jsonpath.Option; |
43 | 40 |
|
44 | 41 | /**
|
45 | 42 | * MLInferenceIngestProcessor requires a modelId string to call model inferences
|
@@ -75,11 +72,6 @@ public class MLInferenceIngestProcessor extends AbstractProcessor implements Mod
|
75 | 72 | public static final String DEFAULT_MODEl_INPUT = "{ \"parameters\": ${ml_inference.parameters} }";
|
76 | 73 | private final NamedXContentRegistry xContentRegistry;
|
77 | 74 |
|
78 |
| - private Configuration suppressExceptionConfiguration = Configuration |
79 |
| - .builder() |
80 |
| - .options(Option.SUPPRESS_EXCEPTIONS, Option.DEFAULT_PATH_LEAF_TO_NULL, Option.ALWAYS_RETURN_LIST) |
81 |
| - .build(); |
82 |
| - |
83 | 75 | protected MLInferenceIngestProcessor(
|
84 | 76 | String modelId,
|
85 | 77 | List<Map<String, String>> inputMaps,
|
@@ -320,24 +312,29 @@ private void getMappedModelInputFromDocuments(
|
320 | 312 | Object documentFieldValue = ingestDocument.getFieldValue(originalFieldPath, Object.class);
|
321 | 313 | String documentFieldValueAsString = toString(documentFieldValue);
|
322 | 314 | updateModelParameters(modelInputFieldName, documentFieldValueAsString, modelParameters);
|
| 315 | + return; |
323 | 316 | }
|
324 |
| - // else when cannot find field path in document, try check for nested array using json path |
325 |
| - else { |
326 |
| - if (documentFieldName.contains(DOT_SYMBOL)) { |
327 |
| - |
328 |
| - Map<String, Object> sourceObject = ingestDocument.getSourceAndMetadata(); |
329 |
| - ArrayList<Object> fieldValueList = JsonPath |
330 |
| - .using(suppressExceptionConfiguration) |
331 |
| - .parse(sourceObject) |
332 |
| - .read(documentFieldName); |
333 |
| - if (!fieldValueList.isEmpty()) { |
334 |
| - updateModelParameters(modelInputFieldName, toString(fieldValueList), modelParameters); |
335 |
| - } else if (!ignoreMissing) { |
336 |
| - throw new IllegalArgumentException("cannot find field name defined from input map: " + documentFieldName); |
| 317 | + // If the standard dot path fails, try to check for a nested array using JSON path |
| 318 | + if (StringUtils.isValidJSONPath(documentFieldName)) { |
| 319 | + Map<String, Object> sourceObject = ingestDocument.getSourceAndMetadata(); |
| 320 | + Object fieldValue = JsonPath.using(suppressExceptionConfiguration).parse(sourceObject).read(documentFieldName); |
| 321 | + |
| 322 | + if (fieldValue != null) { |
| 323 | + if (fieldValue instanceof List) { |
| 324 | + List<?> fieldValueList = (List<?>) fieldValue; |
| 325 | + if (!fieldValueList.isEmpty()) { |
| 326 | + updateModelParameters(modelInputFieldName, toString(fieldValueList), modelParameters); |
| 327 | + } else if (!ignoreMissing) { |
| 328 | + throw new IllegalArgumentException("Cannot find field name defined from input map: " + documentFieldName); |
| 329 | + } |
| 330 | + } else { |
| 331 | + updateModelParameters(modelInputFieldName, toString(fieldValue), modelParameters); |
337 | 332 | }
|
338 | 333 | } else if (!ignoreMissing) {
|
339 |
| - throw new IllegalArgumentException("cannot find field name defined from input map: " + documentFieldName); |
| 334 | + throw new IllegalArgumentException("Cannot find field name defined from input map: " + documentFieldName); |
340 | 335 | }
|
| 336 | + } else { |
| 337 | + throw new IllegalArgumentException("Cannot find field name defined from input map: " + documentFieldName); |
341 | 338 | }
|
342 | 339 | }
|
343 | 340 |
|
|
0 commit comments