AI Hints
Home›Elasticsearch Tutorial›Custom Scoring and Typos
Elasticsearch · Lesson 65 of 120

Decay Functions

Newer is better.

⏱ 2 min read▶ Video at 1:01:36🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Customers like new products, but an older product that matches well should still show up. A decay function slowly lowers the score as a value gets further from an ideal, like today's date.

  • Prefer new
  • Don't hide old
  • Decay

Newer first, gently

The gauss function centres on the first of November twenty twenty five. Scale of ninety days means a product that much older or newer gets half the boost. Run it. Recently added backpacks rise to the top.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source
{
  "query": { "function_score": {
    "query": { "match": { "category": "Backpacks" } },
    "gauss": { "added": { "origin": "2025-11-01", "scale": "90d" } } } },
  "_source": ["name", "added"],
  "size": 3
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {
          "name": "Trek 45L Hiking Backpack",
          "added": "2025-05-05"
        }
      },
      {
        "_source": {
          "name": "Carry-On 35L Travel Pack",
          "added": "2025-02-14"
        }
      },
      {
        "_source": {
          "name": "Daypack 22L Hiking Backpack",
          "added": "2025-01-07"
        }
      }
    ]
  }
}
Also uses _load
catalog

Decay shapes

Gauss falls off smoothly, like a bell. Linear falls off in a straight line, to zero. Exp falls fast at first, then slowly. Use them for dates, prices, and even distance from a store.

keyvalue
gausssmooth bell curve
linearstraight line to zero
expfast, then slow
Works ondates, numbers, geo

Summary

Decay functions gently lower the score as a value moves away from an ideal, like now, or a target price. Next, handling typos.

Key takeaways
  • gauss
  • origin
  • scale
Next up: Fuzzy Matching — typos still work