f5fc367ccdb76cac81d2af59a14214de7c06dfbe
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / searchapi / QueryStatement.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.searchapi;
26
27 import com.fasterxml.jackson.annotation.JsonProperty;
28
29 public class QueryStatement {
30
31   private TermQuery match;
32
33   @JsonProperty("not-match")
34   private TermQuery notMatch;
35
36   @JsonProperty("parsed-query")
37   private ParsedQuery parsedQuery;
38
39   private RangeQuery range;
40
41   public TermQuery getMatch() {
42     return match;
43   }
44
45   public void setMatch(TermQuery match) {
46     this.match = match;
47   }
48
49   public TermQuery getNotMatch() {
50     return notMatch;
51   }
52
53   public void setNotMatch(TermQuery notMatch) {
54     this.notMatch = notMatch;
55   }
56
57   public ParsedQuery getParsedQuery() {
58     return parsedQuery;
59   }
60
61   public void setParsedQuery(ParsedQuery parsedQuery) {
62     this.parsedQuery = parsedQuery;
63   }
64
65   public RangeQuery getRange() {
66     return range;
67   }
68
69   public void setRange(RangeQuery range) {
70     this.range = range;
71   }
72
73   public boolean isNotMatch() {
74     return (notMatch != null);
75   }
76
77   public String toElasticSearch() {
78
79     if (match != null) {
80       return match.toElasticSearch();
81
82     } else if (notMatch != null) {
83       return notMatch.toElasticSearch();
84
85     } else if (parsedQuery != null) {
86
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() + "}}";
91       } else {
92         return parsedQuery.toElasticSearch();
93       }
94
95     } else if (range != null) {
96
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() + "}}";
101       } else {
102         return range.toElasticSearch();
103       }
104
105     } else {
106       // throw an exception?
107       return null;
108     }
109   }
110
111   private boolean fieldIsNested(String field) {
112     return field.contains(".");
113   }
114
115   private String pathForNestedField(String field) {
116     int index = field.lastIndexOf('.');
117     return field.substring(0, index);
118   }
119
120   @Override
121   public String toString() {
122
123     StringBuilder sb = new StringBuilder();
124
125     sb.append("{");
126
127     if (match != null) {
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("}");
135     } else {
136       sb.append("UNDEFINED");
137     }
138
139     sb.append("}");
140     return sb.toString();
141   }
142 }