FileDocCategorySizeDatePackage
TermsQueryBuilder.javaAPI DocApache Lucene 2.1.02868Wed Feb 14 10:46:20 GMT 2007org.apache.lucene.xmlparser.builders

TermsQueryBuilder

public class TermsQueryBuilder extends Object implements org.apache.lucene.xmlparser.QueryBuilder
Builds a BooleanQuery from all of the terms found in the XML element using the choice of analyzer
author
maharwood

Fields Summary
Analyzer
analyzer
Constructors Summary
public TermsQueryBuilder(Analyzer analyzer)

		this.analyzer = analyzer;
	
Methods Summary
public org.apache.lucene.search.QuerygetQuery(org.w3c.dom.Element e)

		
        String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
 		String text=DOMUtils.getNonBlankTextOrFail(e);
 		
		BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
		bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
		TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
		try
		{
			Token token = ts.next();
			Term term = null;
			while (token != null)
			{
				if (term == null)
				{
					term = new Term(fieldName, token.termText());
				} else
				{
//					 create from previous to save fieldName.intern overhead
					term = term.createTerm(token.termText()); 
				}
				bq.add(new BooleanClause(new TermQuery(term),BooleanClause.Occur.SHOULD));
				token = ts.next();
			}
		} 
		catch (IOException ioe)
		{
			throw new RuntimeException("Error constructing terms from index:"
					+ ioe);
		}
  		bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));

  		return bq;