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;
30 * This class represents a simple range query.
32 * <p>A range query is composed of one or more operator/value pairs which define
33 * the upper and lower bounds of the range, and a field to apply the query to.
35 * <p>Operators may be one of the following:
37 * <li>gt - Greater than. </li>
38 * <li>gte - Greater than or equal to. </li>
39 * <li>lt - Less than. </li>
40 * <li>lte - Less than or equal to. </li>
42 * Values may be either numeric values (Integer or Double) or Strings representing
45 * <p>The following examples illustrate a couple of variants of the range query:
48 * // A simple numeric range query:
51 * "field": "fieldname",
57 * // A simple date range query:
60 * "field": "fieldname",
61 * "gt": "2016-10-06T00:00:00.558+03:00",
62 * "lt": "2016-10-06T23:59:59.558+03:00"
67 public class RangeQuery {
70 * The name of the field to apply the range query against.
75 * The value of the field must be greater than this value to be a match.<br>
76 * NOTE: Only one of 'gt' or 'gte' should be set on any single {@link RangeQuery}
82 * The value of the field must be greater than or equal to this value to be a match.<br>
83 * NOTE: Only one of 'gt' or 'gte' should be set on any single {@link RangeQuery}
89 * The value of the field must be less than this value to be a match.<br>
90 * NOTE: Only one of 'lt' or 'lte' should be set on any single {@link RangeQuery}
96 * The value of the field must be less than or equal to than this value to be a match.<br>
97 * NOTE: Only one of 'lt' or 'lte' should be set on any single {@link RangeQuery}
102 private String format;
104 @JsonProperty("time-zone")
105 private String timeZone;
107 public String getField() {
111 public void setField(String field) {
115 public Object getGt() {
119 public void setGt(Object gt) {
121 // It does not make sense to assign a value to both the 'greater than'
122 // and 'greater than or equal' operations, so make sure we are not
123 // trying to do that.
126 // Make sure that we are not trying to mix both numeric and date
127 // type values in the same queries.
128 if (((lt != null) && !typesMatch(gt, lt))
129 || ((lte != null) && !typesMatch(gt, lte))) {
130 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
133 // If we made it here, then we're all good. Store the value.
136 throw new IllegalArgumentException("Cannot assign both 'gt' and 'gte' fields in the same ranged query");
141 public Object getGte() {
145 public void setGte(Object gte) {
147 // It does not make sense to assign a value to both the 'greater than'
148 // and 'greater than or equal' operations, so make sure we are not
149 // trying to do that.
152 // Make sure that we are not trying to mix both numeric and date
153 // type values in the same queries.
154 if (((lt != null) && !typesMatch(gte, lt))
155 || ((lte != null) && !typesMatch(gte, lte))) {
156 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
159 // If we made it here, then we're all good. Store the value.
163 throw new IllegalArgumentException("Cannot assign both 'gt' and 'gte' fields in the same ranged query");
167 public Object getLt() {
171 public void setLt(Object lt) {
173 // It does not make sense to assign a value to both the 'less than'
174 // and 'less than or equal' operations, so make sure we are not
175 // trying to do that.
178 // Make sure that we are not trying to mix both numeric and date
179 // type values in the same queries.
180 if (((gt != null) && !typesMatch(lt, gt))
181 || ((gte != null) && !typesMatch(lt, gte))) {
182 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
185 // If we made it here, then we're all good. Store the value.
189 throw new IllegalArgumentException("Cannot assign both 'lt' and 'lte' fields in the same ranged query");
193 public Object getLte() {
197 public void setLte(Object lte) {
199 // It does not make sense to assign a value to both the 'greater than'
200 // and 'greater than or equal' operations, so make sure we are not
201 // trying to do that.
204 // Make sure that we are not trying to mix both numeric and date
205 // type values in the same queries.
206 if (((gt != null) && !typesMatch(lte, gt))
207 || ((gte != null) && !typesMatch(lte, gte))) {
208 throw new IllegalArgumentException("Cannot mix date and numeric values in the same ranged query");
211 // If we made it here, then we're all good. Store the value.
215 throw new IllegalArgumentException("Cannot assign both 'lt' and 'lte' fields in the same ranged query");
219 public String getFormat() {
223 public void setFormat(String format) {
224 this.format = format;
227 public String getTimeZone() {
231 public void setTimeZone(String timeZone) {
232 this.timeZone = timeZone;
236 * This convenience method determines whether or not the supplied
237 * value needs to be enclosed in '"' characters when generating
238 * ElasticSearch compatible syntax.
240 * @param val - The value to check.
241 * @return - A string representation of the value for inclusion
242 * in an ElasticSearch syntax string.
244 private String formatStringOrNumericVal(Object val) {
246 if (val instanceof String) {
247 return "\"" + val.toString() + "\"";
249 return val.toString();
255 * This convenience method verifies that the supplied objects are
256 * of classes considered to be compatible for a ranged query.
258 * @param value1 - The first value to check.
259 * @param value2 - The second value to check.
260 * @return - True if the two objects are compatible for inclusion in the
261 * same ranged query, False, otherwise.
263 boolean typesMatch(Object value1, Object value2) {
265 return ((value1 instanceof String) && (value2 instanceof String))
266 || (!(value1 instanceof String) && !(value2 instanceof String));
271 * This method returns a string which represents this query in syntax
272 * that is understandable by ElasticSearch and is suitable for inclusion
273 * in an ElasticSearch query string.
275 * @return - ElasticSearch syntax string.
277 public String toElasticSearch() {
279 StringBuilder sb = new StringBuilder();
282 sb.append("\"range\": {");
283 sb.append("\"").append(field).append("\": {");
285 // We may have one or zero of 'greater than' or 'greater
287 boolean needComma = false;
289 sb.append("\"gte\": ").append(formatStringOrNumericVal(gte));
291 } else if (gt != null) {
292 sb.append("\"gt\": ").append(formatStringOrNumericVal(gt));
296 // We may have one or zero of 'less than' or 'less
302 sb.append("\"lte\": ").append(formatStringOrNumericVal(lte));
303 } else if (lt != null) {
307 sb.append("\"lt\": ").append(formatStringOrNumericVal(lt));
310 // Append the format specifier if one was provided.
311 if (format != null) {
312 sb.append(", \"format\": \"").append(format).append("\"");
315 // Append the time zone specifier if one was provided.
316 if (timeZone != null) {
317 sb.append(", \"time_zone\": \"").append(timeZone).append("\"");
324 return sb.toString();
328 public String toString() {
330 String str = "{ field: " + field + ", ";
334 } else if (gte != null) {
335 str += "gte: " + gte;
339 str += (((gt != null) || (gte != null)) ? ", " : "") + "lt: " + lt;
340 } else if (lte != null) {
341 str += (((gt != null) || (gte != null)) ? ", " : "") + "lte: " + lte;