Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/javasoze/bobo
Browse files Browse the repository at this point in the history
  • Loading branch information
ymatsuda committed Jun 8, 2010
2 parents 573eef8 + 2df385e commit 3a66ea0
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.browseengine.bobo.query;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Scorer;

import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.facets.data.FacetDataCache;
import com.browseengine.bobo.facets.data.TermLongList;
import com.browseengine.bobo.util.BigSegmentedArray;

public class RecencyBoostScorerBuilder implements ScorerBuilder {

private final float _maxFactor;
private final TimeUnit _timeunit;
private final float _min;
private final float _max;
private final long _cutoffInMillis;
private final float _A;
private final String _timeFacetName;
private final long _now;

public RecencyBoostScorerBuilder(String timeFacetName,float maxFactor,long cutoff,TimeUnit timeunit) {
_timeFacetName = timeFacetName;
_maxFactor = maxFactor;
_min = 1.0f;
_max = _maxFactor + _min;
_timeunit = timeunit;
_cutoffInMillis = _timeunit.toMillis(cutoff);
_A = (_min - _max) / (((float)_cutoffInMillis)*((float)_cutoffInMillis));
_now = System.currentTimeMillis();
}

public Explanation explain(IndexReader reader, int doc,
Explanation innerExplaination) throws IOException {
if (reader instanceof BoboIndexReader){
BoboIndexReader boboReader = (BoboIndexReader)reader;
Object dataObj = boboReader.getFacetData(_timeFacetName);
if (dataObj instanceof FacetDataCache<?>){
FacetDataCache<Long> facetDataCache = (FacetDataCache<Long>)(boboReader.getFacetData(_timeFacetName));
final BigSegmentedArray orderArray = facetDataCache.orderArray;
final TermLongList termList = (TermLongList)facetDataCache.valArray;
final long now = System.currentTimeMillis();
Explanation finalExpl = new Explanation();
finalExpl.addDetail(innerExplaination);
float rawScore = innerExplaination.getValue();
long timeVal = termList.getRawValue(orderArray.get(doc));
float timeScore = computeTimeFactor(timeVal);
float finalScore = combineScores(timeScore,rawScore);
finalExpl.setValue(finalScore);
finalExpl.setDescription("final score = (time score: "+timeScore+") * (raw score: "+rawScore+"), timeVal: "+timeVal);
return finalExpl;
}
else{
throw new IllegalStateException("underlying facet data must be of type FacetDataCache<Long>");
}
}
else{
throw new IllegalStateException("reader not instance of "+BoboIndexReader.class);
}
}

public Scorer createScorer(final Scorer innerScorer, IndexReader reader,
boolean scoreDocsInOrder, boolean topScorer) throws IOException {
if (reader instanceof BoboIndexReader){
BoboIndexReader boboReader = (BoboIndexReader)reader;
Object dataObj = boboReader.getFacetData(_timeFacetName);
if (dataObj instanceof FacetDataCache<?>){
FacetDataCache<Long> facetDataCache = (FacetDataCache<Long>)(boboReader.getFacetData(_timeFacetName));
final BigSegmentedArray orderArray = facetDataCache.orderArray;
final TermLongList termList = (TermLongList)facetDataCache.valArray;
return new Scorer(innerScorer.getSimilarity()){


@Override
public float score() throws IOException {
float rawScore = innerScorer.score();
long timeVal = termList.getRawValue(orderArray.get(innerScorer.docID()));
float timeScore = computeTimeFactor(timeVal);
return combineScores(timeScore,rawScore);
}

};
}
else{
throw new IllegalStateException("underlying facet data must be of type FacetDataCache<Long>");
}
}
else{
throw new IllegalStateException("reader not instance of "+BoboIndexReader.class);
}
}

private float computeTimeFactor(long timeVal){
long xVal = _now - timeVal;
if (xVal > _cutoffInMillis){
return _min;
}
else{
float xValFloat = (float)xVal;
return _A*xValFloat*xValFloat+_max;
}
}

private static float combineScores(float timeScore,float rawScore){
return timeScore * rawScore;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.browseengine.bobo.query;

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Weight;

public abstract class ScoreAdjusterQuery extends Query
{
private static final long serialVersionUID = 1L;

private class ScoreAdjusterWeight extends Weight
{
private static final long serialVersionUID = 1L;

Weight _innerWeight;

public ScoreAdjusterWeight(Weight innerWeight) throws IOException
{
_innerWeight = innerWeight;
}

public String toString()
{
return "weight(" + ScoreAdjusterQuery.this + ")";
}

public Query getQuery()
{
return _innerWeight.getQuery();
}

public float getValue()
{
return _innerWeight.getValue();
}

public float sumOfSquaredWeights() throws IOException
{
return _innerWeight.sumOfSquaredWeights();
}

public void normalize(float queryNorm)
{
_innerWeight.normalize(queryNorm);
}

@Override
public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException
{
Scorer innerScorer = _innerWeight.scorer(reader, scoreDocsInOrder, topScorer);
return _scorerBuilder.createScorer(innerScorer, reader, scoreDocsInOrder, topScorer);
}

public Explanation explain(IndexReader reader, int doc) throws IOException
{
Explanation innerExplain = _innerWeight.explain(reader, doc);
return _scorerBuilder.explain(reader, doc,innerExplain);
}
}

protected final Query _query;
protected final ScorerBuilder _scorerBuilder;
public ScoreAdjusterQuery(Query query,ScorerBuilder scorerBuilder)
{
_query = query;
_scorerBuilder = scorerBuilder;
}

@Override
public Weight createWeight(Searcher searcher) throws IOException
{
return new ScoreAdjusterWeight(_query.createWeight(searcher));
}

@Override
public Query rewrite(IndexReader reader) throws IOException
{
_query.rewrite(reader);
return this;
}

@Override
public String toString(String field)
{
return _query.toString(field);
}
}
12 changes: 12 additions & 0 deletions bobo-browse/src/com/browseengine/bobo/query/ScorerBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.browseengine.bobo.query;

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Scorer;

public interface ScorerBuilder {
Scorer createScorer(Scorer innerScorer, IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException;
Explanation explain(IndexReader reader,int doc,Explanation innerExplaination) throws IOException;
}

0 comments on commit 3a66ea0

Please sign in to comment.