public Filter getFilter(Element e) throws ParserException {
TermsFilter tf = new TermsFilter();
String text = DOMUtils.getNonBlankTextOrFail(e);
String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
TokenStream ts = analyzer.tokenStream(fieldName, new StringReader(text));
TermAttribute termAtt = ts.addAttribute(TermAttribute.class);
try
{
Term term = null;
while (ts.incrementToken()) {
if (term == null)
{
term = new Term(fieldName, termAtt.term());
} else
{
// create from previous to save fieldName.intern overhead
term = term.createTerm(termAtt.term());
}
tf.addTerm(term);
}
}
catch (IOException ioe)
{
throw new RuntimeException("Error constructing terms from index:"
+ ioe);
}
return tf;
}
|