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

TermsFilterBuilder

public class TermsFilterBuilder extends Object implements org.apache.lucene.xmlparser.FilterBuilder
author
maharwood

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

param
analyzer

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

		TermsFilter tf=new TermsFilter();
		NodeList nl = e.getElementsByTagName("Field");
		for(int i=0;i<nl.getLength();i++)
		{
			Element fieldElem=(Element) nl.item(i);
			String fieldName=DOMUtils.getAttributeWithInheritance(fieldElem,"fieldName");
			
			if(fieldName==null)
			{
				throw new ParserException("TermsFilter missing \"fieldName\" element");				
			}
			String text=DOMUtils.getText(fieldElem).trim();
			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
				{
					term=term.createTerm(token.termText()); //create from previous to save fieldName.intern overhead
				}
				tf.addTerm(term);
				token=ts.next();
			}
			}
			catch(IOException ioe)
			{
				throw new RuntimeException("Error constructing terms from index:"+ioe);
			}
		}
		return tf;