2 * Created on 25-Jan-2006
4 package org.apache.lucene.xmlparser.builders;
6 import org.apache.lucene.search.BooleanClause;
7 import org.apache.lucene.search.BooleanQuery;
8 import org.apache.lucene.search.Query;
9 import org.apache.lucene.xmlparser.DOMUtils;
10 import org.apache.lucene.xmlparser.ParserException;
11 import org.apache.lucene.xmlparser.QueryBuilder;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
16 * Licensed to the Apache Software Foundation (ASF) under one or more
17 * contributor license agreements. See the NOTICE file distributed with
18 * this work for additional information regarding copyright ownership.
19 * The ASF licenses this file to You under the Apache License, Version 2.0
20 * (the "License"); you may not use this file except in compliance with
21 * the License. You may obtain a copy of the License at
23 * http://www.apache.org/licenses/LICENSE-2.0
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS,
27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
35 public class BooleanQueryBuilder implements QueryBuilder {
37 private QueryBuilder factory;
39 public BooleanQueryBuilder(QueryBuilder factory)
45 * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
47 public Query getQuery(Element e) throws ParserException {
48 BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
49 bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
50 bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
52 NodeList nl = e.getChildNodes();
53 for(int i=0;i<nl.getLength();i++)
55 Node node = nl.item(i);
56 if(node.getNodeName().equals("Clause"))
58 Element clauseElem=(Element) node;
59 BooleanClause.Occur occurs=getOccursValue(clauseElem);
61 Element clauseQuery=DOMUtils.getFirstChildOrFail(clauseElem);
62 Query q=factory.getQuery(clauseQuery);
63 bq.add(new BooleanClause(q,occurs));
69 static BooleanClause.Occur getOccursValue(Element clauseElem) throws ParserException
71 String occs=clauseElem.getAttribute("occurs");
72 BooleanClause.Occur occurs=BooleanClause.Occur.SHOULD;
73 if("must".equalsIgnoreCase(occs))
75 occurs=BooleanClause.Occur.MUST;
79 if("mustNot".equalsIgnoreCase(occs))
81 occurs=BooleanClause.Occur.MUST_NOT;
85 if(("should".equalsIgnoreCase(occs))||("".equals(occs)))
87 occurs=BooleanClause.Occur.SHOULD;
93 throw new ParserException("Invalid value for \"occurs\" attribute of clause:"+occs);