Sana Assistant (online)
Table of Contents

ProductIndexExtension reference

This article provides reference material about ProductIndexExtension class. All product indexing extensions have to be inherited from the ProductIndexExtension which, in turn, inherits from the core Sana Extension class.

ProductIndexExtension class

Methods

Note

All code examples may contain nonexistent methods or properties. They are used as declarations only and should be created if they fit the designed extension or deleted if they do not.

AddToIndexAsync

Adds the product document to the index.

This method should be used to convert the Sana product document with fields to external search document and add it then to external search index. ProductIndexContext can be used there to get additional information required for documents conversion.

Example:

public override Task AddToIndexAsync(
    ProductIndexContext context,
    ProductIndexDocument document,
    CancellationToken cancellationToken = default)
{
    var convertedDocument = Convert(document, context);
    return ExternalSearchService.AddDocumentAsync(context.IndexId, convertedDocument, cancellationToken);
}

CompleteAsync

Completes product indexing.

Should be used to release resources owned by extension during the indexing if applicable or store index additional information.

Example:

public override async Task CompleteAsync(
    ProductIndexContext context,
    CancellationToken cancellationToken = default)
{
    await DeleteTemporaryFilesAsync(context, cancellationToken);
        
    var websiteId = Api.WebsiteId;
    await UpdateWebsiteIndexPathAsync(Api.WebsiteId, context.IndexId, cancellationToken);

    var currentIndexId = await GetCurrentWebsiteIndexIdAsync(websiteId, cancellationToken);
    if (currentIndexId != context.IndexId)
        await DeleteOldIndexAsync(currentIndexId, cancellationToken);

    Api.Cache.ClearAll();
}

CreateLoadOptions

Creates the products load options.

Example:

public override ProductIndexLoadOptions CreateLoadOptions() => new(
    LoadPrices: true,
    LoadStock: true);

DeleteFromIndexAsync

Deletes the product from index.

Example:

public override Task DeleteFromIndexAsync(
    ProductIndexContext context,
    string id,
    CancellationToken cancellationToken = default)
{
    return ExternalSearchService.DeleteAsync(context.IndexId, id, cancellationToken);
}

FlushAsync

Flushes current changes from memory to physical storage.

Can be left empty if changes are not stored in memory but instead directly saved to external search service.

Example:

public override Task FlushAsync(
    ProductIndexContext context,
    CancellationToken cancellationToken = default)
{
    return ExternalSearchService.FlushAsync(context.IndexId, cancellationToken);
}

InitializeContextAsync

Initializes the product indexing context.

The product index context state can be initialized there which can be used then during products indexing.

Example:

public override Task InitializeContextAsync(
    ProductIndexContext context,
    CancellationToken cancellationToken = default)
{
    var httpClient = new HttpClient { BaseAddress = new("https://external.search.com") };
    context.State = new IndexContext(httpClient);

    return Task.CompleteResult;
}

public override async Task FlushAsync(
    ProductIndexContext context,
    CancellationToken cancellationToken = default)
{
    var indexContext = (IndexContext)context.State;
    using var response = await indexContext.Client.PostAsync("/flush", content: null, cancellationToken);

    if (response.StatusCode != HttpStatusCode.OK)
        throw new InvalidOperationException();
}

sealed record IndexContext(HttpClient Client) : IDisposable
{
    public bool CategoriesIndexed { get; set; }

    public bool HasChanges { get; set; }

    public void Dispose() => Client.Dispose();
}

OptimizeAsync

Optimizes the index physical storage.

Should be used to optimize physical storage structure to speed up searching if applicable.

Example:

public override Task OptimizeAsync(
    ProductIndexContext context,
    CancellationToken cancellationToken = default)
{
    return ExternalSearchService.OptimizeAsync(context.IndexId, cancellationToken);
}

See also