How to Add a Recency Scorer

A Recency Scorer lets you understand the average age of the content you are returning.

  1. Go to the Scorers page and choose Add New to create the new Scorer.
  2. Give it a meaningful name like Recency or Age of Product that works for you.
  3. For the actual JavaScript code, start with this basic scorer:
const k = 10; // @Rank
let rank = 0;
let score = 0;
baseDate = new Date("2023-08-15").getTime();
eachDoc(function(doc, i) {
  docDate = doc['publish_date'];
  const diffTime = (baseDate - new Date(docDate).getTime());
  const diff = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  score = score + diff;
  rank = rank + 1
}, k);
score = rank > 0 ? score / rank : 0.0;
setScore(score);
  1. We need to specify what field in the docs has the date field. Change the publish_date to your date field.
  2. In order to calculate a recency score, we have to decide what is out of date, and that is the baseDate. In this example, we have a "frozen" index, so we hard code the base date to 2023-08-15. To use today as your date, you can use Date.now().
  3. Since this is a calculated scorer, not one based on ratings, you can ignore the scale settings for this scorer.

  1. Go to your Case and choose Select scorer.

  2. You should see the Recency scorer you created listed, pick it for your case and it will run.

  3. If you get 0's, make sure that in the field spec you have the date field included, in our example it is publish_doc.

  4. If you are using Solr, you can test out the scorer by forcing it to sort by recency by adding &sort=date_released desc.

  5. The lower the score, the more recent the average of the docs being returned.