Copied to clipboard

Flag this post as spam?

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


  • Vidyesh 5 posts 85 karma points c-trib
    Apr 27, 2019 @ 14:10
    Vidyesh
    0

    Unable to access searchers by their name in Umbraco 8 using Fluent API

    Unable to call the method to fetch a particular searcher using the following method in Umbraco 8:

    var externalSearcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];

    It says 'IExamineManager' does not contain a definition for 'SearchProviderCollection' Examine version: 1.0.0.0

    Umbraco documentation: Link

  • Marc Goodson 1676 posts 11070 karma points MVP 6x c-trib
    Apr 28, 2019 @ 09:45
    Marc Goodson
    102

    Hi Vidyesh

    Umbraco V8 uses a brand new version of Examine - there isn't much information available on how this latest version of Examine works... the Examine wiki: https://github.com/Shazwazza/Examine/wiki - so information in the Umbraco documentation currently is only for V7.

    However a lot of the concepts are the same, the main difference is how you get underway and how to configure Examine in Umbraco, and the names of some methods and events have changed.

    Anyway. first things first, how do you get an ExamineManager? this can now be achieved via DI, so if you have an MVC Controller you can get an ExamineManager object via its constructor:

    eg

    using System.Web.Mvc;
    using Umbraco.Web.Models;
    using Umbraco8.Services;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Services;
    using Umbraco.Core.Models.Membership;
    using Umbraco.Core.Composing;
    using Examine;
    
    namespace Umbraco8.Controllers
    {
        public class BlogPostController : Umbraco.Web.Mvc.RenderMvcController
        { 
            private readonly IExamineManager _examineManager;
    
            public BlogPostController(IExamineManager examineManager)
            {
             // and now we have an instance of an ExamineManager we can use in our controller actions...
                _examineManager = examineManager;
    
            }
    

    given we have an ExamineManager how do we get our ExamineSearcher for a particular index?

    Looking at the source of Umbraco which also uses Examine here and there, we can use a TryGetIndex method like so:

      public override ActionResult Index(ContentModel model)
            {
               var examineSearcher = _examineManager.TryGetIndex("ExternalIndex", out var externalIndex) ? externalIndex.GetSearcher() : null;
    }
    

    That should get us a searcher for the ExternalIndex!

    Now we're starting to get on more familiar territory, as we can fluid-ily create our examine search criteria:

       public override ActionResult Index(ContentModel model)
            {
               var examineSearcher = _examineManager.TryGetIndex("ExternalIndex", out var externalIndex) ? externalIndex.GetSearcher() : null;
                    //restrict teh search to the Content section, create our search criteria object
                var criteria = examineSearcher.CreateQuery("content");
                // build up our search filter in the same way as in V7/early examine:
                var filter = criteria.Field("bodyText", "Fish");
    

    Once we have our criteria ready to search, there is I think a further change, we now call Execute() instead of Search():

        public override ActionResult Index(ContentModel model)
        {
           var examineSearcher = _examineManager.TryGetIndex("ExternalIndex", out var externalIndex) ? externalIndex.GetSearcher() : null;
    
            var criteria = examineSearcher.CreateQuery("content");
            var filter = criteria.Field("bodyText", "Fish");
            // call Execute() to get matching search results
            ISearchResults results = filter.Execute();
         foreach (var result in results)
            {
                var id = result.Id;
                var name = result.Values.TryGetValue("nodeName", out string nodeName) ? nodeName : "no name!";
            }
    

    Anyway hope that gets you going, there are lots of little subtle changes to how you do things, it's helpful to look at Examine queries in Umbraco source to see how they are executed there with the new Examine, or if you know what something was called before eg GatheringNodes Event, you can find it in an old 'dev' branch of Examine:

    https://github.com/Shazwazza/Examine/tree/dev

    and then compare that file to see what has changed in the Examine 1.0 release branch, and then make a guess as to what things have been changed to... eg GatheringNodeEvent appears now to be TransformingIndexValues event

    regards Marc

  • Vidyesh 5 posts 85 karma points c-trib
    Apr 29, 2019 @ 08:32
    Vidyesh
    0

    This works like a charm :)

    Examine team is also still working on the documentation of their latest version (1.0.1)

    Thanks a lot for the detailed explanation Marc. This helped a lot !

  • Marc Goodson 1676 posts 11070 karma points MVP 6x c-trib
    Apr 29, 2019 @ 19:06
    Marc Goodson
    1

    cool Vidyesh, and also, as if by magic...

    there is now V8 Examine documentation appearing: https://our.umbraco.com/Documentation/Reference/Searching/Examine/quick-start/

  • Vidyesh 5 posts 85 karma points c-trib
    May 02, 2019 @ 06:48
    Vidyesh
    0

    Finally !! :)

Please Sign in or register to post replies

Write your reply to:

Draft