Copied to clipboard

Flag this post as spam?

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


  • David Armitage 508 posts 2078 karma points
    Jun 06, 2021 @ 09:19
    David Armitage
    0

    Umbraco 9 - How to get up and running with Examine

    Hi All,

    Is Examine up and running yet for Umbraco 9? If so does anyone have any example code so I can try and get up and running and test it.

    I am looking for the equivalent of how we used to do it for Umbraco 8.

    So something similar to the below. Adding a custom field into the search index etc.

    Maybe also some code of actually doing a search if this has changed much?

    public class ExamineComponents : IComponent
        {
            private readonly IUmbracoContextFactory _umbracoContextFactory;
            private readonly IExamineManager _examineManager;
            private readonly ILogger _logger;
    
            public ExamineComponents(IUmbracoContextFactory umbracoContextFactory, IExamineManager examineManager, ILogger logger)
            {
                _umbracoContextFactory = umbracoContextFactory;
                _examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
                _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            }
    
            public void Initialize()
            {
                if (!_examineManager.TryGetIndex(Umbraco.Core.Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
                {
                    throw new InvalidOperationException($"No index found by name {Umbraco.Core.Constants.UmbracoIndexes.ExternalIndexName}");
                }
    
                if (!(index is BaseIndexProvider indexProvider))
                    throw new InvalidOperationException("Could not cast");
    
                indexProvider.TransformingIndexValues += IndexProviderTransformingIndexValues;
            }
    
    
            private void IndexProviderTransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                if (int.TryParse(e.ValueSet.Id, out var pageId))
                {
                    if (e.ValueSet.ItemType == "residence" || e.ValueSet.ItemType == "university" || e.ValueSet.ItemType == "blogArticle" || e.ValueSet.ItemType == "eventArticle")
                    {
                        using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                        {
                            var contentNode = umbracoContextReference.UmbracoContext.Content.GetById(pageId);
                            if (contentNode != null){
    
                                var combinedFields = new StringBuilder();
                                foreach (var fieldValues in e.ValueSet.Values)
                                {
                                    foreach (var value in fieldValues.Value)
                                    {
                                        if (value != null)
                                            combinedFields.AppendLine(value.ToString());
                                    }
                                }
    
                                e.ValueSet.Add("searchableContent", combinedFields.ToString().CleanForExamineSearch());
                            }   
                        }
                    }
                }
            }
    
            public void Terminate() { }
        }
    

    Kind Regards

    David

  • Stuart Mullinger 79 posts 422 karma points
    Jun 06, 2021 @ 15:31
    Stuart Mullinger
    0

    Hi David,

    I have got it up-and-running using an Component as you have there. Not sure if that's the ideal .Net Core way, but it's working for me.

    Stuart.

  • Benjamin Carleski 33 posts 294 karma points MVP c-trib
    Jun 07, 2021 @ 15:36
    Benjamin Carleski
    100

    The Composer/Component way works really well for packages, because you can register it without having to have the end user change their Startup.cs file to call your methods. But in my own project, I'd just add calls to Startup.cs.

    That said, I updated the Our.Umbraco.GraphQL package last week, and while it doesn't do custom indexes, it does query Examine. I didn't find I needed to change much of anything except perhaps some namespace references. I did have to update the maxResults parameter to instead pass in the ExamineOptions class with skip/take, as Stuart mentions at https://our.umbraco.com/forum/umbraco-9/106215-examine-skiptake, but otherwise the same v8 code seemed to work fine in v9.

  • David Armitage 508 posts 2078 karma points
    Jun 14, 2021 @ 08:38
    David Armitage
    0

    Thanks Benjamin.

    I am taking a look at this again now.

  • David Armitage 508 posts 2078 karma points
    Jun 14, 2021 @ 12:10
    David Armitage
    1

    Hi,

    I have also followed up by writing two blog articles which might help people trying to get up and running with Examine on Umbraco 9.

    Umbraco 9 Examine - Basic Search https://www.umbrajobs.com/blog/posts/2021/june/umbraco-9-examine-basic-search/

    Umbraco 9 Examine - Creating a Custom Index https://www.umbrajobs.com/blog/posts/2021/june/umbraco-9-examine-creating-a-custom-index/

    Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft