HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Decay functions for relevance scoring

Boost content relevance based on proximity to a target value, such as recent publish dates, using decay-based scoring in Graph queries.

Decay functions boost content that is closer to a target value—commonly a date—so that more recent content appears higher in search results. Use decay functions when recency affects relevance, such as in news feeds, blog listings, or activity streams.

Decay functions influence relevance scoring, not filtering. All items that match the query still appear in the results, but the scoring system ranks items closer to the target value higher.

Apply date-based decay

Use a DecayOptions object in a .Where() clause to apply decay to a date field. Content closer to the Origin value receives a higher relevance score. Content further away gradually loses influence based on the decay rate.

var result = await client
    .QueryContent<BlogPostPage>()
    .Where(x => x.StartPublish, new Optimizely.Graph.Cms.Query.Abstractions.DecayOptions
    {
        Origin = DateTime.UtcNow,
        Rate   = 0.3f,
        Scale  = 100
    })
    .GetAsContentAsync();

In this example, the query boosts blog posts that are closer to the current date.

DecayOptions parameters

The DecayOptions object defines how the scoring changes relative to the target value.

ParameterTypeDescription
OriginDateTimeThe reference value used to calculate distance. Items closest to this value receive the highest relevance score.
RatefloatThe rate at which relevance decreases as distance from Origin increases. Higher values cause the score to decrease faster.
ScaleintThe distance at which the score decays by the specified Rate. For date fields, the distance is measured in days.

Decay affects the relevance score, not the result set. All matching items are still returned they're just ranked with recency taken into account. Combine decay with .OrderBy() if you want to sort purely by date instead.

Combine decay with other query operations

Decay functions work together with filtering, searching, and pagination. You can combine them with other query methods to refine results while still boosting recent content.

var result = await client
    .QueryContent<BlogPostPage>()
    .Where(x => x.BlogCategory == "News")
    .Where(x => x.StartPublish, new DecayOptions
    {
        Origin = DateTime.UtcNow,
        Rate   = 0.5f,
        Scale  = 30
    })
    .Limit(20)
    .GetAsContentAsync();

Decay and sorting

Decay functions affect the relevance score, which influences ranking in search results. If you want to sort results strictly by a field value, use .OrderBy() instead.

For example, sorting purely by publication date:

var result = await client
    .QueryContent<BlogPostPage>()
    .OrderBy(x => x.StartPublish, OrderDirection.Descending)
    .GetAsContentAsync();

Use decay when you want recency to influence ranking while still considering other relevance signals, such as search matches or filters.