FileDocCategorySizeDatePackage
BooleanQueryBuilder.javaAPI DocApache Lucene 1.92233Mon Feb 27 11:12:20 GMT 2006org.apache.lucene.xmlparser.builders

BooleanQueryBuilder

public class BooleanQueryBuilder extends Object implements org.apache.lucene.xmlparser.QueryBuilder
author
maharwood

Fields Summary
private org.apache.lucene.xmlparser.QueryBuilder
factory
Constructors Summary
public BooleanQueryBuilder(org.apache.lucene.xmlparser.QueryBuilder factory)

		this.factory=factory;
	
Methods Summary
private org.apache.lucene.search.BooleanClause$OccurgetOccursValue(org.w3c.dom.Element clauseElem)

		String occs=clauseElem.getAttribute("occurs");
		BooleanClause.Occur occurs=BooleanClause.Occur.SHOULD;
		if("must".equalsIgnoreCase(occs))
		{
			occurs=BooleanClause.Occur.MUST;
		}
		else
		{
			if("mustNot".equalsIgnoreCase(occs))
			{
				occurs=BooleanClause.Occur.MUST_NOT;
			}			
			else
			{
				if(("should".equalsIgnoreCase(occs))||("".equals(occs)))
				{
					occurs=BooleanClause.Occur.SHOULD;
				}			
				else				
				{
					if(occs!=null)
					{
						throw new ParserException("Invalid value for \"occurs\" attribute of clause:"+occs);
					}
				}
			}
		}
		return occurs;
		
	
public org.apache.lucene.search.QuerygetQuery(org.w3c.dom.Element e)

		BooleanQuery bq=new BooleanQuery();
		bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
		NodeList nl = e.getElementsByTagName("Clause");
		for(int i=0;i<nl.getLength();i++)
		{
			Element clauseElem=(Element) nl.item(i);
			BooleanClause.Occur occurs=getOccursValue(clauseElem);
			
			//find the first element child which should contain a Query 
			Element clauseQuery=DOMUtils.getFirstChildElement(clauseElem); 
			if(clauseQuery!=null)
			{
				Query q=factory.getQuery(clauseQuery);
				bq.add(new BooleanClause(q,occurs));
				
			}
			else
			{
				throw new ParserException("BooleanClause missing child query element ");
			}
		}
		
		return bq;