Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / searchapi / DateRangeAggregation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.searchapi;
26
27 import com.fasterxml.jackson.annotation.JsonProperty;
28
29 /**
30  * An example of a date_range aggregation:
31  *
32  * <p><pre>
33  * {
34  *    "aggs": {
35  *        "range": {
36  *            "date_range": {
37  *                "field": "date",
38  *                "format": "MM-yyy",
39  *                "ranges": [
40  *                    { "to": "now-10M/M" },
41  *                    { "from": "now-10M/M" }
42  *                ]
43  *            }
44  *        }
45  *    }
46  * }
47  * </pre>
48  *
49  * @author sye
50  */
51 public class DateRangeAggregation extends AbstractAggregation {
52
53
54   private String format;
55
56   @JsonProperty("ranges")
57   private DateRange[] dateRanges;
58
59
60   public String getFormat() {
61     return format;
62   }
63
64   public void setFormat(String format) {
65     this.format = format;
66   }
67
68   public DateRange[] getDateRanges() {
69     return dateRanges;
70   }
71
72   public void setDateRanges(DateRange[] dateRanges) {
73     this.dateRanges = dateRanges;
74   }
75
76   @Override
77   public String toElasticSearch() {
78     StringBuilder sb = new StringBuilder();
79
80     sb.append("\"date_range\": {\"field\": \"");
81     sb.append(field);
82     sb.append("\"");
83
84     if (format != null) {
85       sb.append(", \"format\": \"");
86       sb.append(format);
87       sb.append("\"");
88     }
89
90     if (dateRanges != null && dateRanges.length > 0) {
91       sb.append(", \"ranges\": [");
92
93       for (int i = 0; i < dateRanges.length; i++) {
94         if (i > 0) {
95           sb.append(",");
96         }
97         sb.append(dateRanges[i].toElasticSearch());
98       }
99
100       sb.append("]");
101     }
102
103     if (size != null) {
104       sb.append(", \"size\": ");
105       sb.append(size);
106     }
107
108     if (minThreshold != null) {
109       sb.append(", \"min_doc_count\": ").append(minThreshold);
110     }
111
112     sb.append("}");
113
114     return sb.toString();
115   }
116
117   @Override
118   public String toString() {
119     StringBuffer sb = new StringBuffer();
120     sb.append("date-range: {field: " + field + ", format: " + format + ", size: " + size
121         + ", minThreshold: " + minThreshold + "ranges: [");
122     for (int i = 0; i < dateRanges.length; i++) {
123       if (i > 0) {
124         sb.append(",");
125       }
126       sb.append(dateRanges[i].toString());
127     }
128     sb.append("]");
129
130     return sb.toString();
131   }
132
133 }