I'm trying to get a query-time boost on recent items in a Compass index. I have tried using a property on my class mapping, but this only seems to affect the boost during index-time, not query-time.
Any ideas?
DefaultCompassQuery query = (DefaultCompassQuery) compassBuilderQuery.toQuery();
query.setTypes( types.toArray( new Class[types.size()] ) );
LuceneSearchEngineQuery searchEngineQuery = (LuceneSearchEngineQuery) query.getSearchEngineQuery();
final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
final long timeInMillis = Calendar.getInstance().getTimeInMillis();
ValueSourceQuery valSrcQuery = new ValueSourceQuery( new ValueSource() {
private static final long serialVersionUID = 1L;
@Override
public int hashCode() {
return System.identityHashCode( this );
}
@Override
public DocValues getValues( final IndexReader reader ) throws IOException {
return new DocValues() {
@Override
public float floatVal( int doc ) {
try {
Document document = reader.document( doc );
Field field = document.getField( "date" );
if (null != field) {
Date parse = sdf.parse( field.stringValue() );
long t = timeInMillis - parse.getTime();
float f = (1.0f / (t * (1.0f / TimeUnit.DAYS.toMillis( 30 )) + 1.0f));
if (logger.isDebugEnabled()) {
logger.debug( "Date match: " + parse.toString() );
logger.debug( "Calculated date boost as: " + f + " for doc id: " + doc );
}
return f;
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return 1.0f;
}
@Override
public String toString( int doc ) {
return description() + "=" + strVal( doc );
}
};
}
@Override
public boolean equals( Object o ) {
return this == o;
}
@Override
public String description() {
return "[boost: date]";
}
} );
CustomScoreQuery sq = new CustomScoreQuery( searchEngineQuery.getQuery(), valSrcQuery );
searchEngineQuery.setQuery( sq );
Where 'date' is a date field on your document that you want to boost and compassBuilderQuery is a query you have generated with the compass query builder. You can also tweak the '30' up there to make the dates boosted less or more based on their age.