@@ -151,9 +151,12 @@ public static String populateMatcherClause(Matcher matcher, String indexFieldNam
151151 * @param indexName The name of the index to reference in the entity model.
152152 * @param attributeSet The names and values of the input attributes.
153153 * @param attributeName The name of the attribute to reference in the attributeSet.
154+ * @param combiner Combine clauses with "should" or "filter".
154155 * @return
155156 */
156- public static List <String > makeIndexFieldClauses (Model model , String indexName , Map <String , Set <Object >> attributeSet , String attributeName ) {
157+ public static List <String > makeIndexFieldClauses (Model model , String indexName , Map <String , Set <Object >> attributeSet , String attributeName , String combiner ) throws ValidationException {
158+ if (!combiner .equals ("should" ) && !combiner .equals ("filter" ))
159+ throw new ValidationException ("'" + combiner + "' is not a supported clause combiner." );
157160 List <String > indexFieldClauses = new ArrayList <>();
158161 for (String indexFieldName : model .indices ().get (indexName ).attributeIndexFieldsMap ().get (attributeName ).keySet ()) {
159162
@@ -177,10 +180,10 @@ public static List<String> makeIndexFieldClauses(Model model, String indexName,
177180 if (valueClauses .size () == 0 )
178181 continue ;
179182
180- // Combine each value clause into a single "should" clause.
183+ // Combine each value clause into a single "should" or "filter" clause.
181184 String valuesClause = String .join ("," , valueClauses );
182185 if (valueClauses .size () > 1 )
183- valuesClause = "{\" bool\" :{\" should \" :[" + valuesClause + "]}}" ;
186+ valuesClause = "{\" bool\" :{\" " + combiner + " \" :[" + valuesClause + "]}}" ;
184187 indexFieldClauses .add (valuesClause );
185188 }
186189 return indexFieldClauses ;
@@ -194,21 +197,24 @@ public static List<String> makeIndexFieldClauses(Model model, String indexName,
194197 * @param model The entity model.
195198 * @param indexName The name of the index to reference in the entity model.
196199 * @param attributeSet The names and values of the input attributes.
200+ * @param combiner Combine clauses with "should" or "filter".
197201 * @return
198202 */
199- public static List <String > makeAttributeClauses (Model model , String indexName , Map <String , Set <Object >> attributeSet ) {
203+ public static List <String > makeAttributeClauses (Model model , String indexName , Map <String , Set <Object >> attributeSet , String combiner ) throws ValidationException {
204+ if (!combiner .equals ("should" ) && !combiner .equals ("filter" ))
205+ throw new ValidationException ("'" + combiner + "' is not a supported clause combiner." );
200206 List <String > attributeClauses = new ArrayList <>();
201207 for (String attributeName : attributeSet .keySet ()) {
202208
203- // Construct a "should" clause for each index field mapped to this attribute.
204- List <String > indexFieldClauses = makeIndexFieldClauses (model , indexName , attributeSet , attributeName );
209+ // Construct a "should" or "filter" clause for each index field mapped to this attribute.
210+ List <String > indexFieldClauses = makeIndexFieldClauses (model , indexName , attributeSet , attributeName , combiner );
205211 if (indexFieldClauses .size () == 0 )
206212 continue ;
207213
208- // Combine each matcher clause into a single "should" clause.
214+ // Combine each matcher clause into a single "should" or "filter" clause.
209215 String indexFieldsClause = String .join ("," , indexFieldClauses );
210216 if (indexFieldClauses .size () > 1 )
211- indexFieldsClause = "{\" bool\" :{\" should \" :[" + indexFieldsClause + "]}}" ;
217+ indexFieldsClause = "{\" bool\" :{\" " + combiner + " \" :[" + indexFieldsClause + "]}}" ;
212218 attributeClauses .add (indexFieldsClause );
213219 }
214220 return attributeClauses ;
@@ -223,14 +229,14 @@ public static List<String> makeAttributeClauses(Model model, String indexName, M
223229 * @param attributeSet The names and values for the input attributes.
224230 * @return A "bool" clause for all applicable resolvers.
225231 */
226- public static String populateResolversFilterTree (Model model , String indexName , TreeMap <String , TreeMap > resolversFilterTree , Map <String , Set <Object >> attributeSet ) {
232+ public static String populateResolversFilterTree (Model model , String indexName , TreeMap <String , TreeMap > resolversFilterTree , Map <String , Set <Object >> attributeSet ) throws ValidationException {
227233
228234 // Construct a "filter" clause for each attribute at this level of the filter tree.
229235 List <String > attributeClauses = new ArrayList <>();
230236 for (String attributeName : resolversFilterTree .keySet ()) {
231237
232238 // Construct a "should" clause for each index field mapped to this attribute.
233- List <String > indexFieldClauses = makeIndexFieldClauses (model , indexName , attributeSet , attributeName );
239+ List <String > indexFieldClauses = makeIndexFieldClauses (model , indexName , attributeSet , attributeName , "should" );
234240 if (indexFieldClauses .size () == 0 )
235241 continue ;
236242
@@ -503,7 +509,7 @@ private void traverse() throws IOException, ValidationException {
503509
504510 // Create "scope.exclude.attributes" clauses. Combine them into a single "should" clause.
505511 if (!this .scopeExcludeAttributes .isEmpty ()) {
506- List <String > attributeClauses = makeAttributeClauses (this .model , indexName , this .scopeExcludeAttributes );
512+ List <String > attributeClauses = makeAttributeClauses (this .model , indexName , this .scopeExcludeAttributes , "should" );
507513 int size = attributeClauses .size ();
508514 if (size > 1 )
509515 queryMustNotClauses .add ("{\" bool\" :{\" should\" :[" + String .join ("," , attributeClauses ) + "]}}" );
@@ -515,12 +521,12 @@ else if (size == 1)
515521 if (!queryMustNotClauses .isEmpty ())
516522 queryClauses .add ("\" must_not\" :[" + String .join ("," , queryMustNotClauses ) + "]" );
517523
518- // Create "scope.include.attributes" clauses. Combine them into a single "should " clause.
524+ // Create "scope.include.attributes" clauses. Combine them into a single "filter " clause.
519525 if (!this .scopeIncludeAttributes .isEmpty ()) {
520- List <String > attributeClauses = makeAttributeClauses (this .model , indexName , this .scopeIncludeAttributes );
526+ List <String > attributeClauses = makeAttributeClauses (this .model , indexName , this .scopeIncludeAttributes , "filter" );
521527 int size = attributeClauses .size ();
522528 if (size > 1 )
523- queryFilterClauses .add ("{\" bool\" :{\" should \" :[" + String .join ("," , attributeClauses ) + "]}}" );
529+ queryFilterClauses .add ("{\" bool\" :{\" filter \" :[" + String .join ("," , attributeClauses ) + "]}}" );
524530 else if (size == 1 )
525531 queryFilterClauses .add (attributeClauses .get (0 ));
526532 }
0 commit comments