Update poms to conform to merge job requirements
[aai/search-data-service.git] / search-data-service-app / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / DateRangeAggregation.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 a date_range aggregation:
27  *
28  * <p>
29  *
30  * <pre>
31  * {
32  *    "aggs": {
33  *        "range": {
34  *            "date_range": {
35  *                "field": "date",
36  *                "format": "MM-yyy",
37  *                "ranges": [
38  *                    { "to": "now-10M/M" },
39  *                    { "from": "now-10M/M" }
40  *                ]
41  *            }
42  *        }
43  *    }
44  * }
45  * </pre>
46  *
47  * @author sye
48  */
49 public class DateRangeAggregation extends AbstractAggregation {
50
51     private String format;
52
53     @JsonProperty("ranges")
54     private DateRange[] dateRanges;
55
56
57     public String getFormat() {
58         return format;
59     }
60
61     public void setFormat(String format) {
62         this.format = format;
63     }
64
65     public DateRange[] getDateRanges() {
66         return dateRanges;
67     }
68
69     public void setDateRanges(DateRange[] dateRanges) {
70         this.dateRanges = dateRanges;
71     }
72
73     @Override
74     public String toElasticSearch() {
75         StringBuilder sb = new StringBuilder();
76
77         sb.append("\"date_range\": {\"field\": \"");
78         sb.append(field);
79         sb.append("\"");
80
81         if (format != null) {
82             sb.append(", \"format\": \"");
83             sb.append(format);
84             sb.append("\"");
85         }
86
87         if (dateRanges != null && dateRanges.length > 0) {
88             sb.append(", \"ranges\": [");
89
90             for (int i = 0; i < dateRanges.length; i++) {
91                 if (i > 0) {
92                     sb.append(",");
93                 }
94                 sb.append(dateRanges[i].toElasticSearch());
95             }
96
97             sb.append("]");
98         }
99
100         if (size != null) {
101             sb.append(", \"size\": ");
102             sb.append(size);
103         }
104
105         if (minThreshold != null) {
106             sb.append(", \"min_doc_count\": ").append(minThreshold);
107         }
108
109         sb.append("}");
110
111         return sb.toString();
112     }
113
114     @Override
115     public String toString() {
116         StringBuilder sb = new StringBuilder();
117         sb.append("date-range: {field: " + field + ", format: " + format + ", size: " + size + ", minThreshold: "
118                 + minThreshold + "ranges: [");
119         for (int i = 0; i < dateRanges.length; i++) {
120             if (i > 0) {
121                 sb.append(",");
122             }
123             sb.append(dateRanges[i].toString());
124         }
125         sb.append("]");
126
127         return sb.toString();
128     }
129
130 }