Image via Wikipedia
ValueSource document_date =
new TrieDateFieldSource("document_date_field",
FieldCache.NUMERIC_UTILS_LONG_PARSER);
/* ValueSource that calculates the number of miliseconds
* between the document_date (e.g. blog publication date)
* and the present time, i.e. now.
ValueSource vs = new DualFloatFunction(
new LongConstValueSource(now), document_date) {
private static final long serialVersionUID = 1L;
protected String name() { return "ms"; }
protected float func(int doc, DocValues aVals,
DocValues bVals) {
return now - bVals.longVal(doc);
}
};
/* ReciprocalFloatFunction implements a reciprocal
* function f(x) = a/(mx+b), based on the float value
* of a field or function as exported by ValueSource vs.
* Values m, a, and b are float constants. */
ValueSource recip = new ReciprocalFloatFunction(vs, m, a, b);
/* Boosting a given query with the reciprocal function recip */
Query boostedQuery = new BoostedQuery(query, recip);
Notes:
- "date" field type should be of the class "solr.TrieDateField" (introduced in Solr 1.4) in order for the above recipe to work.
- Unfortunately, org.apache.solr.search.LongConstValueSource and org.apache.solr.schema.TrieDateFieldSource classes are not public. I've expressed my wish to the Solr team to make this two classes public. Until they do, you should copy these two classes from Solr source code to your project.