Copied to clipboard

Flag this post as spam?

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


  • Daniel Rogers 143 posts 742 karma points
    Oct 07, 2021 @ 23:09
    Daniel Rogers
    0

    Exmaine a field containing Multinode tree content

    Using Umbraco8

    I have to field on my product listing 1. Multinode tree of Brands "productBrand" 2. Multinode tree of Categories "productCategory"

    I are using Examine "ExternalIndexes" to get a list of all the products and this is working.

    But how do I get a list filtered by my 2 Multinode trees.

    I can pass the field name thats easy, but what do I do for the criteria

  • Erik Eelman 82 posts 322 karma points
    Oct 11, 2021 @ 14:15
    Erik Eelman
    0

    Hi Daniel,

    You can add a custom field to the index with the parsed names or id's of your selected multinode tree picker items. Something like this:

    public class IndexComponent : IComponent
        {
            private readonly IExamineManager _examineManager;
            private readonly IUmbracoContextFactory _umbracoContextFactory;
    
            public IndexComponent(IExamineManager examineManager,
                                  IUmbracoContextFactory umbracoContextFactory)
            {
                _websiteIndexCreator = websiteIndexCreator;
                _examineManager = examineManager;
                _umbracoContextFactory = umbracoContextFactory;
            }
    
            public void Initialize()
            {
                if (_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex websiteIndex))
                {
                    websiteIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("productBrand_parsed", FieldDefinitionTypes.FullText));
                }
            }
    
            private void OnIndexTransform(object sender, IndexingItemEventArgs e)
            {
                using (var context = _umbracoContextFactory.EnsureUmbracoContext())
                {
                    var contentId = int.Parse(e.ValueSet.Id);
                    var content = context.UmbracoContext.Content.GetById(contentId);
                    if (content is YourPageType contentPage)
                    {
                        e.ValueSet.Add(productBrand_parsed, string.Join(",", contentPage.ProductBrand.Select(x => x.Name)));
                    }
    
                }
            }
    
            public void Terminate()
            { }
        }
    

    After this your index has a new field called: productBrand_parsed. You can search in this field with the name of the brand.

    You can find more information about custimizing the index here:

    https://our.umbraco.com/documentation/reference/searching/Examine/indexing/

  • Daniel Rogers 143 posts 742 karma points
    Oct 13, 2021 @ 14:01
    Daniel Rogers
    100

    Thanks Erik for the help.

    Im getting there.

    I have got to the point of registering the indexer and initializing it with 4 new fields.

    However they dont appear in the backoffice if I do a manual examine. But that might be related to my next points.

    1. What is the purpose of this line

      _websiteIndexCreator = websiteIndexCreator;

    2. what triggers OnIndexTransform function as if I break point here the code never breaks.

  • Erik Eelman 82 posts 322 karma points
    Oct 15, 2021 @ 12:42
    Erik Eelman
    1

    Hi Daniel,

    I see i forgot to add some code when creating the example. The init function needs some code to bind the onIndexTransform event:

    if (websiteIndex is BaseIndexProvider baseIndex)
    {
         baseIndex.TransformingIndexValues += OnIndexTransform;
    }
    

    So you need to update the initialize function to something like this:

     public void Initialize()
        {
            if (_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex websiteIndex))
            {
                websiteIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("productBrand_parsed", FieldDefinitionTypes.FullText));
    
                if (websiteIndex is BaseIndexProvider baseIndex)
                {
                    baseIndex.TransformingIndexValues += OnIndexTransform;
                }
            }
        }
    

    You can remove the part with:

    websiteIndexCreator = websiteIndexCreator;
    
  • Daniel Rogers 143 posts 742 karma points
    Oct 18, 2021 @ 01:11
    Daniel Rogers
    0

    Thanks Erik for the help.

    Im getting there.

    I have got to the point of registering the indexer and initializing it with 4 new fields.

    However they dont appear in the backoffice if I do a manual examine. But that might be related to my next points.

    1. What is the purpose of this line

      _websiteIndexCreator = websiteIndexCreator;

    2. what triggers OnIndexTransform function as if I break point here the code never breaks.

  • Daniel Rogers 143 posts 742 karma points
    Oct 18, 2021 @ 01:08
    Daniel Rogers
    0

    Thanks Erik for you help.

    With a few changes I have got it going.

    needed to add

    public class CustomizeIndexComposer : ComponentComposer<IndexComponent> { }
    

    before

    public class public class IndexComponent : IComponent
    ...}
    

    to register the composer

    e.ValueSet.Add(productBrand_parsed, string.Join(",", contentPage.ProductBrand.Select(x => x.Name)));
    

    changed to

                Ibd_eCommerce_Brand brand = (Ibd_eCommerce_Brand)contentPage.ProductBrand ;
    
                e.ValueSet.Add("productBrand_parsed", string.Join(",", brand.Id.ToString()));
    

    didn't like .Select

    on side not are you able to give me some insite to this issue as well

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/107288-examine-get-exact-doc-type

    Sorry Erik I marked the wrong post as the solution. Your too answers are the solution.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies