1+ /*
2+ * Licensed to Elasticsearch under one or more contributor
3+ * license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright
5+ * ownership. Elasticsearch licenses this file to you under
6+ * the Apache License, Version 2.0 (the "License"); you may
7+ * not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .elasticsearch .search .aggregations .metrics .valuecount ;
21+
22+ import org .elasticsearch .common .xcontent .ObjectParser ;
23+ import org .elasticsearch .common .xcontent .XContentBuilder ;
24+ import org .elasticsearch .common .xcontent .XContentParser ;
25+ import org .elasticsearch .search .aggregations .ParsedAggregation ;
26+
27+ import java .io .IOException ;
28+
29+ public class ParsedValueCount extends ParsedAggregation implements ValueCount {
30+
31+ private long valueCount ;
32+
33+ @ Override
34+ public double value () {
35+ return getValue ();
36+ }
37+
38+ @ Override
39+ public long getValue () {
40+ return valueCount ;
41+ }
42+
43+ @ Override
44+ public String getValueAsString () {
45+ // InternalValueCount doesn't print "value_as_string", but you can get a formatted value using
46+ // getValueAsString() using the raw formatter and converting the value to double
47+ return Double .toString (valueCount );
48+ }
49+
50+ @ Override
51+ protected String getType () {
52+ return ValueCountAggregationBuilder .NAME ;
53+ }
54+
55+ @ Override
56+ protected XContentBuilder doXContentBody (XContentBuilder builder , Params params ) throws IOException {
57+ builder .field (CommonFields .VALUE .getPreferredName (), valueCount );
58+ return builder ;
59+ }
60+
61+ private static final ObjectParser <ParsedValueCount , Void > PARSER = new ObjectParser <>(ParsedValueCount .class .getSimpleName (), true ,
62+ ParsedValueCount ::new );
63+
64+ static {
65+ declareAggregationFields (PARSER );
66+ PARSER .declareLong ((agg , value ) -> agg .valueCount = value , CommonFields .VALUE );
67+ }
68+
69+ public static ParsedValueCount fromXContent (XContentParser parser , final String name ) {
70+ ParsedValueCount sum = PARSER .apply (parser , null );
71+ sum .setName (name );
72+ return sum ;
73+ }
74+ }
0 commit comments