Skip to content

Commit 42e9e82

Browse files
committed
adds json parsing to modelOutputValue during query rewrite
Signed-off-by: Brian Flores <iflorbri@amazon.com>
1 parent ade09e6 commit 42e9e82

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

plugin/src/main/java/org/opensearch/ml/processor/MLInferenceSearchRequestProcessor.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44
*/
55
package org.opensearch.ml.processor;
66

7-
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
8-
import static org.opensearch.ml.common.utils.StringUtils.toJson;
9-
import static org.opensearch.ml.processor.InferenceProcessorAttributes.INPUT_MAP;
10-
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MAX_PREDICTION_TASKS;
11-
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MODEL_CONFIG;
12-
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MODEL_ID;
13-
import static org.opensearch.ml.processor.InferenceProcessorAttributes.OUTPUT_MAP;
14-
import static org.opensearch.ml.processor.ModelExecutor.combineMaps;
15-
16-
import java.io.IOException;
17-
import java.util.Collection;
18-
import java.util.HashMap;
19-
import java.util.HashSet;
20-
import java.util.List;
21-
import java.util.Map;
22-
import java.util.Set;
23-
7+
import com.google.gson.Gson;
8+
import com.jayway.jsonpath.Configuration;
9+
import com.jayway.jsonpath.JsonPath;
10+
import com.jayway.jsonpath.Option;
11+
import com.jayway.jsonpath.PathNotFoundException;
12+
import com.jayway.jsonpath.ReadContext;
13+
import lombok.Getter;
2414
import org.apache.commons.text.StringSubstitutor;
2515
import org.apache.logging.log4j.LogManager;
2616
import org.apache.logging.log4j.Logger;
@@ -46,13 +36,22 @@
4636
import org.opensearch.search.pipeline.SearchRequestProcessor;
4737
import org.opensearch.transport.client.Client;
4838

49-
import com.jayway.jsonpath.Configuration;
50-
import com.jayway.jsonpath.JsonPath;
51-
import com.jayway.jsonpath.Option;
52-
import com.jayway.jsonpath.PathNotFoundException;
53-
import com.jayway.jsonpath.ReadContext;
39+
import java.io.IOException;
40+
import java.util.Collection;
41+
import java.util.HashMap;
42+
import java.util.HashSet;
43+
import java.util.List;
44+
import java.util.Map;
45+
import java.util.Set;
5446

55-
import lombok.Getter;
47+
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
48+
import static org.opensearch.ml.common.utils.StringUtils.toJson;
49+
import static org.opensearch.ml.processor.InferenceProcessorAttributes.INPUT_MAP;
50+
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MAX_PREDICTION_TASKS;
51+
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MODEL_CONFIG;
52+
import static org.opensearch.ml.processor.InferenceProcessorAttributes.MODEL_ID;
53+
import static org.opensearch.ml.processor.InferenceProcessorAttributes.OUTPUT_MAP;
54+
import static org.opensearch.ml.processor.ModelExecutor.combineMaps;
5655

5756
/**
5857
* MLInferenceSearchRequestProcessor requires a modelId string to call model inferences
@@ -360,6 +359,9 @@ private String updateQueryTemplate(String queryTemplate, Map<String, String> out
360359
String newQueryField = outputMapEntry.getKey();
361360
String modelOutputFieldName = outputMapEntry.getValue();
362361
Object modelOutputValue = getModelOutputValue(mlOutput, modelOutputFieldName, ignoreMissing, fullResponsePath);
362+
if (modelOutputValue instanceof Map) {
363+
modelOutputValue = new Gson().toJson(modelOutputValue);
364+
}
363365
valuesMap.put(newQueryField, modelOutputValue);
364366
}
365367
StringSubstitutor sub = new StringSubstitutor(valuesMap);

0 commit comments

Comments
 (0)