From fc99bf5755cd71e90f56a231c607764c729517a9 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Tue, 3 Oct 2023 10:09:50 +0100 Subject: [PATCH] Revert "use non results fields" This reverts commit 2a9395d94a4e08fe7434a47f86755138c5ea6999. --- .../inference/InferenceResults.java | 20 ------------------- .../ClassificationInferenceResults.java | 8 +------- .../results/ErrorInferenceResults.java | 5 ----- .../ml/inference/results/FillMaskResults.java | 7 ------- .../core/ml/inference/results/NerResults.java | 6 ------ .../NlpClassificationInferenceResults.java | 11 ---------- .../results/NlpInferenceResults.java | 8 -------- .../QuestionAnsweringInferenceResults.java | 11 ---------- .../results/RawInferenceResults.java | 5 ----- .../results/RegressionInferenceResults.java | 8 +------- .../results/WarningInferenceResults.java | 5 ----- .../inference/ingest/InferenceProcessor.java | 12 ++++------- .../InferenceProcessorFactoryTests.java | 1 + 13 files changed, 7 insertions(+), 100 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/inference/InferenceResults.java b/server/src/main/java/org/elasticsearch/inference/InferenceResults.java index 4f01539873e7d..76dfcae2ba530 100644 --- a/server/src/main/java/org/elasticsearch/inference/InferenceResults.java +++ b/server/src/main/java/org/elasticsearch/inference/InferenceResults.java @@ -31,29 +31,9 @@ static void writeResult(InferenceResults results, IngestDocument ingestDocument, } } - static void writeResultToField(InferenceResults results, IngestDocument ingestDocument, String outputField, String modelId, boolean includeModelId) { - Objects.requireNonNull(results, "results"); - Objects.requireNonNull(ingestDocument, "ingestDocument"); - Objects.requireNonNull(outputField, "outputField"); - Map resultMap = results.asMap(); - if (includeModelId) { - resultMap.put(MODEL_ID_RESULTS_FIELD, modelId); - } - Object predictedValue = results.predictedValue(); - if (predictedValue != null) { - if (ingestDocument.hasField(outputField)) { - ingestDocument.appendFieldValue(outputField, results.predictedValue()); - } else { - ingestDocument.setFieldValue(outputField, resultMap); - } - } - } - String getResultsField(); Map asMap(); - Map nonResultFeatures(); - Object predictedValue(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java index c01f142380f63..ebf946aa34716 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java @@ -218,14 +218,8 @@ public String getResultsField() { @Override public Map asMap() { - Map map = nonResultFeatures(); - map.put(resultsField, predictionFieldType.transformPredictedValue(value(), valueAsString())); - return map; - } - - @Override - public Map nonResultFeatures() { Map map = new LinkedHashMap<>(); + map.put(resultsField, predictionFieldType.transformPredictedValue(value(), valueAsString())); if (topClasses.isEmpty() == false) { map.put(topNumClassesField, topClasses.stream().map(TopClassEntry::asValueMap).collect(Collectors.toList())); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResults.java index 3c81d7b6b266b..daf1175bbc547 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResults.java @@ -69,11 +69,6 @@ public String getResultsField() { @Override public Map asMap() { - return nonResultFeatures(); - } - - @Override - public Map nonResultFeatures() { Map asMap = new LinkedHashMap<>(); asMap.put(NAME, exception.getMessage()); return asMap; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResults.java index cac1ca23a1438..863efad3e3b0e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResults.java @@ -55,13 +55,6 @@ void addMapFields(Map map) { map.put(resultsField + "_sequence", predictedSequence); } - @Override - public Map nonResultFeatures() { - var map = super.nonResultFeatures(); - map.put(resultsField + "_sequence", predictedSequence); // TODO - return map; - } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java index 603bc9ccca8d3..b077c93c141a5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java @@ -73,12 +73,6 @@ void addMapFields(Map map) { map.put(ENTITY_FIELD, entityGroups.stream().map(EntityGroup::toMap).collect(Collectors.toList())); } - @Override - public Map nonResultFeatures() { - var map = super.nonResultFeatures(); - return Map.of(ENTITY_FIELD, entityGroups.stream().map(EntityGroup::toMap).collect(Collectors.toList())); - } - @Override public Object predictedValue() { return annotatedResult; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpClassificationInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpClassificationInferenceResults.java index 9695890969125..a49e81e40a7a6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpClassificationInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpClassificationInferenceResults.java @@ -100,17 +100,6 @@ public Object predictedValue() { @Override void addMapFields(Map map) { map.put(resultsField, classificationLabel); - addNonResultFeaturesToMap(map); - } - - @Override - public Map nonResultFeatures() { - var map = super.nonResultFeatures(); - addNonResultFeaturesToMap(map); - return map; - } - - private void addNonResultFeaturesToMap(Map map) { if (topClasses.isEmpty() == false) { map.put( NlpConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpInferenceResults.java index eec0e60f26c89..ab1939013e976 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NlpInferenceResults.java @@ -14,7 +14,6 @@ import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -66,13 +65,6 @@ public final Map asMap() { return map; } - @Override - public Map nonResultFeatures() { - var map = new HashMap(); - map.put("is_truncated", isTruncated); - return map; - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/QuestionAnsweringInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/QuestionAnsweringInferenceResults.java index 361eed2fcdee2..dec5343fe8ddf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/QuestionAnsweringInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/QuestionAnsweringInferenceResults.java @@ -120,18 +120,7 @@ public String predictedValue() { @Override void addMapFields(Map map) { - addNonResultFeaturesToMap(map); map.put(resultsField, answer); - } - - @Override - public Map nonResultFeatures() { - var map = super.nonResultFeatures(); - addNonResultFeaturesToMap(map); - return map; - } - - private void addNonResultFeaturesToMap(Map map) { map.put(START_OFFSET.getPreferredName(), startOffset); map.put(END_OFFSET.getPreferredName(), endOffset); if (topClasses.isEmpty() == false) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java index 5f81b7c9b7e9c..b7744a6c54800 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java @@ -69,11 +69,6 @@ public Map asMap() { throw new UnsupportedOperationException("[raw] does not support map conversion"); } - @Override - public Map nonResultFeatures() { - throw new UnsupportedOperationException("[raw] does not support map conversion of features"); - } - @Override public Object predictedValue() { return null; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java index 9676f1eb9f4cb..e0af785f827e3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java @@ -120,14 +120,8 @@ public String getResultsField() { @Override public Map asMap() { - Map map = nonResultFeatures(); - map.put(resultsField, predictedValue()); - return map; - } - - @Override - public Map nonResultFeatures() { Map map = new LinkedHashMap<>(); + map.put(resultsField, value()); if (featureImportance.isEmpty() == false) { map.put(FEATURE_IMPORTANCE, featureImportance.stream().map(RegressionFeatureImportance::toMap).collect(Collectors.toList())); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java index 1dc12d255d6ea..a956c91007934 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java @@ -70,11 +70,6 @@ public String getResultsField() { @Override public Map asMap() { - return nonResultFeatures(); - } - - @Override - public Map nonResultFeatures() { Map asMap = new LinkedHashMap<>(); asMap.put(NAME, warning); return asMap; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java index 122fe8024927e..4120cd6669e7f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java @@ -213,7 +213,7 @@ InferModelAction.Request buildRequest(IngestDocument ingestDocument) { if (configuredWithInputsFields) { List requestInputs = new ArrayList<>(); for (var inputFields : inputs) { - var lookup = (String) fields.get(inputFields.inputField); + var lookup = (String)fields.get(inputFields.inputField); if (lookup == null) { lookup = ""; // need to send a non-null request to the same number of results back } @@ -249,15 +249,11 @@ void mutateDocument(InferModelAction.Response response, IngestDocument ingestDoc if (configuredWithInputsFields) { if (response.getInferenceResults().size() != inputs.size()) { - throw new ElasticsearchStatusException( - "number of results [{}] does not match the number of inputs [{}]", - RestStatus.INTERNAL_SERVER_ERROR, - response.getInferenceResults().size(), - inputs.size() - ); + throw new ElasticsearchStatusException("number of results [{}] does not match the number of inputs [{}]", + RestStatus.INTERNAL_SERVER_ERROR, response.getInferenceResults().size(), inputs.size()); } - for (int i = 0; i < inputs.size(); i++) { + for (int i=0; i< inputs.size(); i++) { InferenceResults.writeResult( response.getInferenceResults().get(i), ingestDocument, diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorFactoryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorFactoryTests.java index 8e518208a36ed..e0c366f52aaf1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorFactoryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorFactoryTests.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.Tuple; +import org.elasticsearch.inference.InferenceResults; import org.elasticsearch.ingest.IngestMetadata; import org.elasticsearch.ingest.PipelineConfiguration; import org.elasticsearch.test.ESTestCase;