1616import java .util .List ;
1717
1818import static org .elasticsearch .common .xcontent .XContentParserUtils .ensureExpectedToken ;
19+ import static org .elasticsearch .xpack .analytics .mapper .TDigestFieldMapper .CENTROIDS_NAME ;
20+ import static org .elasticsearch .xpack .analytics .mapper .TDigestFieldMapper .COUNTS_NAME ;
1921
2022public class TDigestParser {
2123
22- private static final ParseField COUNTS_FIELD = new ParseField ("counts" );
23- private static final ParseField VALUES_FIELD = new ParseField ("centroids" );
24+ private static final ParseField COUNTS_FIELD = new ParseField (COUNTS_NAME );
25+ private static final ParseField CENTROIDS_FIELD = new ParseField (CENTROIDS_NAME );
2426
2527 /**
26- * A parsed histogram field, can represent either a T-Digest or a HDR histogram.
27- * @param values the centroids, guaranteed to be distinct and in increasing order
28- * @param counts the counts, guaranteed to be non-negative and of the same length as values
28+ * A parsed histogram field, can represent either a T-Digest
29+ * @param centroids the centroids, guaranteed to be distinct and in increasing order
30+ * @param counts the counts, guaranteed to be non-negative and of the same length as the centroids array
2931 */
30- public record ParsedHistogram (List <Double > values , List <Long > counts ) {}
32+ public record ParsedHistogram (List <Double > centroids , List <Long > counts ) {}
3133
3234 /**
3335 * Parses an XContent object into a histogram.
34- * The parse is expected to point at the next token after {@link XContentParser.Token#START_OBJECT}.
36+ * The parser is expected to point at the next token after {@link XContentParser.Token#START_OBJECT}.
3537 *
3638 * @param mappedFieldName the name of the field being parsed, used for error messages
3739 * @param parser the parser to use
3840 * @return the parsed histogram
3941 */
4042 public static ParsedHistogram parse (String mappedFieldName , XContentParser parser ) throws IOException {
41- ArrayList <Double > values = null ;
43+ ArrayList <Double > centroids = null ;
4244 ArrayList <Long > counts = null ;
4345 XContentParser .Token token = parser .currentToken ();
4446 while (token != XContentParser .Token .END_OBJECT ) {
4547 // should be a field
4648 ensureExpectedToken (XContentParser .Token .FIELD_NAME , token , parser );
4749 String fieldName = parser .currentName ();
48- if (fieldName .equals (VALUES_FIELD .getPreferredName ())) {
50+ if (fieldName .equals (CENTROIDS_FIELD .getPreferredName ())) {
4951 token = parser .nextToken ();
5052 // should be an array
5153 ensureExpectedToken (XContentParser .Token .START_ARRAY , token , parser );
52- values = new ArrayList <>();
54+ centroids = new ArrayList <>();
5355 token = parser .nextToken ();
5456 double previousVal = -Double .MAX_VALUE ;
5557 while (token != XContentParser .Token .END_ARRAY ) {
5658 // should be a number
5759 ensureExpectedToken (XContentParser .Token .VALUE_NUMBER , token , parser );
5860 double val = parser .doubleValue ();
5961 if (val < previousVal ) {
60- // values must be in increasing order
62+ // centroids must be in increasing order
6163 throw new DocumentParsingException (
6264 parser .getTokenLocation (),
6365 "error parsing field ["
6466 + mappedFieldName
6567 + "], ["
66- + VALUES_FIELD
67- + "] values must be in increasing order, got ["
68+ + CENTROIDS_FIELD
69+ + "] centroids must be in increasing order, got ["
6870 + val
6971 + "] but previous value was ["
7072 + previousVal
7173 + "]"
7274 );
7375 }
74- values .add (val );
76+ centroids .add (val );
7577 previousVal = val ;
7678 token = parser .nextToken ();
7779 }
@@ -102,10 +104,10 @@ public static ParsedHistogram parse(String mappedFieldName, XContentParser parse
102104 }
103105 token = parser .nextToken ();
104106 }
105- if (values == null ) {
107+ if (centroids == null ) {
106108 throw new DocumentParsingException (
107109 parser .getTokenLocation (),
108- "error parsing field [" + mappedFieldName + "], expected field called [" + VALUES_FIELD .getPreferredName () + "]"
110+ "error parsing field [" + mappedFieldName + "], expected field called [" + CENTROIDS_FIELD .getPreferredName () + "]"
109111 );
110112 }
111113 if (counts == null ) {
@@ -114,24 +116,24 @@ public static ParsedHistogram parse(String mappedFieldName, XContentParser parse
114116 "error parsing field [" + mappedFieldName + "], expected field called [" + COUNTS_FIELD .getPreferredName () + "]"
115117 );
116118 }
117- if (values .size () != counts .size ()) {
119+ if (centroids .size () != counts .size ()) {
118120 throw new DocumentParsingException (
119121 parser .getTokenLocation (),
120122 "error parsing field ["
121123 + mappedFieldName
122124 + "], expected same length from ["
123- + VALUES_FIELD .getPreferredName ()
125+ + CENTROIDS_FIELD .getPreferredName ()
124126 + "] and "
125127 + "["
126128 + COUNTS_FIELD .getPreferredName ()
127129 + "] but got ["
128- + values .size ()
130+ + centroids .size ()
129131 + " != "
130132 + counts .size ()
131133 + "]"
132134 );
133135 }
134- return new ParsedHistogram (values , counts );
136+ return new ParsedHistogram (centroids , counts );
135137 }
136138
137139}
0 commit comments