20
20
package org .elasticsearch .client .dataframe .transforms .pivot ;
21
21
22
22
import org .elasticsearch .common .ParsingException ;
23
+ import org .elasticsearch .common .Strings ;
23
24
import org .elasticsearch .common .xcontent .ToXContentObject ;
24
25
import org .elasticsearch .common .xcontent .XContentBuilder ;
25
26
import org .elasticsearch .common .xcontent .XContentParser ;
26
27
27
28
import java .io .IOException ;
28
29
import java .util .LinkedHashMap ;
29
- import java .util .Locale ;
30
30
import java .util .Map ;
31
31
import java .util .Objects ;
32
32
@@ -51,9 +51,7 @@ public static GroupConfig fromXContent(final XContentParser parser) throws IOExc
51
51
52
52
// be parsing friendly, whether the token needs to be advanced or not (similar to what ObjectParser does)
53
53
XContentParser .Token token ;
54
- if (parser .currentToken () == XContentParser .Token .START_OBJECT ) {
55
- parser .currentToken ();
56
- } else {
54
+ if (parser .currentToken () != XContentParser .Token .START_OBJECT ) {
57
55
token = parser .nextToken ();
58
56
if (token != XContentParser .Token .START_OBJECT ) {
59
57
throw new ParsingException (parser .getTokenLocation (), "Failed to parse object: Expected START_OBJECT but was: " + token );
@@ -70,37 +68,64 @@ public static GroupConfig fromXContent(final XContentParser parser) throws IOExc
70
68
}
71
69
continue ;
72
70
}
73
- String destinationFieldName = parser .currentName ();
74
71
72
+ String destinationFieldName = parser .currentName ();
75
73
ensureExpectedToken (XContentParser .Token .START_OBJECT , token , parser ::getTokenLocation );
76
74
token = parser .nextToken ();
77
75
ensureExpectedToken (XContentParser .Token .FIELD_NAME , token , parser ::getTokenLocation );
78
- SingleGroupSource . Type groupType = SingleGroupSource . Type . valueOf ( parser .currentName (). toUpperCase ( Locale . ROOT ) );
76
+ String groupType = parser .currentName ();
79
77
80
78
token = parser .nextToken ();
81
- ensureExpectedToken (XContentParser .Token .START_OBJECT , token , parser ::getTokenLocation );
82
- SingleGroupSource groupSource ;
79
+ if (token != XContentParser .Token .START_OBJECT ) {
80
+ // need to consume up to dest field end obj
81
+ consumeUntilEndObject (parser , 1 );
82
+ continue ;
83
+ }
84
+
85
+ SingleGroupSource groupSource = null ;
83
86
switch (groupType ) {
84
- case TERMS :
87
+ case "terms" :
85
88
groupSource = TermsGroupSource .fromXContent (parser );
86
89
break ;
87
- case HISTOGRAM :
90
+ case "histogram" :
88
91
groupSource = HistogramGroupSource .fromXContent (parser );
89
92
break ;
90
- case DATE_HISTOGRAM :
93
+ case "date_histogram" :
91
94
groupSource = DateHistogramGroupSource .fromXContent (parser );
92
95
break ;
93
96
default :
94
- throw new ParsingException (parser .getTokenLocation (), "invalid grouping type: " + groupType );
97
+ // not a valid group source. Consume up to the dest field end object
98
+ consumeUntilEndObject (parser , 2 );
95
99
}
96
- // destination field end_object
97
- parser .nextToken ();
98
100
99
- groups .put (destinationFieldName , groupSource );
101
+ if (groupSource != null ) {
102
+ groups .put (destinationFieldName , groupSource );
103
+ // destination field end_object
104
+ parser .nextToken ();
105
+ }
100
106
}
101
107
return new GroupConfig (groups );
102
108
}
103
109
110
+ /**
111
+ * Consume tokens from the parser until {@code endObjectCount} of end object
112
+ * tokens have been read. Nested objects that start and end inside the current
113
+ * field are skipped and do contribute to the end object count.
114
+ * @param parser The XContent parser
115
+ * @param endObjectCount Number of end object tokens to consume
116
+ * @throws IOException On parsing error
117
+ */
118
+ private static void consumeUntilEndObject (XContentParser parser , int endObjectCount ) throws IOException {
119
+ do {
120
+ XContentParser .Token token = parser .nextToken ();
121
+ if (token == XContentParser .Token .START_OBJECT ) {
122
+ endObjectCount ++;
123
+ } else if (token == XContentParser .Token .END_OBJECT ) {
124
+ endObjectCount --;
125
+ }
126
+ } while (endObjectCount != 0 );
127
+ }
128
+
104
129
public GroupConfig (Map <String , SingleGroupSource > groups ) {
105
130
this .groups = groups ;
106
131
}
@@ -144,4 +169,9 @@ public boolean equals(Object other) {
144
169
public int hashCode () {
145
170
return Objects .hash (groups );
146
171
}
172
+
173
+ @ Override
174
+ public String toString () {
175
+ return Strings .toString (this , true , true );
176
+ }
147
177
}
0 commit comments