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.onap.aai.sparky.search.filters.entity;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.json.Json;
29 import javax.json.JsonArray;
30 import javax.json.JsonArrayBuilder;
31 import javax.json.JsonBuilderFactory;
32 import javax.json.JsonObject;
33 import javax.json.JsonObjectBuilder;
35 public class BoolQueryBuilder {
37 private List<MatchFilterCriteriaEntity> mustFilters;
38 private List<MatchFilterCriteriaEntity> shouldFilters;
40 private int minShouldMatch;
42 public BoolQueryBuilder() {
44 mustFilters = new ArrayList<MatchFilterCriteriaEntity>();
45 shouldFilters = new ArrayList<MatchFilterCriteriaEntity>();
50 public void addMustFilter(MatchFilterCriteriaEntity filter) {
52 if (!mustFilters.contains(filter)) {
53 mustFilters.add(filter);
58 public void addShouldFilter(MatchFilterCriteriaEntity filter) {
60 if (!shouldFilters.contains(filter)) {
61 shouldFilters.add(filter);
66 public void setMinShouldMatch(int minShouldMatch) {
67 this.minShouldMatch = minShouldMatch;
70 public boolean isMatchAll() {
71 return (mustFilters.isEmpty() && shouldFilters.isEmpty());
74 public JsonObject getJsonObject() {
76 * Specify a null config for now, but if we want normalize all the builders, we can do it at one
77 * location, when we are ready.
79 JsonBuilderFactory factory = Json.createBuilderFactory(null);
81 JsonObjectBuilder boolBuilder = factory.createObjectBuilder();
83 if (!mustFilters.isEmpty()) {
84 JsonArrayBuilder mustArrayBuilder = factory.createArrayBuilder();
86 for (MatchFilterCriteriaEntity matchCriteria : mustFilters) {
87 mustArrayBuilder.add(matchCriteria.getJsonObject());
90 JsonArray mustArray = mustArrayBuilder.build();
91 boolBuilder.add("must", mustArray);
94 if (!shouldFilters.isEmpty()) {
95 JsonArray shouldArray = null;
96 JsonArrayBuilder shouldArrayBuilder = factory.createArrayBuilder();
98 for (MatchFilterCriteriaEntity matchCriteria : shouldFilters) {
99 shouldArrayBuilder.add(matchCriteria.getJsonObject());
102 shouldArray = shouldArrayBuilder.build();
103 boolBuilder.add("should", shouldArray).add("min_should_match", minShouldMatch);
106 JsonObjectBuilder queryObjectBuilder = factory.createObjectBuilder();
109 * If both filter lists are empty then we are doing an aggregation based off fields. Just
110 * match-all for the query.
113 JsonObject matchAllObject = factory.createObjectBuilder().build();
114 queryObjectBuilder.add("match_all", matchAllObject);
116 queryObjectBuilder.add("bool", boolBuilder.build());
119 return queryObjectBuilder.build();