Skip to content

Commit 2ccd83f

Browse files
committed
[ML] fixing bug when returning multi-doc compressed model definitions
1 parent 4c6ab08 commit 2ccd83f

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.common.CheckedBiFunction;
3636
import org.elasticsearch.common.Numbers;
3737
import org.elasticsearch.common.Strings;
38+
import org.elasticsearch.common.bytes.BytesArray;
3839
import org.elasticsearch.common.bytes.BytesReference;
3940
import org.elasticsearch.common.bytes.CompositeBytesReference;
4041
import org.elasticsearch.common.regex.Regex;
@@ -1222,11 +1223,14 @@ private static <T> List<T> handleHits(
12221223
private static BytesReference getDefinitionFromDocs(List<TrainedModelDefinitionDoc> docs, String modelId)
12231224
throws ElasticsearchException {
12241225

1225-
BytesReference[] bb = new BytesReference[docs.size()];
1226-
for (int i = 0; i < docs.size(); i++) {
1227-
bb[i] = docs.get(i).getBinaryData();
1228-
}
1229-
BytesReference bytes = CompositeBytesReference.of(bb);
1226+
// If the user requested the compressed data string, we need access to the underlying bytes.
1227+
// BytesArray gives us that access.
1228+
BytesReference bytes = docs.size() == 1
1229+
? docs.get(0).getBinaryData()
1230+
: new BytesArray(
1231+
CompositeBytesReference.of(docs.stream().map(TrainedModelDefinitionDoc::getBinaryData).toArray(BytesReference[]::new))
1232+
.toBytesRef()
1233+
);
12301234

12311235
if (docs.get(0).getTotalDefinitionLength() != null) {
12321236
if (bytes.length() != docs.get(0).getTotalDefinitionLength()) {
@@ -1264,7 +1268,6 @@ private TrainedModelConfig.Builder parseModelConfigLenientlyFromSource(BytesRefe
12641268
// lang ident model were the only models supported. Models created after
12651269
// VERSION_3RD_PARTY_CONFIG_ADDED must have modelType set, if not set modelType
12661270
// is a tree ensemble
1267-
assert builder.getVersion().before(TrainedModelConfig.VERSION_3RD_PARTY_CONFIG_ADDED);
12681271
builder.setModelType(TrainedModelType.TREE_ENSEMBLE);
12691272
}
12701273
return builder;

0 commit comments

Comments
 (0)