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 public class QueryStatement {
29 private TermQuery match;
31 @JsonProperty("not-match")
32 private TermQuery notMatch;
34 @JsonProperty("parsed-query")
35 private ParsedQuery parsedQuery;
37 private RangeQuery range;
39 public TermQuery getMatch() {
43 public void setMatch(TermQuery match) {
47 public TermQuery getNotMatch() {
51 public void setNotMatch(TermQuery notMatch) {
52 this.notMatch = notMatch;
55 public ParsedQuery getParsedQuery() {
59 public void setParsedQuery(ParsedQuery parsedQuery) {
60 this.parsedQuery = parsedQuery;
63 public RangeQuery getRange() {
67 public void setRange(RangeQuery range) {
71 public boolean isNotMatch() {
72 return (notMatch != null);
75 public String toElasticSearch() {
78 return match.toElasticSearch();
80 } else if (notMatch != null) {
81 return notMatch.toElasticSearch();
83 } else if (parsedQuery != null) {
85 // We need some special wrapping if this query is against a nested field.
86 if (fieldIsNested(parsedQuery.getField())) {
87 return "{\"nested\": { \"path\": \"" + pathForNestedField(parsedQuery.getField())
88 + "\", \"query\": " + parsedQuery.toElasticSearch() + "}}";
90 return parsedQuery.toElasticSearch();
93 } else if (range != null) {
95 // We need some special wrapping if this query is against a nested field.
96 if (fieldIsNested(range.getField())) {
97 return "{\"nested\": { \"path\": \"" + pathForNestedField(range.getField())
98 + "\", \"query\": " + range.toElasticSearch() + "}}";
100 return range.toElasticSearch();
104 // throw an exception?
109 private boolean fieldIsNested(String field) {
110 return field.contains(".");
113 private String pathForNestedField(String field) {
114 int index = field.lastIndexOf('.');
115 return field.substring(0, index);
119 public String toString() {
121 StringBuilder sb = new StringBuilder();
126 sb.append("TERM QUERY: { match: {").append(match.toString()).append("}}");
127 } else if (notMatch != null) {
128 sb.append("TERM QUERY: { not-match: {").append(match.toString()).append("}}");
129 } else if (parsedQuery != null) {
130 sb.append("PARSED QUERY: { ").append(parsedQuery.toString()).append("}");
131 } else if (range != null) {
132 sb.append("RANGE QUERY: { ").append(range.toString()).append("}");
134 sb.append("UNDEFINED");
138 return sb.toString();