Decrease score over time

Hi.

I've got a 'post' index, which has a 'createdAt' date field.

I'm trying to create a query that will return all posts, however, it should apply negative scoring for every day from now to the past.

So, if a post was created today, the scoring would be unaffected, but a post created for example 5 days ago, should have -5 penalty applied. So for every day it would have an extra -1 score applied.

I've tried to achieve this with "linear" decay function but it seems to cover only a very specific date range and I'm rather looking for something more open.

Thanks in advance

If you shared your existing query it might help someone to offer alternatives or improvements.

Thanks

Here's the query I have so far:

GET /post/_doc/_search
{
  "explain": false,
  "query": {
    "function_score": {
      "score_mode": "sum",
      "functions": [
        {
            "field_value_factor": {
              "field": "totalVotes"
            },
            "weight": 0.1
        },
        {
            "field_value_factor": {
              "field": "score"
            },
            "weight": 0.05
        },
        {
            "field_value_factor": {
              "field": "totalComments"
            },
            "weight": 0.5
        },
        {
          "filter": {"match": { "hot": true }},
          "weight": 0
        },
        {
          "linear": {
            "createdAt": {
              "scale": "1h",
              "offset": 0,
              "decay": 0.1
            }
          },
          "weight": 100
        }
      ]
    }
  }
}    

Everything apart from the linear function seems to work fine. Just need to find a way to prioritize posts based on their number of days since they're added.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.