Copied to clipboard

Flag this post as spam?

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


  • Peter Cort Larsen 418 posts 1015 karma points
    Dec 11, 2020 @ 11:24
    Peter Cort Larsen
    0

    Umbraco 8 TransformingIndexValues from MNTP using variants

    Hi all,

    I have site in Umbraco 8 with 2 languages.

    For a specific node type, I convert the values selected by MNTP to names in my index. Thats works for danish, but not for the node in english.

    I cant figure out what causing this, can anyone help?

    here is my code for adding the MNTP values to the externalIndex.

    using Examine;
    using Examine.LuceneEngine.Providers;
    using Examine.Providers;
    using System;
    using System.Collections.Generic;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Logging;
    using Umbraco.Web;
    using UmbracoExamine.PDF;
    using Website.Models.ModelsBuilder;
    using static Umbraco.Core.Constants;
    
    namespace Website.Composers
    {
    
        [ComposeAfter(typeof(ExaminePdfComposer))] //this must execute after the ExaminePdfComposer composer
        public class RegisterIndexerComponentComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<IndexerComponent>();
            }
        }
        public class IndexerComponent : IComponent
        {
            private readonly IExamineManager examineManager;
            private readonly IUmbracoContextFactory umbracoContextFactory;
            private readonly ILogger logger;
    
            public IndexerComponent(IExamineManager examineManager, IUmbracoContextFactory umbracoContextFactory, ILogger logger)
            {
                this.examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
                this.umbracoContextFactory = umbracoContextFactory ?? throw new ArgumentNullException(nameof(umbracoContextFactory));
                this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
            }
            public void Initialize()
            {
                //Get both the external and pdf index
                if (examineManager.TryGetIndex(UmbracoIndexes.ExternalIndexName, out var externalIndex)
               && examineManager.TryGetIndex(PdfIndexConstants.PdfIndexName, out var pdfIndex))
                {
    
                    //register a multisearcher for both of them
                    var multiSearcher = new MultiIndexSearcher("MultiSearcher", new IIndex[] { externalIndex, pdfIndex });
                    examineManager.AddSearcher(multiSearcher);
    
                    externalIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("categoryNames", FieldDefinitionTypes.Raw));
    
                    ((BaseIndexProvider)externalIndex).TransformingIndexValues += IndexerComponent_TransformingIndexValues;
                }
            }
    
            private void IndexerComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                if (int.TryParse(e.ValueSet.Id, out var nodeId))
    
                    switch (e.ValueSet.ItemType)
                    {
                        case "ePDPage":
                            using (var umbracoContext = umbracoContextFactory.EnsureUmbracoContext())
                            {
                                var contentNode = umbracoContext.UmbracoContext.Content.GetById(nodeId);
    
                                if (contentNode != null)
                                {
                                    var ePDPage = contentNode as EPdpage;
    
                                    List<string> categoryNames = new List<string>();
    
                                    if (ePDPage.Kategori != null)
                                    {
                                        foreach (var category in ePDPage.Kategori)
                                        {
                                            categoryNames.Add(category.Name);
                                        }
    
                                        e.ValueSet.Set("categoryNames", string.Join(",", categoryNames.ToArray()));
                                    }
                                }
                            }
                            break;
                    }
            }
            public void Terminate()
            {
    
            }
        }
    }
    
  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Dec 11, 2020 @ 20:58
    Marc Goodson
    100

    Hi Peter

    I've been doing something similar today, and I'm not sure if that's making me misconstrue your situation, or whether it's the same problem I had.

    But basically I had a content item, that had some fields that varied by language and pickers (that didn't vary) which picked categories that did vary.

    I was putting a version of each document for each language variation into an Azure Search index, and I wanted to be able to facet by the picked category Names, but I was getting them all back in English (my default language) for each language variation of the document.

    What I found is that the 'Name' property when the DocType varies is language specific, and if the culture code isn't specified when using it, it will default to the culture/language of the current thread, which on a View will make sense, but in the TransformingIndexValues event, it probably won't be the culture/language you want.

    So if I've understood correctly, and it is the same issue for you, try setting the culture whenever using the Name property eg:

    category.Name("en-GB")
    

    to be sure you get the variant language you want ...

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft