2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sa.searchdbabstraction.searchapi;
25 import com.fasterxml.jackson.annotation.JsonProperty;
27 import java.util.Arrays;
29 public class AggregationStatement {
31 @JsonProperty("group-by")
32 private GroupByAggregation groupBy;
34 @JsonProperty("date-range")
35 private DateRangeAggregation dateRange;
37 @JsonProperty("date-histogram")
38 private DateHistogramAggregation dateHist;
40 @JsonProperty("nested")
41 private Aggregation[] nested;
43 @JsonProperty("sub-aggregations")
44 private Aggregation[] subAggregations;
46 public GroupByAggregation getGroupBy() {
50 public void setGroupBy(GroupByAggregation groupBy) {
51 this.groupBy = groupBy;
54 public DateRangeAggregation getDateRange() {
58 public void setDateRange(DateRangeAggregation dateRange) {
59 this.dateRange = dateRange;
62 public DateHistogramAggregation getDateHist() {
66 public void setDateHist(DateHistogramAggregation dateHist) {
67 this.dateHist = dateHist;
70 public Aggregation[] getNested() {
74 public void setNested(Aggregation[] nested) {
78 public Aggregation[] getSubAggregations() {
79 return subAggregations;
82 public void setSubAggregations(Aggregation[] subAggregations) {
83 this.subAggregations = subAggregations;
86 public String toElasticSearch() {
87 StringBuffer sb = new StringBuffer();
91 if (nested != null && nested.length > 0) {
92 sb.append("\"nested\": {\"path\": \"");
93 if (nested[0].getStatement() != null) {
94 sb.append(nested[0].getStatement().getNestedPath());
96 sb.append("\"}, \"aggs\": {");
97 for (int i = 0; i < nested.length; i++) {
101 sb.append(nested[i].toElasticSearch());
106 if (groupBy != null) {
107 sb.append(groupBy.toElasticSearch());
108 } else if (dateRange != null) {
109 sb.append(dateRange.toElasticSearch());
110 } else if (dateHist != null) {
111 sb.append(dateHist.toElasticSearch());
114 if (subAggregations != null && subAggregations.length > 0) {
115 sb.append(", \"aggs\": {");
116 for (int i = 0; i < subAggregations.length; i++) {
120 sb.append(subAggregations[i].toElasticSearch());
128 return sb.toString();
132 public String toString() {
133 StringBuffer sb = new StringBuffer();
135 if (nested != null) {
136 sb.append("{nested: ");
137 sb.append(Arrays.toString(nested));
138 } else if (groupBy != null) {
139 sb.append(groupBy.toString());
140 } else if (dateHist != null) {
141 sb.append(dateHist.toString());
142 } else if (dateRange != null) {
143 sb.append(dateRange.toString());
146 if (subAggregations != null) {
147 sb.append(", sub-aggregations: ");
148 sb.append(Arrays.toString(subAggregations));
153 return sb.toString();
156 public String getNestedPath() {
158 String fieldName = null;
160 if (groupBy != null) {
161 fieldName = groupBy.getField();
162 } else if (dateRange != null) {
163 fieldName = dateRange.getField();
164 } else if (dateHist != null) {
165 fieldName = dateHist.getField();
168 if (fieldName != null && fieldName.contains(".")) {
169 // we have nested field
170 path = fieldName.substring(0, fieldName.indexOf("."));