2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sa.searchdbabstraction.searchapi;
25 import java.util.ArrayList;
26 import java.util.List;
29 * This class represents the filter stanza in a search statement.
31 * <p>The expected JSON structure for a filter stanza is as follows:
35 * "all": [ {query structure}, {query structure}, ... {query structure} ],
36 * "any": [ {query structure}, {query structure}, ... {query structure} ]
44 * All queries in this list must evaluate to true for the filter to pass.
46 private QueryStatement[] all;
49 * Any one of the queries in this list must evaluate to true for the
52 private QueryStatement[] any;
55 public QueryStatement[] getAll() {
59 public void setAll(QueryStatement[] all) {
63 public QueryStatement[] getAny() {
67 public void setAny(QueryStatement[] any) {
72 * This method returns a string which represents this filter in syntax
73 * that is understandable by ElasticSearch and is suitable for inclusion
74 * in an ElasticSearch query string.
76 * @return - ElasticSearch syntax string.
78 public String toElasticSearch() {
80 StringBuilder sb = new StringBuilder();
82 List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
84 sb.append("\"bool\": {");
86 // Add the queries from our 'all' list.
87 int matchQueriesCount = 0;
88 int notMatchQueriesCount = 0;
90 sb.append("\"must\": [");
92 for (QueryStatement query : all) {
93 if (matchQueriesCount > 0) {
97 if (query.isNotMatch()) {
98 notMatchQueries.add(query);
100 sb.append(query.toElasticSearch());
107 sb.append("\"must_not\": [");
108 for (QueryStatement query : notMatchQueries) {
109 if (notMatchQueriesCount > 0) {
112 sb.append(query.toElasticSearch());
113 notMatchQueriesCount++;
118 // Add the queries from our 'any' list.
119 notMatchQueries.clear();
124 sb.append("\"should\": [");
126 matchQueriesCount = 0;
127 for (QueryStatement query : any) {
128 //if(!firstQuery.compareAndSet(true, false)) {
129 if (matchQueriesCount > 0) {
133 if (query.isNotMatch()) {
134 notMatchQueries.add(query);
136 sb.append(query.toElasticSearch());
142 //firstQuery.set(true);
143 notMatchQueriesCount = 0;
144 sb.append("\"must_not\": [");
145 for (QueryStatement query : notMatchQueries) {
146 //if(!firstQuery.compareAndSet(true, false)) {
147 if (notMatchQueriesCount > 0) {
150 sb.append(query.toElasticSearch());
151 notMatchQueriesCount++;
158 return sb.toString();
162 public String toString() {
164 StringBuilder sb = new StringBuilder();
170 for (QueryStatement query : all) {
171 sb.append(query.toString());
178 for (QueryStatement query : any) {
179 sb.append(query.toString());
186 return sb.toString();