Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil Chapman 42 posts 170 karma points
    Sep 26, 2022 @ 16:10
    Neil Chapman
    0

    Umbraco 10 custom index registering but not populating due to IIndexPopulator

    I'm trying to create a new index using Examine 3 in an Umbraco 10 project.

    I'm following the documentation for Umbraco 10 to create a custom index - https://our.umbraco.com/Documentation/Reference/Searching/Examine/indexing/#creating-your-own-index.

    The index is registered and I can see it in the backoffice, but it has no records in it and I cannot rebuild it through the backoffice because:

    This index cannot be rebuilt because it has no assigned IIndexPopulator.
    

    I have created a ProductIndexPopulator:

    public class ProductIndexPopulator : IndexPopulator
    {
        private readonly IContentService _contentService;
        private readonly ProductIndexValueSetBuilder _productIndexValueSetBuilder;
    
        public ProductIndexPopulator(IContentService contentService, ProductIndexValueSetBuilder productIndexValueSetBuilder)
        {
            _contentService = contentService ?? throw new ArgumentNullException(nameof(contentService));
            _productIndexValueSetBuilder = productIndexValueSetBuilder ?? throw new ArgumentNullException(nameof(productIndexValueSetBuilder));
    
            RegisterIndex("ProductIndex");
        }
    
        protected override void PopulateIndexes(IReadOnlyList<IIndex> indexes)
        {
            // blah blah blah
        }
    }
    

    And registered it in a composer:

    public class SearchComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.Services.AddExamineLuceneIndex<ProductIndex, ConfigurationEnabledDirectoryFactory>("ProductIndex");
            builder.Services.ConfigureOptions<ConfigureProductIndexOptions>();
            builder.Services.AddSingleton<ProductIndexValueSetBuilder>();
            builder.Services.AddSingleton<ProductIndexPopulator>();
        }
    }
    

    Anybody seen this issue before/know what I'm missing?

    I don't think the docs are accurate and I can't find any U10 examples.

    Thanks

  • Aaron 58 posts 406 karma points MVP 2x c-trib
    Oct 03, 2022 @ 18:43
    Aaron
    0

    I was having the exact same issue, I have been able to register the IndexPopulator correctly, however when I add the Validator into the Index to restrict to a certain document type it stops working.

    In the composer:

    builder.Services.AddSingleton<ProductIndexPopulator>();
    

    becomes

    builder.Services.AddSingleton<IIndexPopulator, ProductIndexPopulator>();
    

    But as I said, even though the index can now be built / rebuilt it will be empty if you try to restrict it

  • Neil Chapman 42 posts 170 karma points
    Oct 04, 2022 @ 08:35
    Neil Chapman
    0

    Does it register and populate WITHOUT the Validator?

  • Aaron 58 posts 406 karma points MVP 2x c-trib
    Oct 04, 2022 @ 08:39
    Aaron
    0

    Yep, if I remove the validator it works.

    By the validator I mean this line:

    options.Validator = new ContentValueSetValidator(true, false, _publicAccessService, _scopeProvider, includeItemTypes: new List<string>{"product"});
    
  • Aaron 58 posts 406 karma points MVP 2x c-trib
    Oct 07, 2022 @ 09:37
    Aaron
    1

    I've had some code given to me by @AndreyKarandas4 on Twitter which works

    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;
    public class MapIndex : UmbracoExamineIndex, IUmbracoContentIndex, IDisposable
    {
    private string[] contentTypes = new string[]
    {
        "documentType"
    };
    public MapIndex(
        ILoggerFactory loggerFactory,
        string name,
        IOptionsMonitor<LuceneDirectoryIndexOptions> indexOptions,
        IHostingEnvironment hostingEnvironment,
        IRuntimeState runtimeState)
        : base(loggerFactory, name, indexOptions, hostingEnvironment, runtimeState)
    {
        loggerFactory.CreateLogger<MapIndex>();
    
        LuceneDirectoryIndexOptions namedOptions = indexOptions.Get(name);
        if (namedOptions == null)
        {
            throw new InvalidOperationException($"No named {typeof(LuceneDirectoryIndexOptions)} options with name {name}");
        }
    
        namedOptions.FieldDefinitions = new(
            new("id", FieldDefinitionTypes.Integer)
        );
    
        if (namedOptions.Validator is IContentValueSetValidator contentValueSetValidator)
        {
            PublishedValuesOnly = contentValueSetValidator.PublishedValuesOnly;
        }
    
    }
    
    void IIndex.IndexItems(IEnumerable<ValueSet> values) => PerformIndexItems(values.Where(v => contentTypes.Any(x => x == v.ItemType)), OnIndexOperationComplete);
    }
    

    Then in your composer:

    builder.Services.AddExamineLuceneIndex<MapIndex, ConfigurationEnabledDirectoryFactory>("indexName");
    
  • ewuski 98 posts 285 karma points
    Mar 16, 2023 @ 18:43
    ewuski
    0

    Indeed, when following this tutorial I was getting the same: https://docs.umbraco.com/umbraco-cms/v/10.latest-lts/reference/searching/examine/indexing

    This index cannot be rebuilt because it has no assigned IIndexPopulator
    

    However this seemed to be due that I did not replace reference to "ProductIndex" in RegisterIndex call in IndexPopulator.

    Once I replaced it with my index name - it started working.

Please Sign in or register to post replies

Write your reply to:

Draft