1 package org.apache.lucene.xmlparser.builders;
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.ArrayList;
7 import org.apache.lucene.analysis.Analyzer;
8 import org.apache.lucene.analysis.TokenStream;
9 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
10 import org.apache.lucene.index.Term;
11 import org.apache.lucene.search.spans.SpanOrQuery;
12 import org.apache.lucene.search.spans.SpanQuery;
13 import org.apache.lucene.search.spans.SpanTermQuery;
14 import org.apache.lucene.xmlparser.DOMUtils;
15 import org.apache.lucene.xmlparser.ParserException;
16 import org.w3c.dom.Element;
18 * Licensed to the Apache Software Foundation (ASF) under one or more
19 * contributor license agreements. See the NOTICE file distributed with
20 * this work for additional information regarding copyright ownership.
21 * The ASF licenses this file to You under the Apache License, Version 2.0
22 * (the "License"); you may not use this file except in compliance with
23 * the License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
37 public class SpanOrTermsBuilder extends SpanBuilderBase
45 public SpanOrTermsBuilder(Analyzer analyzer)
48 this.analyzer = analyzer;
50 public SpanQuery getSpanQuery(Element e) throws ParserException
52 String fieldName=DOMUtils.getAttributeWithInheritanceOrFail(e,"fieldName");
53 String value=DOMUtils.getNonBlankTextOrFail(e);
57 ArrayList<SpanQuery> clausesList=new ArrayList<SpanQuery>();
58 TokenStream ts=analyzer.reusableTokenStream(fieldName,new StringReader(value));
59 CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
62 while (ts.incrementToken()) {
63 SpanTermQuery stq=new SpanTermQuery(new Term(fieldName, termAtt.toString()));
68 SpanOrQuery soq=new SpanOrQuery(clausesList.toArray(new SpanQuery[clausesList.size()]));
69 soq.setBoost(DOMUtils.getAttribute(e,"boost",1.0f));
72 catch(IOException ioe)
74 throw new ParserException("IOException parsing value:"+value);