Skip to content

[ML] Parse and index inference model (#48016) #48152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class TrainedModelConfig implements ToXContentObject {

public static final String NAME = "trained_model_doc";
public static final String NAME = "trained_model_config";

public static final ParseField MODEL_ID = new ParseField("model_id");
public static final ParseField CREATED_BY = new ParseField("created_by");
public static final ParseField VERSION = new ParseField("version");
public static final ParseField DESCRIPTION = new ParseField("description");
public static final ParseField CREATED_TIME = new ParseField("created_time");
public static final ParseField MODEL_VERSION = new ParseField("model_version");
public static final ParseField CREATE_TIME = new ParseField("create_time");
public static final ParseField DEFINITION = new ParseField("definition");
public static final ParseField MODEL_TYPE = new ParseField("model_type");
public static final ParseField TAGS = new ParseField("tags");
public static final ParseField METADATA = new ParseField("metadata");

public static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>(NAME,
Expand All @@ -55,16 +55,15 @@ public class TrainedModelConfig implements ToXContentObject {
PARSER.declareString(TrainedModelConfig.Builder::setCreatedBy, CREATED_BY);
PARSER.declareString(TrainedModelConfig.Builder::setVersion, VERSION);
PARSER.declareString(TrainedModelConfig.Builder::setDescription, DESCRIPTION);
PARSER.declareField(TrainedModelConfig.Builder::setCreatedTime,
(p, c) -> TimeUtil.parseTimeFieldToInstant(p, CREATED_TIME.getPreferredName()),
CREATED_TIME,
PARSER.declareField(TrainedModelConfig.Builder::setCreateTime,
(p, c) -> TimeUtil.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()),
CREATE_TIME,
ObjectParser.ValueType.VALUE);
PARSER.declareLong(TrainedModelConfig.Builder::setModelVersion, MODEL_VERSION);
PARSER.declareString(TrainedModelConfig.Builder::setModelType, MODEL_TYPE);
PARSER.declareObject(TrainedModelConfig.Builder::setMetadata, (p, c) -> p.map(), METADATA);
PARSER.declareObject(TrainedModelConfig.Builder::setDefinition,
(p, c) -> TrainedModelDefinition.fromXContent(p),
DEFINITION);
PARSER.declareStringArray(TrainedModelConfig.Builder::setTags, TAGS);
PARSER.declareObject(TrainedModelConfig.Builder::setMetadata, (p, c) -> p.map(), METADATA);
}

public static TrainedModelConfig.Builder fromXContent(XContentParser parser) throws IOException {
Expand All @@ -75,30 +74,27 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser) thr
private final String createdBy;
private final Version version;
private final String description;
private final Instant createdTime;
private final Long modelVersion;
private final String modelType;
private final Map<String, Object> metadata;
private final Instant createTime;
private final TrainedModelDefinition definition;
private final List<String> tags;
private final Map<String, Object> metadata;

TrainedModelConfig(String modelId,
String createdBy,
Version version,
String description,
Instant createdTime,
Long modelVersion,
String modelType,
Instant createTime,
TrainedModelDefinition definition,
List<String> tags,
Map<String, Object> metadata) {
this.modelId = modelId;
this.createdBy = createdBy;
this.version = version;
this.createdTime = Instant.ofEpochMilli(createdTime.toEpochMilli());
this.modelType = modelType;
this.createTime = Instant.ofEpochMilli(createTime.toEpochMilli());
this.definition = definition;
this.description = description;
this.tags = tags == null ? null : Collections.unmodifiableList(tags);
this.metadata = metadata == null ? null : Collections.unmodifiableMap(metadata);
this.modelVersion = modelVersion;
}

public String getModelId() {
Expand All @@ -117,16 +113,12 @@ public String getDescription() {
return description;
}

public Instant getCreatedTime() {
return createdTime;
public Instant getCreateTime() {
return createTime;
}

public Long getModelVersion() {
return modelVersion;
}

public String getModelType() {
return modelType;
public List<String> getTags() {
return tags;
}

public Map<String, Object> getMetadata() {
Expand Down Expand Up @@ -156,18 +148,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (description != null) {
builder.field(DESCRIPTION.getPreferredName(), description);
}
if (createdTime != null) {
builder.timeField(CREATED_TIME.getPreferredName(), CREATED_TIME.getPreferredName() + "_string", createdTime.toEpochMilli());
}
if (modelVersion != null) {
builder.field(MODEL_VERSION.getPreferredName(), modelVersion);
}
if (modelType != null) {
builder.field(MODEL_TYPE.getPreferredName(), modelType);
if (createTime != null) {
builder.timeField(CREATE_TIME.getPreferredName(), CREATE_TIME.getPreferredName() + "_string", createTime.toEpochMilli());
}
if (definition != null) {
builder.field(DEFINITION.getPreferredName(), definition);
}
if (tags != null) {
builder.field(TAGS.getPreferredName(), tags);
}
if (metadata != null) {
builder.field(METADATA.getPreferredName(), metadata);
}
Expand All @@ -189,10 +178,9 @@ public boolean equals(Object o) {
Objects.equals(createdBy, that.createdBy) &&
Objects.equals(version, that.version) &&
Objects.equals(description, that.description) &&
Objects.equals(createdTime, that.createdTime) &&
Objects.equals(modelVersion, that.modelVersion) &&
Objects.equals(modelType, that.modelType) &&
Objects.equals(createTime, that.createTime) &&
Objects.equals(definition, that.definition) &&
Objects.equals(tags, that.tags) &&
Objects.equals(metadata, that.metadata);
}

Expand All @@ -201,12 +189,11 @@ public int hashCode() {
return Objects.hash(modelId,
createdBy,
version,
createdTime,
modelType,
createTime,
definition,
description,
metadata,
modelVersion);
tags,
metadata);
}


Expand All @@ -216,11 +203,10 @@ public static class Builder {
private String createdBy;
private Version version;
private String description;
private Instant createdTime;
private Long modelVersion;
private String modelType;
private Instant createTime;
private Map<String, Object> metadata;
private TrainedModelDefinition.Builder definition;
private List<String> tags;
private TrainedModelDefinition definition;

public Builder setModelId(String modelId) {
this.modelId = modelId;
Expand All @@ -246,18 +232,13 @@ public Builder setDescription(String description) {
return this;
}

private Builder setCreatedTime(Instant createdTime) {
this.createdTime = createdTime;
return this;
}

public Builder setModelVersion(Long modelVersion) {
this.modelVersion = modelVersion;
private Builder setCreateTime(Instant createTime) {
this.createTime = createTime;
return this;
}

public Builder setModelType(String modelType) {
this.modelType = modelType;
public Builder setTags(List<String> tags) {
this.tags = tags;
return this;
}

Expand All @@ -267,6 +248,11 @@ public Builder setMetadata(Map<String, Object> metadata) {
}

public Builder setDefinition(TrainedModelDefinition.Builder definition) {
this.definition = definition == null ? null : definition.build();
return this;
}

public Builder setDefinition(TrainedModelDefinition definition) {
this.definition = definition;
return this;
}
Expand All @@ -277,10 +263,9 @@ public TrainedModelConfig build() {
createdBy,
version,
description,
createdTime,
modelVersion,
modelType,
definition == null ? null : definition.build(),
createTime,
definition,
tags,
metadata);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class TrainedModelConfigTests extends AbstractXContentTestCase<TrainedModelConfig> {
Expand Down Expand Up @@ -58,9 +60,9 @@ protected TrainedModelConfig createTestInstance() {
Version.CURRENT,
randomBoolean() ? null : randomAlphaOfLength(100),
Instant.ofEpochMilli(randomNonNegativeLong()),
randomBoolean() ? null : randomNonNegativeLong(),
randomAlphaOfLength(10),
randomBoolean() ? null : TrainedModelDefinitionTests.createRandomBuilder().build(),
randomBoolean() ? null :
Stream.generate(() -> randomAlphaOfLength(10)).limit(randomIntBetween(0, 5)).collect(Collectors.toList()),
randomBoolean() ? null : Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10)));
}

Expand Down
Loading