Orchardcore lucene

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore lucene is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for configuring Lucene search in Orchard Core. Covers Lucene index creation and management, recipe steps for index operations, Lucene queries and the Lucene Query API, query filters, the Lucene Worker background task, custom data indexing, automatic field mapping, Elasticsearch Query DSL suppo

How to use this skill

Skill file

Orchard Core Lucene Search - Prompt Templates

Configure Lucene Search

You are an Orchard Core expert. Generate code and configuration for Lucene-based full-text search in Orchard Core.

Guidelines

Enabling Lucene Features

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

Creating a Lucene Index Profile (Recommended)

Use CreateOrUpdateIndexProfile to create a Lucene index for content items:

{
  "steps": [
    {
      "name": "CreateOrUpdateIndexProfile",
      "indexes": [
        {
          "Name": "BlogPostsLucene",
          "IndexName": "blogposts",
          "ProviderName": "Lucene",
          "Type": "Content",
          "Properties": {
            "ContentIndexMetadata": {
              "IndexLatest": false,
              "IndexedContentTypes": ["BlogPost"],
              "Culture": "any"
            },
            "LuceneIndexMetadata": {
              "AnalyzerName": "standard",
              "StoreSourceData": true
            }
          }
        }
      ]
    }
  ]
}

| Property | Description |

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

| Name | Unique display name for the index profile. |

| IndexName | Physical index name on disk. |

| ProviderName | Must be "Lucene". |

| Type | Source category, typically "Content" for content items. |

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

| IndexedContentTypes | Array of content type names to include. |

| Culture | "any" for all cultures or a specific culture code. |

| AnalyzerName | Lucene analyzer name (e.g., "standard"). |

| StoreSourceData | When true, stores source data for retrieval. |

Legacy Index Creation (Obsolete)

The lucene-index step is obsolete but still supported:

{
  "steps": [
    {
      "name": "lucene-index",
      "Indices": [
        {
          "Search": {
            "AnalyzerName": "standard",
            "IndexLatest": false,
            "IndexedContentTypes": [
              "Article",
              "BlogPost"
            ]
          }
        }
      ]
    }
  ]
}

Reset Lucene Index

Restarts indexing from the beginning without deleting existing entries:

{
  "steps": [
    {
      "name": "ResetIndex",
      "indexNames": [
        "BlogPostsLucene"
      ]
    }
  ]
}

To reset all indices:

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

Rebuild Lucene Index

Deletes and recreates the full index content:

{
  "steps": [
    {
      "name": "RebuildIndex",
      "indexNames": [
        "BlogPostsLucene"
      ]
    }
  ]
}

To rebuild all indices:

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

Creating a Lucene Query via Recipe

{
  "steps": [
    {
      "name": "Queries",
      "Queries": [
        {
          "Source": "Lucene",
          "Name": "RecentBlogPosts",
          "Index": "BlogPostsLucene",
          "Template": "{ \"query\": { \"match_all\": {} }, \"size\": 10 }",
          "ReturnContentItems": true
        }
      ]
    }
  ]
}

Index is the index profile Name (not the physical IndexName). Saved Lucene queries are executed through api/queries/{name}, Liquid (Queries.RecentBlogPosts | query), GraphQL and the Queries data source (see orchardcore-content-queries and orchardcore-data-sources). Since Orchard Core 4.0, the GraphQL field of a named query requires the ExecuteApi_{QueryName} permission (implied by ExecuteApiAll and ManageQueries).

Lucene Query Filters

Query filters retrieve records without considering boost values, similar to SQL filtering.

Bool filter with term and wildcard:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "Content.ContentItem.Published": "true"
          }
        },
        {
          "wildcard": {
            "Content.ContentItem.DisplayText": "Main*"
          }
        }
      ]
    }
  }
}

Bool with must and filter (filtering by content type):

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "Content.ContentItem.ContentType.keyword": "Menu"
        }
      },
      "filter": [
        {
          "term": {
            "Content.ContentItem.Published": "true"
          }
        },
        {
          "wildcard": {
            "Content.ContentItem.DisplayText": "Main*"
          }
        }
      ]
    }
  }
}

Full-text search with query_string:

{
  "query": {
    "query_string": {
      "query": "Content.ContentItem.FullText:\"exploration\""
    }
  }
}

Full-text search with simple_query_string and default field:

{
  "query": {
    "simple_query_string": {
      "query": "\"exploration\"",
      "fields": [
        "Content.ContentItem.FullText"
      ]
    }
  }
}

Web APIs

api/lucene/content — Executes a query and returns content items.

Verbs: POST and GET

| Parameter | Example | Description |

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

| indexName | "search" | The name of the index to query. |

| query | { "query": { "match_all": {} }, "size": 10 } | JSON query object. |

| parameters | { "size": 3 } | JSON parameters object. |

api/lucene/documents — Executes a query and returns Lucene documents (stored fields only).

Same parameters as api/lucene/content.

Both endpoints use the Api authentication scheme and require LuceneSearchPermissions.QueryLuceneApi. Their endpoint names are ApiGetLuceneContent, ApiPostLuceneContent, ApiGetLuceneDocuments and ApiPostLuceneDocuments (used by OpenAPI, see orchardcore-openapi).

Permissions

| Permission | Member | Notes |

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

| ManageLuceneIndexes | OrchardCore.Lucene.LuceneSearchPermissions.ManageLuceneIndexes | Manage Lucene indexes and run admin queries. |

| QueryLuceneApi | OrchardCore.Lucene.LuceneSearchPermissions.QueryLuceneApi | Call api/lucene/*. Implied by ManageLuceneIndexes. |

| QueryIndex_{IndexName} | IndexingPermissions.CreateDynamicPermission(indexProfile) | Query one index from the front-end search. Implied by ManageIndexes and QuerySearchIndex. |

| QuerySearchIndex | OrchardCore.Indexing.Core.IndexingPermissions.QuerySearchIndex | Query any index. |

A PermissionMigrations deferred task rewrites the legacy per-index role claims (QueryLucene{IndexName}Index) to the dynamic QueryIndex_{ProfileName} permissions once the index profiles exist.

Upgrading from 3.x

Lucene Worker

Enable OrchardCore.Search.Lucene.Worker to synchronize the local file system index across multiple instances. This creates a background task that keeps indexes in sync in a farm scenario.

Do not enable the Worker if:

Indexing Custom Data

Register a custom indexing source in Startup.cs:

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

namespace MyModule;

[Feature("MyModule.CustomLuceneIndex")]
public sealed class Startup : StartupBase
{
    private readonly IStringLocalizer<Startup> _localizer;

    public Startup(IStringLocalizer<Startup> localizer)
    {
        _localizer = localizer;
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddLuceneIndexingSource("CustomSource", o =>
        {
            o.DisplayName = _localizer["Custom Source"];
            o.Description = _localizer["Create a Lucene index based on a custom data source."];
        });
    }
}

Automatic Field Mapping

Lucene automatically maps text fields with a .keyword suffix as stored values in the index, unless the document is already explicitly stored. Values longer than 256 characters are ignored. This enables any TextField to be searched using term queries by appending .keyword to the field name.

For example, to query by the exact DisplayText value, use Content.ContentItem.DisplayText.keyword in a term query.

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.