1 package org.apache.lucene.queryParser.precedence.processors;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.nodes.AndQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.BooleanQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.OrQueryNode;
28 import org.apache.lucene.queryParser.core.nodes.QueryNode;
29 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode.Modifier;
30 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
31 import org.apache.lucene.queryParser.precedence.PrecedenceQueryParser;
32 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler;
33 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
34 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator;
38 * This processor is used to apply the correct {@link ModifierQueryNode} to {@link BooleanQueryNode}s children.
41 * It walks through the query node tree looking for {@link BooleanQueryNode}s. If an {@link AndQueryNode} is found,
42 * every child, which is not a {@link ModifierQueryNode} or the {@link ModifierQueryNode}
43 * is {@link Modifier#MOD_NONE}, becomes a {@link Modifier#MOD_REQ}. For any other
44 * {@link BooleanQueryNode} which is not an {@link OrQueryNode}, it checks the default operator is {@link Operator#AND},
45 * if it is, the same operation when an {@link AndQueryNode} is found is applied to it.
48 * @see ConfigurationKeys#DEFAULT_OPERATOR
49 * @see PrecedenceQueryParser#setDefaultOperator
51 public class BooleanModifiersQueryNodeProcessor extends QueryNodeProcessorImpl {
53 private ArrayList<QueryNode> childrenBuffer = new ArrayList<QueryNode>();
55 private Boolean usingAnd = false;
57 public BooleanModifiersQueryNodeProcessor() {
62 public QueryNode process(QueryNode queryTree) throws QueryNodeException {
63 Operator op = getQueryConfigHandler().get(ConfigurationKeys.DEFAULT_OPERATOR);
66 throw new IllegalArgumentException(
67 "StandardQueryConfigHandler.ConfigurationKeys.DEFAULT_OPERATOR should be set on the QueryConfigHandler");
70 this.usingAnd = StandardQueryConfigHandler.Operator.AND == op;
72 return super.process(queryTree);
77 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
79 if (node instanceof AndQueryNode) {
80 this.childrenBuffer.clear();
81 List<QueryNode> children = node.getChildren();
83 for (QueryNode child : children) {
84 this.childrenBuffer.add(applyModifier(child, Modifier.MOD_REQ));
87 node.set(this.childrenBuffer);
89 } else if (this.usingAnd && node instanceof BooleanQueryNode
90 && !(node instanceof OrQueryNode)) {
92 this.childrenBuffer.clear();
93 List<QueryNode> children = node.getChildren();
95 for (QueryNode child : children) {
96 this.childrenBuffer.add(applyModifier(child, Modifier.MOD_REQ));
99 node.set(this.childrenBuffer);
107 private QueryNode applyModifier(QueryNode node, Modifier mod) {
109 // check if modifier is not already defined and is default
110 if (!(node instanceof ModifierQueryNode)) {
111 return new ModifierQueryNode(node, mod);
114 ModifierQueryNode modNode = (ModifierQueryNode) node;
116 if (modNode.getModifier() == Modifier.MOD_NONE) {
117 return new ModifierQueryNode(modNode.getChild(), mod);
127 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
132 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
133 throws QueryNodeException {