Enable NodeName Field Sorting in Umbraco Examine Search

Learn how to enable NodeName sorting in Umbraco Examine search. Improve your site's search functionality with this simple and effective solution.

A common business requirement is the ability to sort search results by NodeName, enhancing content management and display.

Unfortunately, it's tricky with Umbraco Examine.

While looking for a solution with Piotr Bach, we saw many discussions, but unfortunately, we did not find a clear tactic to solve the problem.

Before we start, you need to be aware that:

Examine NodeName field is not sortable in Umbraco by design. When sorting by this field, you will get random and unorganized results.

In this article, I'll demonstrate how to enable NodeName field sorting in Umbraco Examine search, offering a practical solution to enhance your site's search capabilities.

Step 1: Adjust FieldDefinitions for IndexOptions

Here’s the code to enable NodeName sorting in Umbraco Examine for Internal Index:

public class ExamineSortableNodeNameFieldComponent : IComponent
{
    private readonly IOptionsMonitor<LuceneDirectoryIndexOptions> _indexOptions;

    public ExamineSortableNodeNameFieldComponent(
        IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions)
    {
        _indexOptions = indexOptions;
    }

    public void Initialize()
    {
        var internalIndexOptions = _indexOptions.Get(Constants.UmbracoIndexes.InternalIndexName);

        if (internalIndexOptions == null)
            throw new InvalidOperationException($"Unable to obtain options for {Constants.UmbracoIndexes.InternalIndexName}.");

        EnsureSortableNodeNameField(internalIndexOptions);
    }

    private void EnsureSortableNodeNameField(LuceneDirectoryIndexOptions internalIndexOptions)
    {
        var nodeNameSortableFieldDefinition = new FieldDefinition(
            UmbracoExamineFieldNames.NodeNameFieldName, // nodeName
            FieldDefinitionTypes.FullTextSortable);

        internalIndexOptions.FieldDefinitions.AddOrUpdate(nodeNameSortableFieldDefinition);
    }
    
    public void Terminate() { }
}
public class ExamineSortableNodeNameFieldComposer : ComponentComposer<ExamineSortableNodeNameFieldComponent> { }

Breaking Down the Code

Component Definition: The ExamineSortableNodeNameFieldComponent class implements the IComponent interface. It injects IOptionsMonitor<LuceneDirectoryIndexOptions> to access and modify index options.

Initialization: The Initialize method retrieves the internalIndexOptions for the internal index. If the options cannot be obtained, an exception is thrown.

Adding Sortable Field: The EnsureSortableNodeNameField method defines a new field (nodeNameSortableFieldDefinition) with FieldDefinitionTypes.FullTextSortable. This field is added to the index options, making the NodeName field sortable.

Composer Class: The ExamineSortableNodeNameFieldComposer class inherits from ComponentComposer<ExamineSortableNodeNameFieldComponent>, ensuring the component is registered and initialized correctly.

Step 2: Rebuild Examine Index via Umbraco Panel

Now, you need to apply your changes.

Navigate to the Examine Management Section and force full reindexing with the button below:

Force rebuilding Examine Index

Force rebuilding Examine Index via Umbraco Panel

Step 3: Review Index for Node

Once the index rebuild process is complete, you will notice that Umbraco has added an special field named __Sort_nodeName .

New __Sort_nodeName field in Examine Index View

New __Sort_nodeName field in Examine Index View

This means the process was successful.
Now your sorting should work properly when using Examine search by default "nodeName" field.

What's next?

🌐 Explore More: Interested in learning about Umbraco, .NET, and other web development insights? Explore our blog for a wealth of information and expert advice.

✉️Get in Touch: If you have questions or need assistance with your Umbraco projects, don't hesitate to contact us. Our team of experts is always ready to help you navigate any challenges and optimize your digital solutions.

↑ Top ↑