2 * ============LICENSE_START=======================================================
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
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 package org.onap.aai.sa.searchdbabstraction.searchapi;
23 import com.fasterxml.jackson.annotation.JsonProperty;
25 import java.util.Arrays;
27 public class AggregationStatement {
29 @JsonProperty("group-by")
30 private GroupByAggregation groupBy;
32 @JsonProperty("date-range")
33 private DateRangeAggregation dateRange;
35 @JsonProperty("date-histogram")
36 private DateHistogramAggregation dateHist;
38 @JsonProperty("nested")
39 private Aggregation[] nested;
41 @JsonProperty("sub-aggregations")
42 private Aggregation[] subAggregations;
44 public GroupByAggregation getGroupBy() {
48 public void setGroupBy(GroupByAggregation groupBy) {
49 this.groupBy = groupBy;
52 public DateRangeAggregation getDateRange() {
56 public void setDateRange(DateRangeAggregation dateRange) {
57 this.dateRange = dateRange;
60 public DateHistogramAggregation getDateHist() {
64 public void setDateHist(DateHistogramAggregation dateHist) {
65 this.dateHist = dateHist;
68 public Aggregation[] getNested() {
72 public void setNested(Aggregation[] nested) {
76 public Aggregation[] getSubAggregations() {
77 return subAggregations;
80 public void setSubAggregations(Aggregation[] subAggregations) {
81 this.subAggregations = subAggregations;
84 public String toElasticSearch() {
85 StringBuffer sb = new StringBuffer();
89 if (nested != null && nested.length > 0) {
90 sb.append("\"nested\": {\"path\": \"");
91 if (nested[0].getStatement() != null) {
92 sb.append(nested[0].getStatement().getNestedPath());
94 sb.append("\"}, \"aggs\": {");
95 for (int i = 0; i < nested.length; i++) {
99 sb.append(nested[i].toElasticSearch());
104 if (groupBy != null) {
105 sb.append(groupBy.toElasticSearch());
106 } else if (dateRange != null) {
107 sb.append(dateRange.toElasticSearch());
108 } else if (dateHist != null) {
109 sb.append(dateHist.toElasticSearch());
112 if (subAggregations != null && subAggregations.length > 0) {
113 sb.append(", \"aggs\": {");
114 for (int i = 0; i < subAggregations.length; i++) {
118 sb.append(subAggregations[i].toElasticSearch());
126 return sb.toString();
130 public String toString() {
131 StringBuffer sb = new StringBuffer();
133 if (nested != null) {
134 sb.append("{nested: ");
135 sb.append(Arrays.toString(nested));
136 } else if (groupBy != null) {
137 sb.append(groupBy.toString());
138 } else if (dateHist != null) {
139 sb.append(dateHist.toString());
140 } else if (dateRange != null) {
141 sb.append(dateRange.toString());
144 if (subAggregations != null) {
145 sb.append(", sub-aggregations: ");
146 sb.append(Arrays.toString(subAggregations));
151 return sb.toString();
154 public String getNestedPath() {
156 String fieldName = null;
158 if (groupBy != null) {
159 fieldName = groupBy.getField();
160 } else if (dateRange != null) {
161 fieldName = dateRange.getField();
162 } else if (dateHist != null) {
163 fieldName = dateHist.getField();
166 if (fieldName != null && fieldName.contains(".")) {
167 // we have nested field
168 path = fieldName.substring(0, fieldName.indexOf("."));