Created
August 7, 2021 18:52
-
-
Save AaronSadlerUK/baa1aa299115cd5207c50c7230caf4ec to your computer and use it in GitHub Desktop.
How to create a custom index in Umbraco V9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Examine; | |
using Microsoft.Extensions.DependencyInjection; | |
using UmbHost.Core.Components; | |
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Infrastructure.Examine; | |
namespace UmbHost.Core.Composers | |
{ | |
public class IndexHelperComposer : IUserComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
IServiceCollection services = builder.Services; | |
services | |
.AddExamineLuceneIndex<KnowledgebaseIndex, ConfigurationEnabledDirectoryFactory>("KnowledgebaseArticles"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Examine; | |
using Examine.Lucene; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
using Umbraco.Cms.Core.Hosting; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Infrastructure.Examine; | |
namespace UmbHost.Core.Components | |
{ | |
public class KnowledgebaseIndex : UmbracoExamineIndex, IUmbracoContentIndex, IDisposable | |
{ | |
public KnowledgebaseIndex( | |
ILoggerFactory loggerFactory, | |
string name, | |
IOptionsSnapshot<LuceneDirectoryIndexOptions> indexOptions, | |
IHostingEnvironment hostingEnvironment, | |
IRuntimeState runtimeState) | |
: base(loggerFactory, name, indexOptions, hostingEnvironment, runtimeState) | |
{ | |
loggerFactory.CreateLogger<KnowledgebaseIndex>(); | |
LuceneDirectoryIndexOptions namedOptions = indexOptions.Get(name); | |
if (namedOptions == null) | |
{ | |
throw new InvalidOperationException($"No named {typeof(LuceneDirectoryIndexOptions)} options with name {name}"); | |
} | |
if (namedOptions.Validator is IContentValueSetValidator contentValueSetValidator) | |
{ | |
PublishedValuesOnly = contentValueSetValidator.PublishedValuesOnly; | |
} | |
} | |
void IIndex.IndexItems(IEnumerable<ValueSet> values) => PerformIndexItems(values.Where(v => v.ItemType == "knowledgebaseArticle"), OnIndexOperationComplete); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is a couple of years old now, but as it was one of the only things I could find and it inspired my solution for an unprotected index. Here's my take in v12 to give back. I think inheriting from
UmbracoContentIndex
rather than implementing the interface means gaining the core implementations for updating indexes when un/protecting content events occur. And theoptions.Validator = new ContentValueSetValidator(true, true, _publicAccessService, _scopeProvider);
second option is supportprotected was what I was actually after.