1 package org.apache.lucene.xmlparser.builders;
3 import java.io.IOException;
4 import java.io.StringReader;
6 import org.apache.lucene.analysis.Analyzer;
7 import org.apache.lucene.analysis.TokenStream;
8 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.search.BooleanClause;
11 import org.apache.lucene.search.BooleanQuery;
12 import org.apache.lucene.search.Query;
13 import org.apache.lucene.search.TermQuery;
14 import org.apache.lucene.xmlparser.DOMUtils;
15 import org.apache.lucene.xmlparser.ParserException;
16 import org.apache.lucene.xmlparser.QueryBuilder;
17 import org.w3c.dom.Element;
20 * Licensed to the Apache Software Foundation (ASF) under one or more
21 * contributor license agreements. See the NOTICE file distributed with
22 * this work for additional information regarding copyright ownership.
23 * The ASF licenses this file to You under the Apache License, Version 2.0
24 * (the "License"); you may not use this file except in compliance with
25 * the License. You may obtain a copy of the License at
27 * http://www.apache.org/licenses/LICENSE-2.0
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
36 * Builds a BooleanQuery from all of the terms found in the XML element using the choice of analyzer
38 public class TermsQueryBuilder implements QueryBuilder {
43 public TermsQueryBuilder(Analyzer analyzer)
45 this.analyzer = analyzer;
50 public Query getQuery(Element e) throws ParserException {
52 String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
53 String text=DOMUtils.getNonBlankTextOrFail(e);
55 BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
56 bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
59 TokenStream ts = analyzer.reusableTokenStream(fieldName, new StringReader(text));
60 CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
63 while (ts.incrementToken()) {
66 term = new Term(fieldName, termAtt.toString());
69 // create from previous to save fieldName.intern overhead
70 term = term.createTerm(termAtt.toString());
72 bq.add(new BooleanClause(new TermQuery(term),BooleanClause.Occur.SHOULD));
77 catch (IOException ioe)
79 throw new RuntimeException("Error constructing terms from index:"
82 bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));