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 java.util.ArrayList;
24 import java.util.List;
27 * This class represents the filter stanza in a search statement.
29 * <p>The expected JSON structure for a filter stanza is as follows:
33 * "all": [ {query structure}, {query structure}, ... {query structure} ],
34 * "any": [ {query structure}, {query structure}, ... {query structure} ]
42 * All queries in this list must evaluate to true for the filter to pass.
44 private QueryStatement[] all;
47 * Any one of the queries in this list must evaluate to true for the
50 private QueryStatement[] any;
53 public QueryStatement[] getAll() {
57 public void setAll(QueryStatement[] all) {
61 public QueryStatement[] getAny() {
65 public void setAny(QueryStatement[] any) {
70 * This method returns a string which represents this filter in syntax
71 * that is understandable by ElasticSearch and is suitable for inclusion
72 * in an ElasticSearch query string.
74 * @return - ElasticSearch syntax string.
76 public String toElasticSearch() {
78 StringBuilder sb = new StringBuilder();
80 List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
82 sb.append("\"bool\": {");
84 // Add the queries from our 'all' list.
85 int matchQueriesCount = 0;
86 int notMatchQueriesCount = 0;
88 sb.append("\"must\": [");
90 for (QueryStatement query : all) {
91 if (matchQueriesCount > 0) {
95 if (query.isNotMatch()) {
96 notMatchQueries.add(query);
98 sb.append(query.toElasticSearch());
105 sb.append("\"must_not\": [");
106 for (QueryStatement query : notMatchQueries) {
107 if (notMatchQueriesCount > 0) {
110 sb.append(query.toElasticSearch());
111 notMatchQueriesCount++;
116 // Add the queries from our 'any' list.
117 notMatchQueries.clear();
122 sb.append("\"should\": [");
124 matchQueriesCount = 0;
125 for (QueryStatement query : any) {
126 //if(!firstQuery.compareAndSet(true, false)) {
127 if (matchQueriesCount > 0) {
131 if (query.isNotMatch()) {
132 notMatchQueries.add(query);
134 sb.append(query.toElasticSearch());
140 //firstQuery.set(true);
141 notMatchQueriesCount = 0;
142 sb.append("\"must_not\": [");
143 for (QueryStatement query : notMatchQueries) {
144 //if(!firstQuery.compareAndSet(true, false)) {
145 if (notMatchQueriesCount > 0) {
148 sb.append(query.toElasticSearch());
149 notMatchQueriesCount++;
156 return sb.toString();
160 public String toString() {
162 StringBuilder sb = new StringBuilder();
168 for (QueryStatement query : all) {
169 sb.append(query.toString());
176 for (QueryStatement query : any) {
177 sb.append(query.toString());
184 return sb.toString();