2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sa.searchdbabstraction.searchapi;
27 import com.fasterxml.jackson.annotation.JsonProperty;
29 import java.util.Arrays;
31 public class AggregationStatement {
33 @JsonProperty("group-by")
34 private GroupByAggregation groupBy;
36 @JsonProperty("date-range")
37 private DateRangeAggregation dateRange;
39 @JsonProperty("date-histogram")
40 private DateHistogramAggregation dateHist;
42 @JsonProperty("nested")
43 private Aggregation[] nested;
45 @JsonProperty("sub-aggregations")
46 private Aggregation[] subAggregations;
48 public GroupByAggregation getGroupBy() {
52 public void setGroupBy(GroupByAggregation groupBy) {
53 this.groupBy = groupBy;
56 public DateRangeAggregation getDateRange() {
60 public void setDateRange(DateRangeAggregation dateRange) {
61 this.dateRange = dateRange;
64 public DateHistogramAggregation getDateHist() {
68 public void setDateHist(DateHistogramAggregation dateHist) {
69 this.dateHist = dateHist;
72 public Aggregation[] getNested() {
76 public void setNested(Aggregation[] nested) {
80 public Aggregation[] getSubAggregations() {
81 return subAggregations;
84 public void setSubAggregations(Aggregation[] subAggregations) {
85 this.subAggregations = subAggregations;
88 public String toElasticSearch() {
89 StringBuffer sb = new StringBuffer();
93 if (nested != null && nested.length > 0) {
94 sb.append("\"nested\": {\"path\": \"");
95 if (nested[0].getStatement() != null) {
96 sb.append(nested[0].getStatement().getNestedPath());
98 sb.append("\"}, \"aggs\": {");
99 for (int i = 0; i < nested.length; i++) {
103 sb.append(nested[i].toElasticSearch());
108 if (groupBy != null) {
109 sb.append(groupBy.toElasticSearch());
110 } else if (dateRange != null) {
111 sb.append(dateRange.toElasticSearch());
112 } else if (dateHist != null) {
113 sb.append(dateHist.toElasticSearch());
116 if (subAggregations != null && subAggregations.length > 0) {
117 sb.append(", \"aggs\": {");
118 for (int i = 0; i < subAggregations.length; i++) {
122 sb.append(subAggregations[i].toElasticSearch());
130 return sb.toString();
134 public String toString() {
135 StringBuffer sb = new StringBuffer();
137 if (nested != null) {
138 sb.append("{nested: ");
139 sb.append(Arrays.toString(nested));
140 } else if (groupBy != null) {
141 sb.append(groupBy.toString());
142 } else if (dateHist != null) {
143 sb.append(dateHist.toString());
144 } else if (dateRange != null) {
145 sb.append(dateRange.toString());
148 if (subAggregations != null) {
149 sb.append(", sub-aggregations: ");
150 sb.append(Arrays.toString(subAggregations));
155 return sb.toString();
158 public String getNestedPath() {
160 String fieldName = null;
162 if (groupBy != null) {
163 fieldName = groupBy.getField();
164 } else if (dateRange != null) {
165 fieldName = dateRange.getField();
166 } else if (dateHist != null) {
167 fieldName = dateHist.getField();
170 if (fieldName != null && fieldName.contains(".")) {
171 // we have nested field
172 path = fieldName.substring(0, fieldName.indexOf("."));