Serialize indexed objects
Describes how to customize the serialization of indexed objects in Optimizely Search & Navigation.
Serialization lets you change data into a format that can be saved or sent elsewhere.
In the context of the .NET API for Optimizely, JSON.NET turns objects into a format that can be indexed. When using the Client class, it automatically makes adjustments to how objects are serialized, which you can customize further.
This means you can choose which parts of the data to include or exclude, and decide how different parts of the data are handled during serialization.
You can customize it in two ways:
- By using attributes – you can apply common customizations to classes you can modify.
- By customizing conventions used by the
Client– you can do powerful things like customizing multiple classes simultaneously and including return values from extension methods without modifying the classes of the serialized objects.
Example 1 – Include a specific field for indexing
The first example shows a ProductPage class.
When managing data indexing, standard properties are typically included by default, negating the need for explicit inclusion attributes. However, for complex objects or the return values of extension methods, custom conventions may be necessary to ensure their proper indexing.
Unlike explicit exclusion, a direct Include attribute is less common for standard properties, as their indexing is often governed by default serialization rules or established conventions. Control over a property's indexing behavior, such as ensuring ProductName is indexed with specific characteristics, is usually achieved through these conventions or by configuring serialization settings.
This example focuses on the more frequently encountered scenario of explicitly excluding properties from indexing.
// Assume you have a content type like this:
public class ProductPage : PageData
{
public virtual string ProductName { get; set; }
public virtual string ProductDescription { get; set; }
public virtual decimal Price { get; set; }
// This property will be indexed by default.
public virtual string Manufacturer { get; set; }
}Example 2 – Exclude a specific field from indexing
The following example demonstrates how to exclude fields like CustomerEmail and CreditCardNumber from indexing using the [JsonIgnore] attribute in a CustomerProfilePage class.
- In the
CustomerProfilePageclass, theCustomerEmailandCreditCardNumberproperties are decorated with the[JsonIgnore]attribute. - When Optimizely Search & Navigation serializes an instance of
CustomerProfilePagefor indexing, these properties are ignored and are not stored in the search index. This is useful for preventing sensitive data from being searchable or for optimizing the index by excluding irrelevant data.
// If you have a property that contains sensitive data or data that is not relevant for search,
// you can exclude it from being indexed using JSON.NET attributes.
using Newtonsoft.Json;
using EPiServer.Core;
using EPiServer.DataAnnotations;
public class CustomerProfilePage : PageData
{
[CultureSpecific]
public virtual string CustomerName { get; set; }
[CultureSpecific]
public virtual string CustomerAddress { get; set; }
// Exclude the Email property from being indexed
[JsonIgnore]
public virtual string CustomerEmail { get; set; }
// Exclude the CreditCardNumber property from being indexed for security reasons
[JsonIgnore]
public virtual string CreditCardNumber { get; set; }
[CultureSpecific]
public virtual string Interests { get; set; }
}Updated 3 days ago