Copied to clipboard

Flag this post as spam?

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


  • Fredrik Esseen 610 posts 906 karma points
    Jan 30, 2023 @ 15:12
    Fredrik Esseen
    0

    Using Umbraco.ContentQuery.Search to find related documents

    Hi! Im using the simple search Umbraco.ContentQuery.Search(q, null, "ExternalIndex") to find products on our page.

    On the products we have connected documents (Multiple media picker) but if we make a search on a document name the product that has that document connected doesn't show up.

    Is that possible to do with this solution or how do I change so the externalIndex index that?

  • Johannes Lantz 156 posts 839 karma points c-trib
    Jan 30, 2023 @ 22:30
    Johannes Lantz
    100

    Hi Fredrik!

    If I were you I would add new field to the index which contains the names of the media items. Right now the multiple media picker is saved as JSON in the index. So that's why they aren't showing up! I would do something like this:

    public class IndexComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<IndexerComponent>();
        }
    }
    public class IndexerComponent : IComponent
    {
        private readonly IContentService _contentService;
        private readonly IExamineManager _examineManager;
    
        public IndexerComponent(IContentService contentService, IExamineManager examineManager)
        {
            _contentService = contentService;
            _examineManager = examineManager;
        }
    
        public void Initialize()
        {
            if (!_examineManager.TryGetIndex(UmbracoIndexes.ExternalIndexName, out IIndex index))
                  return;
    
                ((BaseIndexProvider)index).TransformingIndexValues += IndexerComponent_TransformingIndexValues;         
        }   
    
        public void Terminate() { }
    
        private void IndexerComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
        {
            if (int.TryParse(e.ValueSet.Id, out var nodeId))
            {
                foreach (var fieldValues in e.ValueSet.Values.ToList())
                {
                    if (fieldValues.Key == "relatedDoucments")
                    {
                        var node = _contentService.GetById(nodeId);
                        var documents = node.GetValue<IEnumerable<IPublishedContent>>("relatedDoucments").Select(x => x.Name);
                        e.ValueSet.TryAdd("searchableDocumentNames", string.Join(" ", documents));
    
                    }
                }
            }
        }
    }
    

    I wrote this a bit hasty! It should probably have some more if checks. But let me know if it works!

    //Johannes

  • Fredrik Esseen 610 posts 906 karma points
    Jan 31, 2023 @ 15:01
    Fredrik Esseen
    0

    Thanks! That was exactly what I needed :)

Please Sign in or register to post replies

Write your reply to:

Draft