forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...rc/main/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.ml.inference.results; | ||
|
||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.inference.InferenceResults; | ||
import org.elasticsearch.xcontent.ParseField; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class ErrorInferenceResults implements InferenceResults { | ||
|
||
public static final String NAME = "error"; | ||
public static final ParseField WARNING = new ParseField("error"); | ||
|
||
private final Exception exception; | ||
|
||
public ErrorInferenceResults(Exception exception) { | ||
this.exception = Objects.requireNonNull(exception); | ||
} | ||
|
||
public ErrorInferenceResults(StreamInput in) throws IOException { | ||
this.exception = in.readException(); | ||
} | ||
|
||
public Exception getException() { | ||
return exception; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeException(exception); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object == this) { | ||
return true; | ||
} | ||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
ErrorInferenceResults that = (ErrorInferenceResults) object; | ||
// Just compare the message for serialization test purposes | ||
return Objects.equals(exception.getMessage(), that.exception.getMessage()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
// Just compare the message for serialization test purposes | ||
return Objects.hash(exception.getMessage()); | ||
} | ||
|
||
@Override | ||
public String getResultsField() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> asMap() { | ||
Map<String, Object> asMap = new LinkedHashMap<>(); | ||
asMap.put(NAME, exception.getMessage()); | ||
return asMap; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
|
||
@Override | ||
public Object predictedValue() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.field(NAME, exception.getMessage()); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...st/java/org/elasticsearch/xpack/core/ml/inference/results/ErrorInferenceResultsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.ml.inference.results; | ||
|
||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class ErrorInferenceResultsTests extends InferenceResultsTestCase<ErrorInferenceResults> { | ||
|
||
@Override | ||
protected Writeable.Reader<ErrorInferenceResults> instanceReader() { | ||
return ErrorInferenceResults::new; | ||
} | ||
|
||
@Override | ||
protected ErrorInferenceResults createTestInstance() { | ||
return new ErrorInferenceResults(new ElasticsearchStatusException(randomAlphaOfLength(8), randomFrom(RestStatus.values()))); | ||
} | ||
|
||
@Override | ||
protected ErrorInferenceResults mutateInstance(ErrorInferenceResults instance) throws IOException { | ||
return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929 | ||
} | ||
|
||
@Override | ||
void assertFieldValues(ErrorInferenceResults createdInstance, IngestDocument document, String resultsField) { | ||
assertThat(document.getFieldValue(resultsField + ".error", String.class), equalTo(createdInstance.getException().getMessage())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters