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.sparky.search.filters.entity;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.json.Json;
27 import javax.json.JsonArray;
28 import javax.json.JsonArrayBuilder;
29 import javax.json.JsonBuilderFactory;
30 import javax.json.JsonObject;
31 import javax.json.JsonObjectBuilder;
33 public class BoolQueryBuilder {
35 private List<MatchFilterCriteriaEntity> mustFilters;
36 private List<MatchFilterCriteriaEntity> shouldFilters;
38 private int minShouldMatch;
40 public BoolQueryBuilder() {
42 mustFilters = new ArrayList<MatchFilterCriteriaEntity>();
43 shouldFilters = new ArrayList<MatchFilterCriteriaEntity>();
48 public void addMustFilter(MatchFilterCriteriaEntity filter) {
50 if (!mustFilters.contains(filter)) {
51 mustFilters.add(filter);
56 public void addShouldFilter(MatchFilterCriteriaEntity filter) {
58 if (!shouldFilters.contains(filter)) {
59 shouldFilters.add(filter);
64 public void setMinShouldMatch(int minShouldMatch) {
65 this.minShouldMatch = minShouldMatch;
68 public boolean isMatchAll() {
69 return (mustFilters.isEmpty() && shouldFilters.isEmpty());
72 public JsonObject getJsonObject() {
74 * Specify a null config for now, but if we want normalize all the builders, we can do it at one
75 * location, when we are ready.
77 JsonBuilderFactory factory = Json.createBuilderFactory(null);
79 JsonObjectBuilder boolBuilder = factory.createObjectBuilder();
81 if(!mustFilters.isEmpty()){
82 JsonArrayBuilder mustArrayBuilder = factory.createArrayBuilder();
84 for (MatchFilterCriteriaEntity matchCriteria : mustFilters) {
85 mustArrayBuilder.add(matchCriteria.getJsonObject());
88 JsonArray mustArray = mustArrayBuilder.build();
89 boolBuilder.add("must", mustArray);
92 if (!shouldFilters.isEmpty()) {
93 JsonArray shouldArray = null;
94 JsonArrayBuilder shouldArrayBuilder = factory.createArrayBuilder();
96 for (MatchFilterCriteriaEntity matchCriteria : shouldFilters) {
97 shouldArrayBuilder.add(matchCriteria.getJsonObject());
100 shouldArray = shouldArrayBuilder.build();
101 boolBuilder.add("should", shouldArray).add("min_should_match", minShouldMatch);
104 JsonObjectBuilder queryObjectBuilder = factory.createObjectBuilder();
107 * If both filter lists are empty then we are doing an aggregation
108 * based off fields. Just match-all for the query.
111 JsonObject matchAllObject = factory.createObjectBuilder().build();
112 queryObjectBuilder.add("match_all", matchAllObject);
114 queryObjectBuilder.add("bool", boolBuilder.build());
117 return queryObjectBuilder.build();