Skip to content
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

fix index mapping #384

Merged
merged 1 commit into from
Aug 4, 2022
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 @@ -308,7 +308,7 @@ public void searchModel() {
verify(client).execute(eq(MLModelSearchAction.INSTANCE), isA(SearchRequest.class), any());
verify(searchModelActionListener).onResponse(argumentCaptor.capture());
Map<String, Object> source = argumentCaptor.getValue().getHits().getAt(0).getSourceAsMap();
assertEquals(modelContent, source.get(MLModel.MODEL_CONTENT));
assertEquals(modelContent, source.get(MLModel.MODEL_CONTENT_FIELD));
}

@Test
Expand Down
95 changes: 95 additions & 0 deletions common/src/main/java/org/opensearch/ml/common/CommonValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common;

public class CommonValue {

public static Integer NO_SCHEMA_VERSION = 0;
public static final String USER = "user";
public static final String META = "_meta";
public static final String SCHEMA_VERSION_FIELD = "schema_version";

public static final String ML_MODEL_INDEX = ".plugins-ml-model";
public static final String ML_TASK_INDEX = ".plugins-ml-task";
public static final Integer ML_MODEL_INDEX_SCHEMA_VERSION = 1;
public static final Integer ML_TASK_INDEX_SCHEMA_VERSION = 1;
public static final String USER_FIELD_MAPPING = " \""
+ CommonValue.USER
+ "\": {\n"
+ " \"type\": \"nested\",\n"
+ " \"properties\": {\n"
+ " \"name\": {\"type\":\"text\", \"fields\":{\"keyword\":{\"type\":\"keyword\", \"ignore_above\":256}}},\n"
+ " \"backend_roles\": {\"type\":\"text\", \"fields\":{\"keyword\":{\"type\":\"keyword\"}}},\n"
+ " \"roles\": {\"type\":\"text\", \"fields\":{\"keyword\":{\"type\":\"keyword\"}}},\n"
+ " \"custom_attribute_names\": {\"type\":\"text\", \"fields\":{\"keyword\":{\"type\":\"keyword\"}}}\n"
+ " }\n"
+ " }\n";
public static final String ML_MODEL_INDEX_MAPPING = "{\n"
+ " \"_meta\": {\"schema_version\": "
+ ML_MODEL_INDEX_SCHEMA_VERSION
+ "},\n"
+ " \"properties\": {\n"
+ " \""
+ MLModel.ALGORITHM_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLModel.MODEL_NAME_FIELD
+ "\" : {\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}},\n"
+ " \""
+ MLModel.MODEL_VERSION_FIELD
+ "\" : {\"type\": \"long\"},\n"
+ " \""
+ MLModel.MODEL_CONTENT_FIELD
+ "\" : {\"type\": \"binary\"},\n"
+ USER_FIELD_MAPPING
+ " }\n"
+ "}";

public static final String ML_TASK_INDEX_MAPPING = "{\n"
+ " \"_meta\": {\"schema_version\": "
+ ML_TASK_INDEX_SCHEMA_VERSION
+ "},\n"
+ " \"properties\": {\n"
+ " \""
+ MLTask.MODEL_ID_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.TASK_TYPE_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.FUNCTION_NAME_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.STATE_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.INPUT_TYPE_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.PROGRESS_FIELD
+ "\": {\"type\": \"float\"},\n"
+ " \""
+ MLTask.OUTPUT_INDEX_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.WORKER_NODE_FIELD
+ "\": {\"type\": \"keyword\"},\n"
+ " \""
+ MLTask.CREATE_TIME_FIELD
+ "\": {\"type\": \"date\", \"format\": \"strict_date_time||epoch_millis\"},\n"
+ " \""
+ MLTask.LAST_UPDATE_TIME_FIELD
+ "\": {\"type\": \"date\", \"format\": \"strict_date_time||epoch_millis\"},\n"
+ " \""
+ MLTask.ERROR_FIELD
+ "\": {\"type\": \"text\"},\n"
+ " \""
+ MLTask.IS_ASYNC_TASK_FIELD
+ "\" : {\"type\" : \"boolean\"}, \n"
+ USER_FIELD_MAPPING
+ " }\n"
+ "}";
}
33 changes: 19 additions & 14 deletions common/src/main/java/org/opensearch/ml/common/MLModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
import java.util.Base64;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.CommonValue.USER;

@Getter
public class MLModel implements ToXContentObject {
public static final String ALGORITHM = "algorithm";
public static final String MODEL_NAME = "name";
public static final String MODEL_VERSION = "version";
public static final String MODEL_CONTENT = "content";
public static final String USER = "user";
public static final String ALGORITHM_FIELD = "algorithm";
public static final String MODEL_NAME_FIELD = "name";
public static final String MODEL_VERSION_FIELD = "version";
public static final String OLD_MODEL_CONTENT_FIELD = "content";
public static final String MODEL_CONTENT_FIELD = "model_content";

private String name;
private FunctionName algorithm;
Expand Down Expand Up @@ -75,16 +76,16 @@ public void writeTo(StreamOutput out) throws IOException {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (name != null) {
builder.field(MODEL_NAME, name);
builder.field(MODEL_NAME_FIELD, name);
}
if (algorithm != null) {
builder.field(ALGORITHM, algorithm);
builder.field(ALGORITHM_FIELD, algorithm);
}
if (version != null) {
builder.field(MODEL_VERSION, version);
builder.field(MODEL_VERSION_FIELD, version);
}
if (content != null) {
builder.field(MODEL_CONTENT, content);
builder.field(MODEL_CONTENT_FIELD, content);
}
if (user != null) {
builder.field(USER, user);
Expand All @@ -98,6 +99,7 @@ public static MLModel parse(XContentParser parser) throws IOException {
FunctionName algorithm = null;
Integer version = null;
String content = null;
String oldContent = null;
User user = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Expand All @@ -106,19 +108,22 @@ public static MLModel parse(XContentParser parser) throws IOException {
parser.nextToken();

switch (fieldName) {
case MODEL_NAME:
case MODEL_NAME_FIELD:
name = parser.text();
break;
case MODEL_CONTENT:
case MODEL_CONTENT_FIELD:
content = parser.text();
break;
case MODEL_VERSION:
case OLD_MODEL_CONTENT_FIELD:
oldContent = parser.text();
break;
case MODEL_VERSION_FIELD:
version = parser.intValue(false);
break;
case USER:
user = User.parse(parser);
break;
case ALGORITHM:
case ALGORITHM_FIELD:
algorithm = FunctionName.from(parser.text());
break;
default:
Expand All @@ -130,7 +135,7 @@ public static MLModel parse(XContentParser parser) throws IOException {
.name(name)
.algorithm(algorithm)
.version(version)
.content(content)
.content(content == null ? oldContent : content)
.user(user)
.build();
}
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/java/org/opensearch/ml/common/MLTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.Instant;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.CommonValue.USER;

@Getter
@EqualsAndHashCode
Expand All @@ -39,7 +40,6 @@ public class MLTask implements ToXContentObject, Writeable {
public static final String CREATE_TIME_FIELD = "create_time";
public static final String LAST_UPDATE_TIME_FIELD = "last_update_time";
public static final String ERROR_FIELD = "error";
public static final String USER_FIELD = "user";
public static final String IS_ASYNC_TASK_FIELD = "is_async";

@Setter
Expand Down Expand Up @@ -177,7 +177,7 @@ public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params
builder.field(ERROR_FIELD, error);
}
if (user != null) {
builder.field(USER_FIELD, user);
builder.field(USER, user);
}
builder.field(IS_ASYNC_TASK_FIELD, async);
return builder.endObject();
Expand Down Expand Up @@ -246,7 +246,7 @@ public static MLTask parse(XContentParser parser) throws IOException {
case ERROR_FIELD:
error = parser.text();
break;
case USER_FIELD:
case USER:
user = User.parse(parser);
break;
case IS_ASYNC_TASK_FIELD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void toXContent() throws IOException {
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
mlModel.toXContent(builder, EMPTY_PARAMS);
String mlModelContent = TestHelper.xContentBuilderToString(builder);
assertEquals("{\"name\":\"model_name\",\"algorithm\":\"KMEANS\",\"version\":1,\"content\":\"test_content\"}", mlModelContent);
assertEquals("{\"name\":\"model_name\",\"algorithm\":\"KMEANS\",\"version\":1,\"model_content\":\"test_content\"}", mlModelContent);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.transport.model;

import org.junit.Before;
Expand Down Expand Up @@ -56,7 +61,7 @@ public void toXContentTest() throws IOException {
assertEquals("{\"name\":\"model\"," +
"\"algorithm\":\"KMEANS\"," +
"\"version\":1," +
"\"content\":\"content\"," +
"\"model_content\":\"content\"," +
"\"user\":{\"name\":\"\",\"backend_roles\":[],\"roles\":[],\"custom_attribute_names\":[],\"user_requested_tenant\":null}}", jsonStr);
}
}
3 changes: 2 additions & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ jacocoTestReport {
List<String> jacocoExclusions = [
// TODO: add more unit test to meet the minimal test coverage.
'org.opensearch.ml.constant.CommonValue',
'org.opensearch.ml.plugin.MachineLearningPlugin*'
'org.opensearch.ml.plugin.MachineLearningPlugin*',
'org.opensearch.ml.indices.MLIndicesHandler'
]

jacocoTestCoverageVerification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.ml.action.models;

import static org.opensearch.ml.indices.MLIndicesHandler.ML_MODEL_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.action.models;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.indices.MLIndicesHandler.ML_MODEL_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX;
import static org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry;

import lombok.AccessLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.ml.action.tasks;

import static org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX;

import lombok.extern.log4j.Log4j2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package org.opensearch.ml.action.tasks;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.indices.MLIndicesHandler.ML_TASK_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX;
import static org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry;

import lombok.extern.log4j.Log4j2;
Expand Down
47 changes: 47 additions & 0 deletions plugin/src/main/java/org/opensearch/ml/indices/MLIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.indices;

import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX_MAPPING;
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX_SCHEMA_VERSION;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX_MAPPING;
import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX_SCHEMA_VERSION;

public enum MLIndex {
MODEL(ML_MODEL_INDEX, false, ML_MODEL_INDEX_MAPPING, ML_MODEL_INDEX_SCHEMA_VERSION),
TASK(ML_TASK_INDEX, false, ML_TASK_INDEX_MAPPING, ML_TASK_INDEX_SCHEMA_VERSION);

private final String indexName;
// whether we use an alias for the index
private final boolean alias;
private final String mapping;
private final Integer version;

MLIndex(String name, boolean alias, String mapping, Integer version) {
this.indexName = name;
this.alias = alias;
this.mapping = mapping;
this.version = version;
}

public String getIndexName() {
return indexName;
}

public boolean isAlias() {
return alias;
}

public String getMapping() {
return mapping;
}

public Integer getVersion() {
return version;
}
}
Loading