Skip to content

Instantly share code, notes, and snippets.

@zimdo
Created May 21, 2012 03:18
Show Gist options
  • Save zimdo/2760423 to your computer and use it in GitHub Desktop.
Save zimdo/2760423 to your computer and use it in GitHub Desktop.
AgeFieldSource
public class AgeFieldSource extends FieldCacheSource {
private int now;
public AgeFieldSource(String field) {
super(field);
now = (int)(System.currentTimeMillis() / 1000);
}
@Override
public boolean cachedFieldSourceEquals(FieldCacheSource other) {
return other.getClass() == MyFieldSource.class;
}
@Override
public int cachedFieldSourceHashCode() {
return Integer.class.hashCode();
}
@Override
public DocValues getCachedFieldValues(FieldCache cache, String field, IndexReader reader) throws IOException {
int[] times = cache.getInts(reader, field);
float[] weights = new float[times.length];
for (int i=0; i<times.length; i++) {
// Here be the nuts and bolts
weights[i] = new Double(1/Math.log((now - times[i]) / 3600)).floatValue();
}
final float[] arr = weights;
return new DocValues() {
public float floatVal(int doc) {
return (float) arr[doc];
}
public int intVal(int doc) {
return (int) arr[doc];
}
public String toString(int doc) {
return description() + '=' + intVal(doc);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment