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 public class QueryStatement {
27 private TermQuery match;
29 @JsonProperty("not-match")
30 private TermQuery notMatch;
32 @JsonProperty("parsed-query")
33 private ParsedQuery parsedQuery;
35 private RangeQuery range;
37 public TermQuery getMatch() {
41 public void setMatch(TermQuery match) {
45 public TermQuery getNotMatch() {
49 public void setNotMatch(TermQuery notMatch) {
50 this.notMatch = notMatch;
53 public ParsedQuery getParsedQuery() {
57 public void setParsedQuery(ParsedQuery parsedQuery) {
58 this.parsedQuery = parsedQuery;
61 public RangeQuery getRange() {
65 public void setRange(RangeQuery range) {
69 public boolean isNotMatch() {
70 return (notMatch != null);
73 public String toElasticSearch() {
76 return match.toElasticSearch();
78 } else if (notMatch != null) {
79 return notMatch.toElasticSearch();
81 } else if (parsedQuery != null) {
83 // We need some special wrapping if this query is against a nested field.
84 if (fieldIsNested(parsedQuery.getField())) {
85 return "{\"nested\": { \"path\": \"" + pathForNestedField(parsedQuery.getField()) + "\", \"query\": "
86 + parsedQuery.toElasticSearch() + "}}";
88 return parsedQuery.toElasticSearch();
91 } else if (range != null) {
93 // We need some special wrapping if this query is against a nested field.
94 if (fieldIsNested(range.getField())) {
95 return "{\"nested\": { \"path\": \"" + pathForNestedField(range.getField()) + "\", \"query\": "
96 + range.toElasticSearch() + "}}";
98 return range.toElasticSearch();
102 // throw an exception?
107 private boolean fieldIsNested(String field) {
108 return field.contains(".");
111 private String pathForNestedField(String field) {
112 int index = field.lastIndexOf('.');
113 return field.substring(0, index);
117 public String toString() {
119 StringBuilder sb = new StringBuilder();
124 sb.append("TERM QUERY: { match: {").append(match.toString()).append("}}");
125 } else if (notMatch != null) {
126 sb.append("TERM QUERY: { not-match: {").append(match.toString()).append("}}");
127 } else if (parsedQuery != null) {
128 sb.append("PARSED QUERY: { ").append(parsedQuery.toString()).append("}");
129 } else if (range != null) {
130 sb.append("RANGE QUERY: { ").append(range.toString()).append("}");
132 sb.append("UNDEFINED");
136 return sb.toString();