1 package org.apache.lucene.queryParser.standard.builders;
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.List;
22 import org.apache.lucene.messages.MessageImpl;
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.builders.QueryTreeBuilder;
25 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
26 import org.apache.lucene.queryParser.core.nodes.BooleanQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode;
28 import org.apache.lucene.queryParser.core.nodes.QueryNode;
29 import org.apache.lucene.queryParser.standard.parser.EscapeQuerySyntaxImpl;
30 import org.apache.lucene.search.BooleanClause;
31 import org.apache.lucene.search.BooleanQuery;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.BooleanQuery.TooManyClauses;
36 * Builds a {@link BooleanQuery} object from a {@link BooleanQueryNode} object.
37 * Every children in the {@link BooleanQueryNode} object must be already tagged
38 * using {@link QueryTreeBuilder#QUERY_TREE_BUILDER_TAGID} with a {@link Query}
41 * It takes in consideration if the children is a {@link ModifierQueryNode} to
42 * define the {@link BooleanClause}.
44 public class BooleanQueryNodeBuilder implements StandardQueryBuilder {
46 public BooleanQueryNodeBuilder() {
50 public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
51 BooleanQueryNode booleanNode = (BooleanQueryNode) queryNode;
53 BooleanQuery bQuery = new BooleanQuery();
54 List<QueryNode> children = booleanNode.getChildren();
56 if (children != null) {
58 for (QueryNode child : children) {
59 Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
62 Query query = (Query) obj;
65 bQuery.add(query, getModifierValue(child));
67 } catch (TooManyClauses ex) {
69 throw new QueryNodeException(new MessageImpl(
70 QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
71 .getMaxClauseCount(), queryNode
72 .toQueryString(new EscapeQuerySyntaxImpl())), ex);
86 private static BooleanClause.Occur getModifierValue(QueryNode node)
87 throws QueryNodeException {
89 if (node instanceof ModifierQueryNode) {
90 ModifierQueryNode mNode = ((ModifierQueryNode) node);
91 switch (mNode.getModifier()) {
94 return BooleanClause.Occur.MUST;
97 return BooleanClause.Occur.MUST_NOT;
100 return BooleanClause.Occur.SHOULD;
106 return BooleanClause.Occur.SHOULD;