The latest release of EPiServer Find contains, apart for a number of bug fixes, one new feature and that is the ability to set a time to live-value on indexed documents. The value is expressed as a TimeSpan and specifies how long the document should reside in the index before it is automatically deleted. This can be really useful if you index documents continuously (say that you index all items that users currently are looking at on your site) but only what to show the latest (i.e. what users are currently looking at on the site) and don’t want to flood your index over time (i.e I don’t care what someone looked at yesterday). Under these circumstances the time to live-feature can really help you by doing that dirty cleanup job that we all hate to to do.

How to use time to live

Given an object with a TimeToLive property (or TimeSpan):

public class WithTimeToLive
{
    [Id]
    public string Id { get; set; }

    public TimeToLive TimeToLive { get; set; }
}

We first need to configure the client and register the TimeToLive-property for the given object type:

client.Conventions.ForInstancesOf<WithTimeToLive>()
    .TimeToLiveIs(x => x.TimeToLive);

When indexing an object we simply register a TimeSpan value to the property specifying how long it should reside in the index:

indexObject = new WithTimeToLive()
    {
        Id = "123",
        TimeToLive = new TimeSpan(0, 5, 0)
    };

I hope you may find this useful when making your site awesome with EPiServer Find!

Note:

The granularity of time to live is 60 seconds meaning that the documents will be deleted within 60 seconds of the actual time to live.

Updated:

Instead of configuring the TimeToLive-property by the conventions it can be annotated by the TimeToLiveAttribute:

public class WithTimeToLive
{
    [Id]
    public string Id { get; set; }
    
    [TimeToLive]
    public TimeToLive TimeToLive { get; set; }
}

or passed in the index call:

client.Index(indexObjext, x => x.TimeToLive = new TimeSpan(0, 5, 0));