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.
30 * 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 filter to pass.
51 private QueryStatement[] any;
54 public QueryStatement[] getAll() {
58 public void setAll(QueryStatement[] all) {
62 public QueryStatement[] getAny() {
66 public void setAny(QueryStatement[] any) {
71 * This method returns a string which represents this filter in syntax that is understandable by ElasticSearch and
72 * is suitable for inclusion 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<>();
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 (matchQueriesCount > 0) {
130 if (query.isNotMatch()) {
131 notMatchQueries.add(query);
133 sb.append(query.toElasticSearch());
139 notMatchQueriesCount = 0;
140 sb.append("\"must_not\": [");
141 for (QueryStatement query : notMatchQueries) {
142 if (notMatchQueriesCount > 0) {
145 sb.append(query.toElasticSearch());
146 notMatchQueriesCount++;
153 return sb.toString();
157 public String toString() {
159 StringBuilder sb = new StringBuilder();
165 for (QueryStatement query : all) {
166 sb.append(query.toString());
173 for (QueryStatement query : any) {
174 sb.append(query.toString());
181 return sb.toString();