pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / xml-query-parser / src / java / org / apache / lucene / xmlparser / builders / TermsQueryBuilder.java
1 package org.apache.lucene.xmlparser.builders;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5
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;
18
19 /**
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
26  *
27  *     http://www.apache.org/licenses/LICENSE-2.0
28  *
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.
34  */
35 /**
36  * Builds a BooleanQuery from all of the terms found in the XML element using the choice of analyzer
37  */
38 public class TermsQueryBuilder implements QueryBuilder {
39
40         Analyzer analyzer;
41
42                 
43         public TermsQueryBuilder(Analyzer analyzer)
44         {
45                 this.analyzer = analyzer;
46         }
47
48
49
50         public Query getQuery(Element e) throws ParserException {
51                 
52         String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
53                 String text=DOMUtils.getNonBlankTextOrFail(e);
54                 
55                 BooleanQuery bq=new BooleanQuery(DOMUtils.getAttribute(e,"disableCoord",false));
56                 bq.setMinimumNumberShouldMatch(DOMUtils.getAttribute(e,"minimumNumberShouldMatch",0));
57                 try
58                 {
59             TokenStream ts = analyzer.reusableTokenStream(fieldName, new StringReader(text));
60       CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
61                         Term term = null;
62       ts.reset();
63                         while (ts.incrementToken()) {
64                                 if (term == null)
65                                 {
66                                         term = new Term(fieldName, termAtt.toString());
67                                 } else
68                                 {
69 //                                       create from previous to save fieldName.intern overhead
70                                         term = term.createTerm(termAtt.toString()); 
71                                 }
72                                 bq.add(new BooleanClause(new TermQuery(term),BooleanClause.Occur.SHOULD));
73                         }
74                         ts.end();
75                         ts.close();
76                 } 
77                 catch (IOException ioe)
78                 {
79                         throw new RuntimeException("Error constructing terms from index:"
80                                         + ioe);
81                 }
82                 bq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
83
84                 return bq;
85                 
86         }
87
88 }