Table of contents
Removing an Examine index in Umbraco can improve performance in various ways, especially on large, content-heavy websites.
In the previous article How to Disable Examine External Indexing in Umbraco, walked through the process of disabling the Examine External Index
Now, I would like to demonstrate a more flexible approach allowing you to disable any Examine index.
The solution below has been tested on two versions of Umbraco 11 and 13, but it should also work on subsequent and newer ones.
Reviewing Active Umbraco Indexes in Umbraco Backoffice
When you go to the 'Settings' section and select the 'Examine Management' tab, you will see all active Examine indexes:
- DeliveryApiContentIndex
- ExternalIndex
- InternalIndex
- MembersIndex
This means that all four indexes are active.
Understanding Registering Examine Indexes in Runtime
In Umbraco, indexes are typically registered through the ServicesCollectionExtensions.cs file using the AddExamineLuceneIndex<TIndex, TDirectoryFactory>() method.
Here's a simplified look at how this registration process works:
/// <summary>
/// Registers an Examine index
/// </summary>
public static IServiceCollection AddExamineLuceneIndex<TIndex, TDirectoryFactory>(
this IServiceCollection serviceCollection,
string name,
FieldDefinitionCollection fieldDefinitions = null,
Analyzer analyzer = null,
IValueSetValidator validator = null,
IReadOnlyDictionary<string, IFieldValueTypeFactory> indexValueTypesFactory = null)
where TIndex : LuceneIndex
where TDirectoryFactory : class, IDirectoryFactory
{
// This is the long way to add IOptions but gives us access to the
// services collection which we need to get the dir factory
serviceCollection.AddSingleton<IConfigureOptions<LuceneDirectoryIndexOptions>>(
services => new ConfigureNamedOptions<LuceneDirectoryIndexOptions>(
name,
(options) =>
{
options.Analyzer = analyzer;
options.Validator = validator;
options.IndexValueTypesFactory = indexValueTypesFactory;
options.FieldDefinitions = fieldDefinitions ?? options.FieldDefinitions;
options.DirectoryFactory = services.GetRequiredService<TDirectoryFactory>();
}));
return serviceCollection.AddSingleton<IIndex>(services =>
{
IOptionsMonitor<LuceneDirectoryIndexOptions> options
= services.GetRequiredService<IOptionsMonitor<LuceneDirectoryIndexOptions>>();
TIndex index = ActivatorUtilities.CreateInstance<TIndex>(
services,
new object[] { name, options });
return index;
});
}
In the above code, the AddExamineLuceneIndex method registers a new Examine index in Umbraco using dependency injection.
It defines various configurations for the index, including the analyzer, validator, and directory factory, which are crucial components in the Lucene indexing system.
Removing Examine Index by Name
Now, to remove an Examine index by its name, you can use the following extension method:
public static class UmbracoBuilderExtensions
{
public static void RemoveExamineIndex(this IUmbracoBuilder builder, string indexName)
{
var services = builder.Services;
// Find all IIndex services registered
var indexServiceDescriptors = builder.Services
.Where(s => s.ServiceType == typeof(IIndex))
.ToList();
foreach (var service in indexServiceDescriptors)
{
if (service.ImplementationFactory != null)
{
// Inspect the factory target (closure)
var factoryTarget = service.ImplementationFactory.Target;
if (factoryTarget != null)
{
var targetType = factoryTarget.GetType();
var fields = targetType.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
// Check if the field is named "name" and is a string
if (field.Name == "name" && field.FieldType == typeof(string))
{
var fieldValue = field.GetValue(factoryTarget);
if (fieldValue != null && fieldValue.ToString() == indexName)
{
// If the field value matches the indexName, remove the service
services.Remove(service);
Console.WriteLine($"Examine {indexName} index found and removed.");
return;
}
}
}
}
}
}
}
}
The RemoveExamineIndex method above inspects all registered IIndex services, finds the ones matching the specified indexName, and removes them from the services collection.
Removing Umbraco ExternalIndex and DeliveryApiContentIndex
Let's say you want to remove two indexes: ExternalIndex and DeliveryApiContentIndex.
You just need to register a Composer and call the RemoveExamineIndex method twice with the appropriate index name as below:
public class SolutionComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.RemoveExamineIndex(Constants.UmbracoIndexes.ExternalIndexName);
builder.RemoveExamineIndex(Constants.UmbracoIndexes.DeliveryApiContentIndexName);
}
}
This will cleanly remove the ExternalIndex and DeliveryApiContentIndex during the application startup process, improving your site's performance by eliminating unnecessary indexing tasks.
Notice that the index names are defined in Umbraco constants:
namespace Umbraco.Cms.Core;
public static partial class Constants
{
public static class UmbracoIndexes
{
public const string InternalIndexName = "InternalIndex";
public const string ExternalIndexName = "ExternalIndex";
public const string MembersIndexName = "MembersIndex";
public const string DeliveryApiContentIndexName = "DeliveryApiContentIndex";
}
}
Validating Active Umbraco Indexes Again
Now, when you restart Umbraco, you will see in the system settings that only two indexes are active, as show in the screenshot below:
Benefits of Removing Examine Indexes
When working with Umbraco, every active Examine index adds overhead to your system.
These indexes continually track changes to your content, ensuring the search functionality remains up-to-date.
However, there are scenarios where this indexing might be unnecessary or even detrimental:
Improved Performance
Disabling unnecessary indexes can lead to faster content publishing and reduced overall processing load.
Each index requires system resources to maintain its state, especially during large data imports or high-traffic scenarios.
Removing them reduces the indexing time, which improves cold start-ups and system responsiveness.
Simpler Search Management
By removing unused indexes, you streamline your search infrastructure.
This can simplify maintaining and troubleshooting search-related issues, as you're focusing only on what's necessary for your use case.
Optimized Content Queries
In some cases, custom searches (such as those that don’t rely on Umbraco's default indexes) are more efficient for specific types of content.
Removing irrelevant indexes makes it easier to implement custom solutions without conflicts.
Pain Points and Considerations
When removing Examine indexes in Umbraco, there are a few potential challenges to remember.
Breaking Features that Depend on the Index
If your website has a custom search feature that relies on the ExternalIndex, disabling this index could break search functionality.
Imagine users suddenly being unable to search for products on an e-commerce site because the index powering the search no longer exists.
Issues with Third-Party Plugins
A third-party SEO plugin that relies on ExternalIndex to track and analyze content performance could stop functioning correctly.
This could lead to a drop in your site’s SEO performance without you even realizing it.
Testing and Validation Overload
Disabling multiple indexes across a large, complex site can result in unforeseen issues that require extensive testing to identify.
Remember: Removing the InternalIndex will probably kill your Umbraco website immediately.
Conclusion
In this tutorial, we learned how to disable any Examine index.
Removing unnecessary Examine indexes in Umbraco can be a powerful optimization tool, especially on larger websites or those with highly specialized search needs.
Disabling unused indexes can significantly improve your site's speed, reduce resource usage, and simplify search management.
However, be mindful of the potential side effects, and always thoroughly test before going live.
What's Next?
Explore our blog for more insights, and feel free to reach out for any queries or discussions related to Umbraco development.