|
10 | 10 |
|
11 | 11 | import java.io.IOException;
|
12 | 12 | import java.util.Objects;
|
| 13 | +import org.opensearch.core.common.ParsingException; |
13 | 14 | import org.opensearch.core.common.io.stream.StreamInput;
|
14 | 15 | import org.opensearch.core.common.io.stream.StreamOutput;
|
15 | 16 | import org.opensearch.core.common.io.stream.Writeable;
|
16 | 17 | import org.opensearch.core.xcontent.ToXContent;
|
17 | 18 | import org.opensearch.core.xcontent.ToXContentObject;
|
18 | 19 | import org.opensearch.core.xcontent.XContentBuilder;
|
| 20 | +import org.opensearch.core.xcontent.XContentParser; |
19 | 21 |
|
20 | 22 | /**
|
21 | 23 | * Measurement that is stored in the SearchQueryRecord. Measurement can be of a specific AggregationType
|
22 | 24 | */
|
23 | 25 | public class Measurement implements ToXContentObject, Writeable {
|
24 | 26 | private static int DEFAULT_COUNT = 1;
|
| 27 | + |
| 28 | + private static final String NUMBER = "number"; |
| 29 | + private static final String COUNT = "count"; |
| 30 | + private static final String AGGREGATION_TYPE = "aggregationType"; |
| 31 | + |
25 | 32 | private AggregationType aggregationType;
|
26 | 33 | private Number number;
|
27 | 34 | private int count;
|
@@ -55,6 +62,21 @@ public Measurement(Number number) {
|
55 | 62 | this(number, DEFAULT_COUNT, AggregationType.DEFAULT_AGGREGATION_TYPE);
|
56 | 63 | }
|
57 | 64 |
|
| 65 | + private Measurement() {} |
| 66 | + |
| 67 | + /** |
| 68 | + * Construct a measurement from {@link XContentParser} |
| 69 | + * |
| 70 | + * @param parser {@link XContentParser} |
| 71 | + * @return {@link Measurement} |
| 72 | + * @throws IOException IOException |
| 73 | + */ |
| 74 | + public static Measurement fromXContent(XContentParser parser) throws IOException { |
| 75 | + Measurement builder = new Measurement(); |
| 76 | + builder.parseXContent(parser); |
| 77 | + return builder; |
| 78 | + } |
| 79 | + |
58 | 80 | /**
|
59 | 81 | * Add measurement number to the current number based on the aggregationType.
|
60 | 82 | * If aggregateType is NONE, replace the number since we are not aggregating in this case.
|
@@ -150,13 +172,45 @@ public void setAggregationType(AggregationType aggregationType) {
|
150 | 172 | @Override
|
151 | 173 | public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
|
152 | 174 | builder.startObject();
|
153 |
| - builder.field("number", number); |
154 |
| - builder.field("count", count); |
155 |
| - builder.field("aggregationType", aggregationType.toString()); |
| 175 | + builder.field(NUMBER, number); |
| 176 | + builder.field(COUNT, count); |
| 177 | + builder.field(AGGREGATION_TYPE, aggregationType.toString()); |
156 | 178 | builder.endObject();
|
157 | 179 | return builder;
|
158 | 180 | }
|
159 | 181 |
|
| 182 | + /** |
| 183 | + * Parse a measurement from {@link XContentParser} |
| 184 | + * |
| 185 | + * @param parser {@link XContentParser} |
| 186 | + * @throws IOException IOException |
| 187 | + */ |
| 188 | + private void parseXContent(XContentParser parser) throws IOException { |
| 189 | + XContentParser.Token token = parser.currentToken(); |
| 190 | + if (token != XContentParser.Token.START_OBJECT) { |
| 191 | + throw new ParsingException( |
| 192 | + parser.getTokenLocation(), |
| 193 | + "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", |
| 194 | + parser.getTokenLocation() |
| 195 | + ); |
| 196 | + } else { |
| 197 | + String currentFieldName = null; |
| 198 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 199 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 200 | + currentFieldName = parser.currentName(); |
| 201 | + } else if (token.isValue()) { |
| 202 | + if (NUMBER.equals(currentFieldName)) { |
| 203 | + this.number = parser.numberValue(); |
| 204 | + } else if (COUNT.equals(currentFieldName)) { |
| 205 | + this.count = parser.intValue(); |
| 206 | + } else if (AGGREGATION_TYPE.equals(currentFieldName)) { |
| 207 | + this.aggregationType = AggregationType.valueOf(parser.text()); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
160 | 214 | @Override
|
161 | 215 | public void writeTo(StreamOutput out) throws IOException {
|
162 | 216 | writeNumber(out, number);
|
|
0 commit comments