FileDocCategorySizeDatePackage
QueryScorer.javaAPI DocApache Lucene 2.1.04766Wed Feb 14 10:46:22 GMT 2007org.apache.lucene.search.highlight

QueryScorer

public class QueryScorer extends Object implements Scorer
{@link Scorer} implementation which scores text fragments by the number of unique query terms found. This class uses the {@link QueryTermExtractor} class to process determine the query terms and their boosts to be used.
author
mark@searcharea.co.uk

Fields Summary
TextFragment
currentTextFragment
HashSet
uniqueTermsInFragment
float
totalScore
float
maxTermWeight
private HashMap
termsToFind
Constructors Summary
public QueryScorer(Query query)

param
query a Lucene query (ideally rewritten using query.rewrite before being passed to this class and the searcher)

	

	                    	 
	  
	
		this(QueryTermExtractor.getTerms(query));
	
public QueryScorer(Query query, String fieldName)

param
query a Lucene query (ideally rewritten using query.rewrite before being passed to this class and the searcher)
param
fieldName the Field name which is used to match Query terms

		this(QueryTermExtractor.getTerms(query, false,fieldName));
	
public QueryScorer(Query query, IndexReader reader, String fieldName)

param
query a Lucene query (ideally rewritten using query.rewrite before being passed to this class and the searcher)
param
reader used to compute IDF which can be used to a) score selected fragments better b) use graded highlights eg set font color intensity
param
fieldName the field on which Inverse Document Frequency (IDF) calculations are based

		this(QueryTermExtractor.getIdfWeightedTerms(query, reader, fieldName)); 
	
public QueryScorer(WeightedTerm[] weightedTerms)

		termsToFind = new HashMap();
		for (int i = 0; i < weightedTerms.length; i++)
		{
			WeightedTerm existingTerm=(WeightedTerm) termsToFind.get(weightedTerms[i].term);
			if( (existingTerm==null) ||(existingTerm.weight<weightedTerms[i].weight) )
			{
				//if a term is defined more than once, always use the highest scoring weight
				termsToFind.put(weightedTerms[i].term,weightedTerms[i]);
				maxTermWeight=Math.max(maxTermWeight,weightedTerms[i].getWeight());
			}
		}
	
Methods Summary
public voidallFragmentsProcessed()

		//this class has no special operations to perform at end of processing
	
public floatgetFragmentScore()

		return totalScore;		
	
public floatgetMaxTermWeight()

return
The highest weighted term (useful for passing to GradientFormatter to set top end of coloring scale.

        return maxTermWeight;
    
public floatgetTokenScore(org.apache.lucene.analysis.Token token)

		String termText=token.termText();
		
		WeightedTerm queryTerm=(WeightedTerm) termsToFind.get(termText);
		if(queryTerm==null)
		{
			//not a query term - return
			return 0;
		}
		//found a query term - is it unique in this doc?
		if(!uniqueTermsInFragment.contains(termText))
		{
			totalScore+=queryTerm.getWeight();
			uniqueTermsInFragment.add(termText);
		}
		return queryTerm.getWeight();
	
public voidstartFragment(TextFragment newFragment)

		uniqueTermsInFragment = new HashSet();
		currentTextFragment=newFragment;
		totalScore=0;