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.ModifierQueryNode;
27 import org.apache.lucene.queryParser.core.nodes.QueryNode;
28 import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode.Modifier;
29 import org.apache.lucene.queryParser.standard.nodes.StandardBooleanQueryNode;
30 import org.apache.lucene.queryParser.standard.parser.EscapeQuerySyntaxImpl;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.Query;
34 import org.apache.lucene.search.Similarity;
35 import org.apache.lucene.search.BooleanQuery.TooManyClauses;
38 * This builder does the same as the {@link BooleanQueryNodeBuilder}, but this
39 * considers if the built {@link BooleanQuery} should have its coord disabled or
42 * @see BooleanQueryNodeBuilder
44 * @see Similarity#coord(int, int)
46 public class StandardBooleanQueryNodeBuilder implements StandardQueryBuilder {
48 public StandardBooleanQueryNodeBuilder() {
52 public BooleanQuery build(QueryNode queryNode) throws QueryNodeException {
53 StandardBooleanQueryNode booleanNode = (StandardBooleanQueryNode) queryNode;
55 BooleanQuery bQuery = new BooleanQuery(booleanNode.isDisableCoord());
56 List<QueryNode> children = booleanNode.getChildren();
58 if (children != null) {
60 for (QueryNode child : children) {
61 Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
64 Query query = (Query) obj;
67 bQuery.add(query, getModifierValue(child));
68 } catch (TooManyClauses ex) {
70 throw new QueryNodeException(new MessageImpl(
71 QueryParserMessages.TOO_MANY_BOOLEAN_CLAUSES, BooleanQuery
72 .getMaxClauseCount(), queryNode
73 .toQueryString(new EscapeQuerySyntaxImpl())), ex);
87 private static BooleanClause.Occur getModifierValue(QueryNode node)
88 throws QueryNodeException {
90 if (node instanceof ModifierQueryNode) {
91 ModifierQueryNode mNode = ((ModifierQueryNode) node);
92 Modifier modifier = mNode.getModifier();
94 if (Modifier.MOD_NONE.equals(modifier)) {
95 return BooleanClause.Occur.SHOULD;
97 } else if (Modifier.MOD_NOT.equals(modifier)) {
98 return BooleanClause.Occur.MUST_NOT;
101 return BooleanClause.Occur.MUST;
105 return BooleanClause.Occur.SHOULD;