Skip to content

[7.x] [ML] adding result_type and mlcategory fields to category definitions (#63326) #63412

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 3 commits into from
Oct 8, 2020
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 @@ -41,6 +41,7 @@ public class CategoryDefinition implements ToXContentObject, Writeable {
public static final ParseField GROK_PATTERN = new ParseField("grok_pattern");
public static final ParseField NUM_MATCHES = new ParseField("num_matches");
public static final ParseField PREFERRED_TO_CATEGORIES = new ParseField("preferred_to_categories");
public static final ParseField MLCATEGORY = new ParseField("mlcategory");

// Used for QueryPage
public static final ParseField RESULTS_FIELD = new ParseField("categories");
Expand All @@ -63,6 +64,8 @@ private static ConstructingObjectParser<CategoryDefinition, Void> createParser(b
parser.declareString(CategoryDefinition::setGrokPattern, GROK_PATTERN);
parser.declareLongArray(CategoryDefinition::setPreferredToCategories, PREFERRED_TO_CATEGORIES);
parser.declareLong(CategoryDefinition::setNumMatches, NUM_MATCHES);
parser.declareString((cd, rt) -> { /*Ignore as it is always category_definition*/ }, Result.RESULT_TYPE);
parser.declareString((cd, mc) -> { /*Ignore as it is always equal to category_id*/ }, MLCATEGORY);
return parser;
}

Expand Down Expand Up @@ -259,6 +262,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (partitionFieldName != null && partitionFieldValue != null && ReservedFieldNames.isValidFieldName(partitionFieldName)) {
builder.field(partitionFieldName, partitionFieldValue);
}
// Even though category_definitions now have a result type, queries need for category definition values
// still need to be done by looking for the category_id field. At least until 9.x
builder.field(Result.RESULT_TYPE.getPreferredName(), TYPE.getPreferredName());
builder.field(MLCATEGORY.getPreferredName(), String.valueOf(categoryId));

builder.endObject();
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@
"missing_field_count" : {
"type" : "long"
},
"mlcategory": {
"type": "keyword"
},
"model_bytes" : {
"type" : "long"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public void testResultsMappingReservedFields() throws Exception {
overridden.add(Quantiles.TYPE.getPreferredName());
overridden.add(TimingStats.TYPE.getPreferredName());
overridden.add(DatafeedTimingStats.TYPE.getPreferredName());
// This is a special case so that categorical job results can be paired easily with anomaly results
// This is acceptable as both mappings are keyword for the results documents and for category definitions
overridden.add(CategoryDefinition.MLCATEGORY.getPreferredName());

Set<String> expected = collectResultsDocFieldNames();
expected.removeAll(overridden);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ public void categoryDefinitions(String jobId, Long categoryId, String partitionF
if (categoryId != null) {
categoryIdQuery = QueryBuilders.termQuery(CategoryDefinition.CATEGORY_ID.getPreferredName(), categoryId);
} else if (from != null && size != null) {
// Note: Even though category definitions currently have a result_type field, this was not the case for older versions
// So, until at least 9.x, this existsQuery is still the preferred way to gather category definition objects
categoryIdQuery = QueryBuilders.existsQuery(CategoryDefinition.CATEGORY_ID.getPreferredName());
sourceBuilder.from(from).size(size)
.sort(new FieldSortBuilder(CategoryDefinition.CATEGORY_ID.getPreferredName()).order(SortOrder.ASC));
Expand Down