Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / searchapi / Filter.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 java.util.ArrayList;
28 import java.util.List;
29
30 /**
31  * This class represents the filter stanza in a search statement.
32  *
33  * <p>The expected JSON structure for a filter stanza is as follows:
34  * <pre>
35  * {
36  *     "filter": {
37  *        "all": [ {query structure}, {query structure}, ... {query structure} ],
38  *        "any": [ {query structure}, {query structure}, ... {query structure} ]
39  *     }
40  * }
41  * </pre>
42  */
43 public class Filter {
44
45   /**
46    * All queries in this list must evaluate to true for the filter to pass.
47    */
48   private QueryStatement[] all;
49
50   /**
51    * Any one of the queries in this list must evaluate to true for the
52    * filter to pass.
53    */
54   private QueryStatement[] any;
55
56
57   public QueryStatement[] getAll() {
58     return all;
59   }
60
61   public void setAll(QueryStatement[] all) {
62     this.all = all;
63   }
64
65   public QueryStatement[] getAny() {
66     return any;
67   }
68
69   public void setAny(QueryStatement[] any) {
70     this.any = any;
71   }
72
73   /**
74    * This method returns a string which represents this filter in syntax
75    * that is understandable by ElasticSearch and is suitable for inclusion
76    * in an ElasticSearch query string.
77    *
78    * @return - ElasticSearch syntax string.
79    */
80   public String toElasticSearch() {
81
82     StringBuilder sb = new StringBuilder();
83
84     List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
85     sb.append("{");
86     sb.append("\"bool\": {");
87
88     // Add the queries from our 'all' list.
89     int matchQueriesCount = 0;
90     int notMatchQueriesCount = 0;
91     if (all != null) {
92       sb.append("\"must\": [");
93
94       for (QueryStatement query : all) {
95         if (matchQueriesCount > 0) {
96           sb.append(", ");
97         }
98
99         if (query.isNotMatch()) {
100           notMatchQueries.add(query);
101         } else {
102           sb.append(query.toElasticSearch());
103           matchQueriesCount++;
104         }
105       }
106       sb.append("],");
107
108
109       sb.append("\"must_not\": [");
110       for (QueryStatement query : notMatchQueries) {
111         if (notMatchQueriesCount > 0) {
112           sb.append(", ");
113         }
114         sb.append(query.toElasticSearch());
115         notMatchQueriesCount++;
116       }
117       sb.append("]");
118     }
119
120     // Add the queries from our 'any' list.
121     notMatchQueries.clear();
122     if (any != null) {
123       if (all != null) {
124         sb.append(",");
125       }
126       sb.append("\"should\": [");
127
128       matchQueriesCount = 0;
129       for (QueryStatement query : any) {
130         //if(!firstQuery.compareAndSet(true, false)) {
131         if (matchQueriesCount > 0) {
132           sb.append(", ");
133         }
134
135         if (query.isNotMatch()) {
136           notMatchQueries.add(query);
137         } else {
138           sb.append(query.toElasticSearch());
139           matchQueriesCount++;
140         }
141       }
142       sb.append("],");
143
144       //firstQuery.set(true);
145       notMatchQueriesCount = 0;
146       sb.append("\"must_not\": [");
147       for (QueryStatement query : notMatchQueries) {
148         //if(!firstQuery.compareAndSet(true, false)) {
149         if (notMatchQueriesCount > 0) {
150           sb.append(", ");
151         }
152         sb.append(query.toElasticSearch());
153         notMatchQueriesCount++;
154       }
155       sb.append("]");
156     }
157     sb.append("}");
158     sb.append("}");
159
160     return sb.toString();
161   }
162
163   @Override
164   public String toString() {
165
166     StringBuilder sb = new StringBuilder();
167
168     sb.append("{");
169
170     sb.append("all: [");
171     if (all != null) {
172       for (QueryStatement query : all) {
173         sb.append(query.toString());
174       }
175     }
176     sb.append("], ");
177
178     sb.append("any: [");
179     if (any != null) {
180       for (QueryStatement query : any) {
181         sb.append(query.toString());
182       }
183     }
184     sb.append("] ");
185
186     sb.append("}");
187
188     return sb.toString();
189   }
190 }