2967fd2e06a1a7b7e68ce6214790cb1e5d7551ed
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / DateHistogramAggregation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.searchapi;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24
25 /**
26  * An example of elasticsearch date_histogram aggregation:
27  *
28  * <p><pre>
29  * {
30  *    "aggs": {
31  *        "my_group": {
32  *            "date_histogram" : {
33  *               "field" : "date",
34  *               "interval" : "month"
35  *           }
36  *        }
37  *    }
38  * }
39  * </pre>
40  */
41
42 public class DateHistogramAggregation extends AbstractAggregation {
43
44   private String interval;
45
46   private String format;
47
48   @JsonProperty("time-zone")
49   private String timeZone;
50
51
52   public String getInterval() {
53     return interval;
54   }
55
56   public void setInterval(String interval) {
57     this.interval = interval;
58   }
59
60   public String getTimeZone() {
61     return timeZone;
62   }
63
64   public String getFormat() {
65     return format;
66   }
67
68   public void setFormat(String format) {
69     this.format = format;
70   }
71
72   public void setTimeZone(String timeZone) {
73     this.timeZone = timeZone;
74   }
75
76   @Override
77   public String toElasticSearch() {
78     StringBuilder sb = new StringBuilder();
79
80     sb.append("\"date_histogram\": {\"field\": \"");
81     sb.append(field);
82     sb.append("\"");
83     if (interval != null) {
84       sb.append(", \"interval\": \"");
85       sb.append(interval);
86       sb.append("\"");
87     }
88     if (format != null) {
89       sb.append(", \"format\": \"");
90       sb.append(format);
91       sb.append("\"");
92     }
93     if (timeZone != null) {
94       sb.append(", \"time_zone\": \"");
95       sb.append(timeZone);
96       sb.append("\"");
97     }
98     if (size != null) {
99       sb.append(", \"size\": ");
100       sb.append(size);
101     }
102     if (minThreshold != null) {
103       sb.append(", \"min_doc_count\": ").append(minThreshold);
104     }
105     sb.append("}");
106
107     return sb.toString();
108   }
109
110   @Override
111   public String toString() {
112     return "DateHistogramAggregation: [field=" + field + ", interval=" + interval + ", format="
113         + format + ", timeZone=" + timeZone + ", size=" + size + " minThreshold=" + minThreshold;
114   }
115 }