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;
29 public class QueryStatement {
31 private TermQuery match;
33 @JsonProperty("not-match")
34 private TermQuery notMatch;
36 @JsonProperty("parsed-query")
37 private ParsedQuery parsedQuery;
39 private RangeQuery range;
41 public TermQuery getMatch() {
45 public void setMatch(TermQuery match) {
49 public TermQuery getNotMatch() {
53 public void setNotMatch(TermQuery notMatch) {
54 this.notMatch = notMatch;
57 public ParsedQuery getParsedQuery() {
61 public void setParsedQuery(ParsedQuery parsedQuery) {
62 this.parsedQuery = parsedQuery;
65 public RangeQuery getRange() {
69 public void setRange(RangeQuery range) {
73 public boolean isNotMatch() {
74 return (notMatch != null);
77 public String toElasticSearch() {
80 return match.toElasticSearch();
82 } else if (notMatch != null) {
83 return notMatch.toElasticSearch();
85 } else if (parsedQuery != null) {
87 // We need some special wrapping if this query is against a nested field.
88 if (fieldIsNested(parsedQuery.getField())) {
89 return "{\"nested\": { \"path\": \"" + pathForNestedField(parsedQuery.getField())
90 + "\", \"query\": " + parsedQuery.toElasticSearch() + "}}";
92 return parsedQuery.toElasticSearch();
95 } else if (range != null) {
97 // We need some special wrapping if this query is against a nested field.
98 if (fieldIsNested(range.getField())) {
99 return "{\"nested\": { \"path\": \"" + pathForNestedField(range.getField())
100 + "\", \"query\": " + range.toElasticSearch() + "}}";
102 return range.toElasticSearch();
106 // throw an exception?
111 private boolean fieldIsNested(String field) {
112 return field.contains(".");
115 private String pathForNestedField(String field) {
116 int index = field.lastIndexOf('.');
117 return field.substring(0, index);
121 public String toString() {
123 StringBuilder sb = new StringBuilder();
128 sb.append("TERM QUERY: { match: {").append(match.toString()).append("}}");
129 } else if (notMatch != null) {
130 sb.append("TERM QUERY: { not-match: {").append(match.toString()).append("}}");
131 } else if (parsedQuery != null) {
132 sb.append("PARSED QUERY: { ").append(parsedQuery.toString()).append("}");
133 } else if (range != null) {
134 sb.append("RANGE QUERY: { ").append(range.toString()).append("}");
136 sb.append("UNDEFINED");
140 return sb.toString();