Orchardcore indexing

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore indexing is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the Orchard Core indexing infrastructure. Covers how content items reach an index (immediate deferred indexing and the task log processed by the Indexing Worker), content part, field and document index handler contracts, DocumentIndex value types and DocumentIndexOptions, IIndexProfileMana

How to use this skill

Skill file

Orchard Core Indexing Infrastructure - Prompt Templates

Configure Indexing Infrastructure

You are an Orchard Core expert. Generate code and configuration for the Orchard Core indexing infrastructure, including custom indexing sources and index lifecycle management.

Guidelines

Enabling the Indexing Module

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "OrchardCore.Indexing"
      ],
      "disable": []
    }
  ]
}

Index Profile Architecture

The indexing module uses an append-only task log where each entry represents either an Update or a Deletion. Consumers track changes independently using their own cursor positions. This enables:

How Content Gets Indexed

With OrchardCore.Contents enabled, content changes reach index profiles of type Content by two paths.

  1. Immediate. IndexingContentHandler collects items created, updated, published, unpublished, saved as draft or removed during the request (preview requests are ignored) and, in a ShellScope deferred task after the request, rebuilds their documents in every Content profile whose IndexedContentTypes, Culture and IndexLatest match.
  2. Task log. CreateIndexingTaskContentHandler also appends an Update or Delete task with the Content category (IndexingConstants.ContentsIndexSource). With OrchardCore.Indexing.Worker enabled, ContentIndexingBackgroundTask ("Indexes Content Updater", every minute) calls ContentIndexingService.ProcessRecordsForAllIndexesAsync(), which reads tasks after each profile's last task id in batches of 100 under a distributed lock and catches up anything missed (other nodes, failures, resets).

Both paths build a ContentItemDocumentIndex, run every IDocumentIndexHandler (the content coordinator in turn calls each IContentPartIndexHandler and IContentFieldIndexHandler), and write through the provider's IDocumentIndexManager (AddOrUpdateDocumentsAsync is an upsert, DeleteDocumentsAsync removes). The worker then advances the cursor with SetLastTaskIdAsync. A profile whose physical index does not exist is skipped with a warning; rebuild it.

Index Handler Contracts

Part and field handlers live in OrchardCore.Indexing.Abstractions (namespace OrchardCore.Indexing) and are registered as scoped services. orchardcore-search-indexing has full part and document handler examples.

using OrchardCore.ContentFields.Fields;
using OrchardCore.Indexing;

public sealed class {{FieldName}}IndexHandler : ContentFieldIndexHandler<{{FieldName}}>
{
    public override Task BuildIndexAsync({{FieldName}} field, BuildFieldIndexContext context)
    {
        // Honors the per-field index settings (stored, keyword, sanitized).
        var options = context.Settings.ToOptions();

        foreach (var key in context.Keys)
        {
            context.DocumentIndex.Set(key, field.Text, options);
        }

        return Task.CompletedTask;
    }
}

// services.AddScoped<IContentFieldIndexHandler, {{FieldName}}IndexHandler>();
// services.AddScoped<IContentPartIndexHandler, {{PartName}}IndexHandler>();
// services.AddScoped<IDocumentIndexHandler, {{Name}}DocumentIndexHandler>();

DocumentIndex.Set(name, value, options) overloads map to DocumentIndex.Types:

| Value | Type |

|-------|------|

| string, IHtmlContent | Text |

| DateTimeOffset? | DateTime |

| int? | Integer |

| bool? | Boolean |

| double?, decimal? | Number |

| DocumentIndex.GeoPoint | GeoPoint |

| float[] with dimensions | Vector |

| object (optional metadata) | Complex |

DocumentIndexOptions is a flags enum: None (analyzed text), Store (retrievable from the index), Sanitize (strip HTML before indexing), Keyword (not analyzed, exact match).

Creating an Index Profile via Recipe

{
  "steps": [
    {
      "name": "CreateOrUpdateIndexProfile",
      "indexes": [
        {
          "Id": "unique-profile-id",
          "Name": "ArticlesIndex",
          "IndexName": "articles",
          "ProviderName": "Lucene",
          "Type": "Content",
          "Properties": {
            "ContentIndexMetadata": {
              "IndexLatest": false,
              "IndexedContentTypes": ["Article", "BlogPost"],
              "Culture": "any"
            }
          }
        }
      ]
    }
  ]
}

| Property | Description |

|----------|-------------|

| Id | Optional unique identifier for the index profile. |

| Name | Unique display name for the profile. |

| IndexName | Physical index name used by the provider. |

| ProviderName | The search provider: "Lucene", "Elasticsearch", or "AzureAISearch". |

| Type | Source category. Use "Content" for content items or a custom category name. |

| Properties | Provider-specific and source-specific metadata. |

Content Index Metadata Properties

| Property | Description |

|----------|-------------|

| IndexLatest | When true, indexes both published and draft content items. Default false. |

| IndexedContentTypes | Array of content type technical names to include in the index. |

| Culture | "any" to index all cultures, or a specific culture code (e.g., "en-US"). |

Reset Index Step

Restarts the indexing process from the beginning. Existing entries are preserved; new or updated items are added. The operation runs asynchronously in the background.

{
  "steps": [
    {
      "name": "ResetIndex",
      "indexNames": [
        "ArticlesIndex",
        "BlogPostsIndex"
      ]
    }
  ]
}

To reset all indices:

{
  "steps": [
    {
      "name": "ResetIndex",
      "IncludeAll": true
    }
  ]
}

Rebuild Index Step

Deletes the existing index and rebuilds from scratch. The operation runs asynchronously in the background.

{
  "steps": [
    {
      "name": "RebuildIndex",
      "indexNames": [
        "ArticlesIndex",
        "BlogPostsIndex"
      ]
    }
  ]
}

To rebuild all indices:

{
  "steps": [
    {
      "name": "RebuildIndex",
      "IncludeAll": true
    }
  ]
}

Implementing Custom Indexing Sources

An indexing source is a provider name plus a source category (the profile Type). The provider's IIndexManager, IDocumentIndexManager and IIndexNameProvider are registered as keyed services under the provider name with TryAdd, so the first registration for a provider name wins.

1. Implement IIndexManager — Creates, rebuilds, and deletes provider indexes:

using OrchardCore.Indexing;

namespace MyModule;

public sealed class ProductIndexManager : IIndexManager
{
    public Task<bool> CreateAsync(IndexProfile indexProfile)
        => throw new NotImplementedException();

    public Task<bool> RebuildAsync(IndexProfile indexProfile)
        => throw new NotImplementedException();

    public Task<bool> DeleteAsync(IndexProfile indexProfile)
        => throw new NotImplementedException();

    public Task<bool> ExistsAsync(string indexFullName)
        => throw new NotImplementedException();
}

2. Implement IDocumentIndexManager — Writes documents and tracks the task cursor:

using OrchardCore.Indexing;

namespace MyModule;

public sealed class ProductDocumentIndexManager : IDocumentIndexManager
{
    public Task<bool> AddOrUpdateDocumentsAsync(IndexProfile indexProfile, IEnumerable<DocumentIndex> documents)
        => throw new NotImplementedException();

    public Task<bool> DeleteDocumentsAsync(IndexProfile indexProfile, IEnumerable<string> documentIds)
        => throw new NotImplementedException();

    public Task<bool> DeleteAllDocumentsAsync(IndexProfile indexProfile)
        => throw new NotImplementedException();

    public Task<long> GetLastTaskIdAsync(IndexProfile indexProfile)
        => throw new NotImplementedException();

    public Task SetLastTaskIdAsync(IndexProfile indexProfile, long lastTaskId)
        => throw new NotImplementedException();

    public IContentIndexSettings GetContentIndexSettings()
        => throw new NotImplementedException();
}

3. Implement IIndexNameProvider — Provides physical names for profiles:

using OrchardCore.Indexing;

namespace MyModule;

public sealed class ProductIndexNameProvider : IIndexNameProvider
{
    public string GetFullIndexName(string name) => name;
}

4. Register the custom source in Startup.cs:

using OrchardCore.Modules;
using Microsoft.Extensions.Localization;

namespace MyModule;

[Feature("MyModule.ProductIndex")]
public sealed class Startup : StartupBase
{
    private readonly IStringLocalizer S;

    public Startup(IStringLocalizer<Startup> stringLocalizer)
    {
        S = stringLocalizer;
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddIndexingSource<ProductIndexManager, ProductDocumentIndexManager, ProductIndexNameProvider>(
            "Contoso",
            "Products",
            o =>
            {
                o.DisplayName = S["Products in Contoso"];
                o.Description = S["Creates a search index for product data."];
            });
    }
}

Managing Index Profiles from Code

Use IIndexProfileManager (namespace OrchardCore.Indexing) instead of writing profiles directly; it runs the IIndexProfileHandler pipeline and validation. Creating the profile does not create the physical index; do what the CreateOrUpdateIndexProfile step does and call the provider's keyed IIndexManager.

using System.Text.Json.Nodes;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Indexing;

public sealed class ArticleIndexSetup
{
    private readonly IIndexProfileManager _indexProfileManager;
    private readonly IServiceProvider _serviceProvider;

    public ArticleIndexSetup(IIndexProfileManager indexProfileManager, IServiceProvider serviceProvider)
    {
        _indexProfileManager = indexProfileManager;
        _serviceProvider = serviceProvider;
    }

    public async Task EnsureAsync()
    {
        if (await _indexProfileManager.FindByNameAsync("ArticlesIndex") is not null)
        {
            return;
        }

        // The data shape matches one entry of the CreateOrUpdateIndexProfile step.
        var profile = await _indexProfileManager.NewAsync("Lucene", "Content", new JsonObject
        {
            ["Name"] = "ArticlesIndex",
            ["IndexName"] = "articles",
            ["Properties"] = new JsonObject
            {
                ["ContentIndexMetadata"] = new JsonObject
                {
                    ["IndexedContentTypes"] = new JsonArray("Article"),
                    ["Culture"] = "any",
                },
            },
        });

        var validation = await _indexProfileManager.ValidateAsync(profile);

        if (!validation.Succeeded)
        {
            return;
        }

        await _indexProfileManager.CreateAsync(profile);

        var indexManager = _serviceProvider.GetRequiredKeyedService<IIndexManager>(profile.ProviderName);

        if (!await indexManager.CreateAsync(profile))
        {
            await _indexProfileManager.DeleteAsync(profile);

            return;
        }

        // Indexes the existing records after the request ends.
        await _indexProfileManager.SynchronizeAsync(profile);
    }
}

| Member | Purpose |

|--------|---------|

| NewAsync(providerName, type, data) | Build a profile for a registered provider and source category. |

| ValidateAsync / CreateAsync / UpdateAsync(profile, data) / DeleteAsync | Validate and persist the profile. |

| FindByIdAsync, FindByNameAsync, FindByNameAndProviderAsync, GetAllAsync, GetByProviderAsync, GetAsync(provider, type), PageAsync | Look up profiles. |

| SynchronizeAsync | Run the profile handlers' synchronization (indexing the source records) in a background job after the request. |

| ResetAsync | Run the profile handlers' reset so the profile is re-indexed from the start. |

Custom UI Integration

To provide a configuration screen for a custom indexing source:

Deployment Steps

The Indexing deployment category provides Index Profiles (exports CreateOrUpdateIndexProfile), Reset Indexes (ResetIndex) and Rebuild Indexes (RebuildIndex). Each takes IncludeAll or a list of IndexNames.

Permissions

| Permission | Member | Notes |

|------------|--------|-------|

| ManageIndexes | IndexingPermissions.ManageIndexes | Manage index profiles. Implies provider manage permissions such as ManageElasticIndexes. |

| QuerySearchIndex | IndexingPermissions.QuerySearchIndex | Query any index. |

| QueryIndex_{ProfileName} | IndexingPermissions.CreateDynamicPermission(indexProfile) | Query one index; implied by ManageIndexes and QuerySearchIndex. |

Authorize a query against a specific profile with authorizationService.AuthorizeAsync(user, IndexingPermissions.QuerySearchIndex, indexProfile); the indexing authorization handler resolves the dynamic permission.

Upgrading from 3.x

Indexing UI

The admin dashboard under Search > Indexes provides:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.