FileDocCategorySizeDatePackage
SpanNearClauseFactory.javaAPI DocApache Lucene 1.95222Mon Feb 20 09:17:42 GMT 2006org.apache.lucene.queryParser.surround.query

SpanNearClauseFactory

public class SpanNearClauseFactory extends Object
Copyright 2005 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
private IndexReader
reader
private String
fieldName
private HashMap
weightBySpanQuery
private BasicQueryFactory
qf
Constructors Summary
public SpanNearClauseFactory(IndexReader reader, String fieldName, BasicQueryFactory qf)

    this.reader = reader;
    this.fieldName = fieldName;
    this.weightBySpanQuery = new HashMap(); 
    this.qf = qf;
  
Methods Summary
public voidaddSpanNearQuery(org.apache.lucene.search.Query q)

    if (q == SrndQuery.theEmptyLcnQuery)
      return;
    if (! (q instanceof SpanNearQuery))
      throw new AssertionError("Expected SpanNearQuery: " + q.toString(getFieldName()));
    /* CHECKME: wrap in Hashable...? */
    addSpanQueryWeighted((SpanNearQuery)q, q.getBoost());
  
protected voidaddSpanQueryWeighted(org.apache.lucene.search.spans.SpanQuery sq, float weight)

    Float w = (Float) weightBySpanQuery.get(sq);
    if (w != null)
      w = new Float(w.floatValue() + weight);
    else
      w = new Float(weight);
    weightBySpanQuery.put(sq, w); 
  
public voidaddTermWeighted(org.apache.lucene.index.Term t, float weight)

   
    SpanTermQuery stq = qf.newSpanTermQuery(t);
    /* CHECKME: wrap in Hashable...? */
    addSpanQueryWeighted(stq, weight);
  
public voidclear()

weightBySpanQuery.clear();
public BasicQueryFactorygetBasicQueryFactory()

return qf;
public java.lang.StringgetFieldName()

return fieldName;
public org.apache.lucene.index.IndexReadergetIndexReader()

return reader;
public org.apache.lucene.index.TermEnumgetTermEnum(java.lang.String termText)

    return getIndexReader().terms(new Term(getFieldName(), termText));
  
public org.apache.lucene.search.spans.SpanQuerymakeSpanNearClause()

    SpanQuery [] spanQueries = new SpanQuery[size()];
    Iterator sqi = weightBySpanQuery.keySet().iterator();
    int i = 0;
    while (sqi.hasNext()) {
      SpanQuery sq = (SpanQuery) sqi.next();
      sq.setBoost(((Float)weightBySpanQuery.get(sq)).floatValue());
      spanQueries[i++] = sq;
    }
    
    /* CHECKME: Does the underlying implementation of SpanQuery need sorting? */
    if (false) /* true when sorting needed */
      Arrays.sort(spanQueries, new Comparator() { 
        public int compare(Object o1, Object o2) {
          SpanQuery sq1 = (SpanQuery) o1;
          SpanQuery sq2 = (SpanQuery) o2;
          /* compare the text of the first term of each SpanQuery */
          return  ((Term)sq1.getTerms().iterator().next()).text().compareTo(
                  ((Term)sq2.getTerms().iterator().next()).text());
        }
        public boolean equals(Object o) {return false;}
      });
       
    if (spanQueries.length == 1)
      return spanQueries[0];
    else
      return new SpanOrQuery(spanQueries);
  
public intsize()

return weightBySpanQuery.size();