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 java.util.ArrayList;
28 import java.util.List;
31 * This class represents the filter stanza in a search statement.
33 * <p>The expected JSON structure for a filter stanza is as follows:
37 * "all": [ {query structure}, {query structure}, ... {query structure} ],
38 * "any": [ {query structure}, {query structure}, ... {query structure} ]
46 * All queries in this list must evaluate to true for the filter to pass.
48 private QueryStatement[] all;
51 * Any one of the queries in this list must evaluate to true for the
54 private QueryStatement[] any;
57 public QueryStatement[] getAll() {
61 public void setAll(QueryStatement[] all) {
65 public QueryStatement[] getAny() {
69 public void setAny(QueryStatement[] any) {
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.
78 * @return - ElasticSearch syntax string.
80 public String toElasticSearch() {
82 StringBuilder sb = new StringBuilder();
84 List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
86 sb.append("\"bool\": {");
88 // Add the queries from our 'all' list.
89 int matchQueriesCount = 0;
90 int notMatchQueriesCount = 0;
92 sb.append("\"must\": [");
94 for (QueryStatement query : all) {
95 if (matchQueriesCount > 0) {
99 if (query.isNotMatch()) {
100 notMatchQueries.add(query);
102 sb.append(query.toElasticSearch());
109 sb.append("\"must_not\": [");
110 for (QueryStatement query : notMatchQueries) {
111 if (notMatchQueriesCount > 0) {
114 sb.append(query.toElasticSearch());
115 notMatchQueriesCount++;
120 // Add the queries from our 'any' list.
121 notMatchQueries.clear();
126 sb.append("\"should\": [");
128 matchQueriesCount = 0;
129 for (QueryStatement query : any) {
130 //if(!firstQuery.compareAndSet(true, false)) {
131 if (matchQueriesCount > 0) {
135 if (query.isNotMatch()) {
136 notMatchQueries.add(query);
138 sb.append(query.toElasticSearch());
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) {
152 sb.append(query.toElasticSearch());
153 notMatchQueriesCount++;
160 return sb.toString();
164 public String toString() {
166 StringBuilder sb = new StringBuilder();
172 for (QueryStatement query : all) {
173 sb.append(query.toString());
180 for (QueryStatement query : any) {
181 sb.append(query.toString());
188 return sb.toString();