55 */
66package org .elasticsearch .xpack .core .ml .job .results ;
77
8+ import org .elasticsearch .Version ;
89import org .elasticsearch .common .ParseField ;
910import org .elasticsearch .common .io .stream .StreamInput ;
1011import org .elasticsearch .common .io .stream .StreamOutput ;
@@ -34,6 +35,7 @@ public class CategoryDefinition implements ToXContentObject, Writeable {
3435 public static final ParseField REGEX = new ParseField ("regex" );
3536 public static final ParseField MAX_MATCHING_LENGTH = new ParseField ("max_matching_length" );
3637 public static final ParseField EXAMPLES = new ParseField ("examples" );
38+ public static final ParseField GROK_PATTERN = new ParseField ("grok_pattern" );
3739
3840 // Used for QueryPage
3941 public static final ParseField RESULTS_FIELD = new ParseField ("categories" );
@@ -51,6 +53,7 @@ private static ConstructingObjectParser<CategoryDefinition, Void> createParser(b
5153 parser .declareString (CategoryDefinition ::setRegex , REGEX );
5254 parser .declareLong (CategoryDefinition ::setMaxMatchingLength , MAX_MATCHING_LENGTH );
5355 parser .declareStringArray (CategoryDefinition ::setExamples , EXAMPLES );
56+ parser .declareString (CategoryDefinition ::setGrokPattern , GROK_PATTERN );
5457
5558 return parser ;
5659 }
@@ -61,6 +64,7 @@ private static ConstructingObjectParser<CategoryDefinition, Void> createParser(b
6164 private String regex = "" ;
6265 private long maxMatchingLength = 0L ;
6366 private final Set <String > examples ;
67+ private String grokPattern ;
6468
6569 public CategoryDefinition (String jobId ) {
6670 this .jobId = jobId ;
@@ -74,6 +78,9 @@ public CategoryDefinition(StreamInput in) throws IOException {
7478 regex = in .readString ();
7579 maxMatchingLength = in .readLong ();
7680 examples = new TreeSet <>(in .readList (StreamInput ::readString ));
81+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
82+ grokPattern = in .readOptionalString ();
83+ }
7784 }
7885
7986 @ Override
@@ -84,6 +91,9 @@ public void writeTo(StreamOutput out) throws IOException {
8491 out .writeString (regex );
8592 out .writeLong (maxMatchingLength );
8693 out .writeStringList (new ArrayList <>(examples ));
94+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
95+ out .writeOptionalString (grokPattern );
96+ }
8797 }
8898
8999 public String getJobId () {
@@ -139,6 +149,14 @@ public void addExample(String example) {
139149 examples .add (example );
140150 }
141151
152+ public String getGrokPattern () {
153+ return grokPattern ;
154+ }
155+
156+ public void setGrokPattern (String grokPattern ) {
157+ this .grokPattern = grokPattern ;
158+ }
159+
142160 @ Override
143161 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
144162 builder .startObject ();
@@ -148,6 +166,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
148166 builder .field (REGEX .getPreferredName (), regex );
149167 builder .field (MAX_MATCHING_LENGTH .getPreferredName (), maxMatchingLength );
150168 builder .field (EXAMPLES .getPreferredName (), examples );
169+ if (grokPattern != null ) {
170+ builder .field (GROK_PATTERN .getPreferredName (), grokPattern );
171+ }
151172 builder .endObject ();
152173 return builder ;
153174 }
@@ -166,11 +187,12 @@ public boolean equals(Object other) {
166187 && Objects .equals (this .terms , that .terms )
167188 && Objects .equals (this .regex , that .regex )
168189 && Objects .equals (this .maxMatchingLength , that .maxMatchingLength )
169- && Objects .equals (this .examples , that .examples );
190+ && Objects .equals (this .examples , that .examples )
191+ && Objects .equals (this .grokPattern , that .grokPattern );
170192 }
171193
172194 @ Override
173195 public int hashCode () {
174- return Objects .hash (jobId , categoryId , terms , regex , maxMatchingLength , examples );
196+ return Objects .hash (jobId , categoryId , terms , regex , maxMatchingLength , examples , grokPattern );
175197 }
176198}
0 commit comments