Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely Search & Navigation
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

This topic explains what indexing is and what happens when objects are indexed, and how to modify the indexing, for instance with regards to availability and identity of indexed documents. 

How it works

Indexing is the process of sending an object to the Find service for storage and analysis, so it can be retrieved as search results. If a document of the same type and ID already exists, it is overwritten. The client API supports the indexing of any .NET object.

As objects are serialized upon indexing, the only restriction is that indexed objects can be serialized. If for some reason, typically circular references, an object cannot be serialized, the API supports customizing how an object is serialized and indexed. This flexibility can also be used to implement functionality, such as indexing the return value of extension methods.

Indexing objects

Indexing is done via the Index method, exposed by the IClient interface. If you have an instance of a Client and an object to index, you can index using this code.

C#
IClient client = //A client retrieved from config or injected into the method
BlogPost blogPost = //An instance of an arbitrary class

client.Index(blogPost);

You can index several objects in a batch.

C#
BlogPost blogPost = //An instance of an arbitrary class
Article article = //An instance of another arbitrary class

//Indexing supplying objects as params
client.Index(blogPost, article);

var listOfObjects = new List<object>
                    {
                        blogPost,
                        article
                    };
//Indexing supplying IEnumerable
client.Index(listOfObjects);

Once an object is indexed, an instance of the IndexResult class is returned. You can use that class to verify that the indexing was successful and retrieve the document ID.

C#
var result = client.Index(blogPost);
bool succesfull = result.Ok;
string id = result.Id;

Time delay

After an object is indexed, it is instantly available for retrieval via the Client’s Get method. However, before the object is returned in search results, the index must be refreshed. This happens automatically every second. However, if it is crucial that an object be available immediately, modify the client command that tells the service to refresh the index. Only do this if really necessary (and preferably only while testing or debugging), since it can negatively affect performance.

C#
client.Index(blogPost, 
        x => x.Refresh = true);

Identity

Unless specified, the service automatically assigns an ID to an indexed document. To explicitly specify an ID, you can either modify the command or annotate a property on the indexed class with the ID attribute. In both cases, the ID's value must be compatible with the DocumentID type.

C#
//Specifying the id by modifying the command
client.Index(blogPost, 
        x => x.Id = 42);

//Specifying that a property should be used as id
public class BlogPost
{
    [Id]
    public int Id { get; set; }
}

You can also modify the Client class conventions to use a specific property or method as the ID for all instances of a type without modifying the actual class.

C#
client.Conventions.ForInstancesOf<Product>()
    .IdIs(x => x.Key);

Ignoring properties

To exclude individual properties in a class from being indexed, annotate them with the JsonIgnore attribute. You can also exclude properties without modifying their classes via Client class conventions.

C#
public class BlogPost
{
    [JsonIgnore]
    public int SomethingInternal { get; set; }
}

Time to live

You can delete individual documents after a designated time period via the "time to live" functionality. To set this, either modify the command or annotate a property on the indexed class with the TimeToLive attribute.

C#
//Specifying the time to live by modifying the command
client.Index(blogPost, 
        x => x.TimeToLive =  new TimeSpan.FromDays(14));

//Specifying that a property should be used as time to live
public class BlogPost
{
     [Id]
     public int Id { get; set; }

     [TimeToLive]
     public TimeToLive TimeToLive { get; set; }
}

The granularity of time to live is 60 seconds. This means that documents are deleted within 60 seconds of the actual time to live.

Customizing type indexing

There are several ways to customize how type is serialized and indexed. You can exclude properties, remove HTML tags in string properties, and include return values of methods so they can be used later when searching or filtering.

Updating a single field

With Episerver Find, you can update a single field if you have the indexed item's ID.

C#
client.Update<BlogPost>(Id).Field(x => x.PublishDate, newTime).Execute();
Do you find this information helpful? Please log in to provide feedback.

Last updated: Nov 16, 2015

Recommended reading