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;
28 * This class represents a simple range query.
30 * <p>A range query is composed of one or more operator/value pairs which define
31 * the upper and lower bounds of the range, and a field to apply the query to.
33 * <p>Operators may be one of the following:
35 * <li>gt - Greater than. </li>
36 * <li>gte - Greater than or equal to. </li>
37 * <li>lt - Less than. </li>
38 * <li>lte - Less than or equal to. </li>
40 * Values may be either numeric values (Integer or Double) or Strings representing
43 * <p>The following examples illustrate a couple of variants of the range query:
46 * // A simple numeric range query:
49 * "field": "fieldname",
55 * // A simple date range query:
58 * "field": "fieldname",
59 * "gt": "2016-10-06T00:00:00.558+03:00",
60 * "lt": "2016-10-06T23:59:59.558+03:00"
65 public class RangeQuery {
68 * The name of the field to apply the range query against.
73 * The value of the field must be greater than this value to be a match.<br>
74 * NOTE: Only one of 'gt' or 'gte' should be set on any single {@link RangeQuery}
80 * The value of the field must be greater than or equal to this value to be a match.<br>
81 * NOTE: Only one of 'gt' or 'gte' should be set on any single {@link RangeQuery}
87 * The value of the field must be less than this value to be a match.<br>
88 * NOTE: Only one of 'lt' or 'lte' should be set on any single {@link RangeQuery}
94 * The value of the field must be less than or equal to than this value to be a match.<br>
95 * NOTE: Only one of 'lt' or 'lte' should be set on any single {@link RangeQuery}
100 private String format;
102 @JsonProperty("time-zone")
103 private String timeZone;
105 public String getField() {
109 public void setField(String field) {
113 public Object getGt() {
117 public void setGt(Object gt) {
119 // It does not make sense to assign a value to both the 'greater than'
120 // and 'greater than or equal' operations, so make sure we are not
121 // trying to do that.
124 // Make sure that we are not trying to mix both numeric and date
125 // type values in the same queries.
126 if (((lt != null) && !typesMatch(gt, lt))
127 || ((lte != null) && !typesMatch(gt, lte))) {
128 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
131 // If we made it here, then we're all good. Store the value.
134 throw new IllegalArgumentException("Cannot assign both 'gt' and 'gte' fields in the same ranged query");
139 public Object getGte() {
143 public void setGte(Object gte) {
145 // It does not make sense to assign a value to both the 'greater than'
146 // and 'greater than or equal' operations, so make sure we are not
147 // trying to do that.
150 // Make sure that we are not trying to mix both numeric and date
151 // type values in the same queries.
152 if (((lt != null) && !typesMatch(gte, lt))
153 || ((lte != null) && !typesMatch(gte, lte))) {
154 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
157 // If we made it here, then we're all good. Store the value.
161 throw new IllegalArgumentException("Cannot assign both 'gt' and 'gte' fields in the same ranged query");
165 public Object getLt() {
169 public void setLt(Object lt) {
171 // It does not make sense to assign a value to both the 'less than'
172 // and 'less than or equal' operations, so make sure we are not
173 // trying to do that.
176 // Make sure that we are not trying to mix both numeric and date
177 // type values in the same queries.
178 if (((gt != null) && !typesMatch(lt, gt))
179 || ((gte != null) && !typesMatch(lt, gte))) {
180 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
183 // If we made it here, then we're all good. Store the value.
187 throw new IllegalArgumentException("Cannot assign both 'lt' and 'lte' fields in the same ranged query");
191 public Object getLte() {
195 public void setLte(Object lte) {
197 // It does not make sense to assign a value to both the 'greater than'
198 // and 'greater than or equal' operations, so make sure we are not
199 // trying to do that.
202 // Make sure that we are not trying to mix both numeric and date
203 // type values in the same queries.
204 if (((gt != null) && !typesMatch(lte, gt))
205 || ((gte != null) && !typesMatch(lte, gte))) {
206 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
209 // If we made it here, then we're all good. Store the value.
213 throw new IllegalArgumentException("Cannot assign both 'lt' and 'lte' fields in the same ranged query");
217 public String getFormat() {
221 public void setFormat(String format) {
222 this.format = format;
225 public String getTimeZone() {
229 public void setTimeZone(String timeZone) {
230 this.timeZone = timeZone;
234 * This convenience method determines whether or not the supplied
235 * value needs to be enclosed in '"' characters when generating
236 * ElasticSearch compatible syntax.
238 * @param val - The value to check.
239 * @return - A string representation of the value for inclusion
240 * in an ElasticSearch syntax string.
242 private String formatStringOrNumericVal(Object val) {
244 if (val instanceof String) {
245 return "\"" + val.toString() + "\"";
247 return val.toString();
253 * This convenience method verifies that the supplied objects are
254 * of classes considered to be compatible for a ranged query.
256 * @param value1 - The first value to check.
257 * @param value2 - The second value to check.
258 * @return - True if the two objects are compatible for inclusion in the
259 * same ranged query, False, otherwise.
261 boolean typesMatch(Object value1, Object value2) {
263 return ((value1 instanceof String) && (value2 instanceof String))
264 || (!(value1 instanceof String) && !(value2 instanceof String));
269 * This method returns a string which represents this query in syntax
270 * that is understandable by ElasticSearch and is suitable for inclusion
271 * in an ElasticSearch query string.
273 * @return - ElasticSearch syntax string.
275 public String toElasticSearch() {
277 StringBuilder sb = new StringBuilder();
280 sb.append("\"range\": {");
281 sb.append("\"").append(field).append("\": {");
283 // We may have one or zero of 'greater than' or 'greater
285 boolean needComma = false;
287 sb.append("\"gte\": ").append(formatStringOrNumericVal(gte));
289 } else if (gt != null) {
290 sb.append("\"gt\": ").append(formatStringOrNumericVal(gt));
294 // We may have one or zero of 'less than' or 'less
300 sb.append("\"lte\": ").append(formatStringOrNumericVal(lte));
301 } else if (lt != null) {
305 sb.append("\"lt\": ").append(formatStringOrNumericVal(lt));
308 // Append the format specifier if one was provided.
309 if (format != null) {
310 sb.append(", \"format\": \"").append(format).append("\"");
313 // Append the time zone specifier if one was provided.
314 if (timeZone != null) {
315 sb.append(", \"time_zone\": \"").append(timeZone).append("\"");
322 return sb.toString();
326 public String toString() {
328 String str = "{ field: " + field + ", ";
332 } else if (gte != null) {
333 str += "gte: " + gte;
337 str += (((gt != null) || (gte != null)) ? ", " : "") + "lt: " + lt;
338 } else if (lte != null) {
339 str += (((gt != null) || (gte != null)) ? ", " : "") + "lte: " + lte;