Last active
October 11, 2022 14:12
-
-
Save nul800sebastiaan/b9a6730a6ce1bf9f8d00fcd82e087f1f to your computer and use it in GitHub Desktop.
Custom Examine Index Umbraco 10.2.0
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 Examine.Lucene; | |
using Lucene.Net.Analysis.Standard; | |
using Lucene.Net.Index; | |
using Lucene.Net.Util; | |
using Microsoft.Extensions.Options; | |
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.Configuration.Models; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Cms.Core.Scoping; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Infrastructure.Examine; | |
namespace MyProject; | |
public class MyIndex : UmbracoExamineIndex | |
{ | |
private static class IndexConstants | |
{ | |
public static string IndexName => "MyCustomIndex"; | |
} | |
public MyIndex( | |
ILoggerFactory loggerFactory, | |
string name, | |
IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions, | |
Umbraco.Cms.Core.Hosting.IHostingEnvironment hostingEnvironment, | |
IRuntimeState runtimeState) | |
: base(loggerFactory, name, indexOptions, hostingEnvironment, runtimeState) | |
{ } | |
public class ConfigureMyIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions> | |
{ | |
private readonly IOptions<IndexCreatorSettings> _settings; | |
private readonly IPublicAccessService _publicAccessService; | |
private readonly IScopeProvider _scopeProvider; | |
public ConfigureMyIndexOptions( | |
IOptions<IndexCreatorSettings> settings, | |
IPublicAccessService publicAccessService, | |
IScopeProvider scopeProvider) | |
{ | |
_settings = settings; | |
_publicAccessService = publicAccessService; | |
_scopeProvider = scopeProvider; | |
} | |
public void Configure(string name, LuceneDirectoryIndexOptions options) | |
{ | |
if (name.Equals(IndexConstants.IndexName)) | |
{ | |
options.Analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48); | |
options.FieldDefinitions = new( | |
new("id", FieldDefinitionTypes.Integer), | |
new("nodeName", FieldDefinitionTypes.FullText) | |
); | |
options.UnlockIndex = true; | |
var contentValueSetValidator = new ContentValueSetValidator(publishedValuesOnly: true, supportProtectedContent: false, _publicAccessService, _scopeProvider, includeItemTypes: new []{ "home" }); | |
options.Validator = contentValueSetValidator; | |
if (_settings.Value.LuceneDirectoryFactory == LuceneDirectoryFactory.SyncedTempFileSystemDirectoryFactory) | |
{ | |
// if this directory factory is enabled then a snapshot deletion policy is required | |
options.IndexDeletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); | |
} | |
} | |
} | |
public void Configure(LuceneDirectoryIndexOptions options) | |
{ | |
// why is this needed?? | |
} | |
} | |
public class MyIndexValueSetBuilder : IValueSetBuilder<IContent> | |
{ | |
public IEnumerable<ValueSet> GetValueSets(params IContent[] contents) | |
{ | |
foreach (var content in contents) | |
{ | |
var indexValues = new Dictionary<string, object> | |
{ | |
["nodeName"] = content.Name, | |
["id"] = content.Id, | |
}; | |
yield return new ValueSet(content.Id.ToString(), category: IndexTypes.Content, itemType: content.ContentType.Alias, indexValues); | |
} | |
} | |
} | |
public class MyIndexPopulator : IndexPopulator | |
{ | |
private readonly IContentService _contentService; | |
private readonly MyIndexValueSetBuilder _myIndexValueSetBuilder; | |
public MyIndexPopulator(IContentService contentService, MyIndexValueSetBuilder myIndexValueSetBuilder) | |
{ | |
_contentService = contentService; | |
_myIndexValueSetBuilder = myIndexValueSetBuilder; | |
RegisterIndex(IndexConstants.IndexName); | |
} | |
protected override void PopulateIndexes(IReadOnlyList<IIndex> indexes) | |
{ | |
foreach (var index in indexes) | |
{ | |
var roots = _contentService.GetRootContent(); | |
index.IndexItems(_myIndexValueSetBuilder.GetValueSets(roots.ToArray())); | |
foreach (var root in roots) | |
{ | |
var valueSets = _myIndexValueSetBuilder.GetValueSets(_contentService.GetPagedDescendants(root.Id, 0, Int32.MaxValue, out _).ToArray()); | |
index.IndexItems(valueSets); | |
} | |
} | |
} | |
} | |
public class MyIndexComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.Services.ConfigureOptions<ConfigureMyIndexOptions>(); | |
builder.Services.AddSingleton<MyIndexValueSetBuilder>(); | |
builder.Services.AddSingleton<IIndexPopulator, MyIndexPopulator>(); | |
builder.Services.AddExamineLuceneIndex<MyIndex, ConfigurationEnabledDirectoryFactory>(IndexConstants.IndexName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment