Recursive AI

Boost recent documents in C# search results

OneIndex is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

Use ScoreBy with date decay to add a freshness bonus to the text score.

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneIndex;
using OneIndex.Search;
 
var today = new DateTimeOffset(2026, 11, 25,
    0, 0, 0, TimeSpan.Zero);
using var index =
    SearchIndex.InMemory<Note>();
Seed(index, today);
var hits = index.Search("coffee").ScoreBy(score =>
{
    score.BoostMode = FunctionBoostMode.Sum;
    score.Decay(x => x.Published, DecayShape.Gauss,
        today, TimeSpan.FromDays(30));
}).Execute();
foreach (var hit in hits)
    Console.WriteLine(FormattableString.Invariant(
        $"{hit.Document.Title}: {hit.Score:F3}"));
 
static void Seed(SearchIndex<Note> index,
    DateTimeOffset today)
{
    index.Upsert([
        new("old", "coffee guide", today.AddDays(-30)),
        new("new", "coffee tips", today)
    ]);
    index.Commit();
}
 
public class Note(
    string id,
    string title,
    DateTimeOffset published)
{
    public Note() : this("", "", default) { }
 
    [SearchId]
    public string Id { get; set; } = id;
    [FullText]
    public string Title { get; set; } = title;
    [Filterable]
    public DateTimeOffset Published
        { get; set; } = published;
}

Unzip the example, then dotnet run.

The example uses a fixed date so the output is repeatable. In your app, use DateTimeOffset.UtcNow for today.

Text
coffee tips: 1.096
coffee guide: 0.596

This example uses OneIndex 0.4.0-preview.2. APIs and file formats may change between previews.

Mark the Published date with [Filterable] so OneIndex can read it when scoring each hit. Without [Filterable], [Sortable], or [Facet] on the date property, Decay throws ArgumentException.

Variations

To give everything published in the last week the same full bonus, replace the Decay call with:

C#
score.Decay(x => x.Published, DecayShape.Gauss,
    today, TimeSpan.FromDays(30),
    offset: TimeSpan.FromDays(7));
Text
coffee tips: 1.096
coffee guide: 0.761

Explain a search relevance score · Customize search ranking rules


The short answer

Call ScoreBy on the full-text query and add a date Decay. With FunctionBoostMode.Sum, the decay contributes a bonus to the original text score rather than replacing it. The two titles have the same text score for “coffee”; the document published on the origin date gets a bonus of 1, and the 30-day-old document gets 0.5.

How do you prioritize recent content without sorting search results by date?

A date sort answers “Which matching document is newest?” A freshness boost answers “Which match is most useful when both relevance and age matter?” If you’re searching a help center, a new article that mentions the error once shouldn’t automatically outrank an older article written specifically about that error. Adding the time-decay bonus keeps the difference in their text scores part of the decision. It doesn’t add documents to the match set or drop old ones unless you separately set a minimum score or a filter.

Choose a date that represents freshness to your reader. Publication time works for a news archive. For support articles, use the date of a substantive update if that’s what you want to reward, rather than the date of every import. Keep the scores sorted by relevance when using the bonus; reserve a date sort for a separate “Newest” option.

How does gradual decay work for a recency boost?

Pick an origin, usually DateTimeOffset.UtcNow, and a scale. With the defaults in the example, a Gaussian decay is 1 at the origin and 0.5 at the scale distance. A longer scale keeps the bonus larger farther from the origin. The optional offset gives dates close to the origin the same bonus before decay starts. In the seven-day variation, the bonus reaches 0.5 at 37 days rather than at 30 days.

The decay uses absolute distance from the origin: a date 30 days in the future gets the same bonus as a date 30 days in the past. If drafts or future-dated posts shouldn’t appear, filter them out explicitly. A missing date gets a decay value of 1, so supply a publication date for each document if you don’t want undated results to receive the full bonus. See function scores for the formulas and defaults.

All oneindex recipes