Copied to clipboard

Flag this post as spam?

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


  • Steven Applegate 18 posts 178 karma points
    Dec 18, 2017 @ 00:57
    Steven Applegate
    0

    How can I set Examine to search under a specific node?

    I'm trying to create a simple search results page for my blog. The structure of my site looks like:

    • Home
      • Blog
        • Blog Post 1
        • Blog Post 1
        • Blog Post 1
        • ...

    I have some test code to get some search only the node title:

    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
    var query = searchCriteria.Field("nodeName", "blog post 3").Compile();
    var searchResults = Searcher.Search(query);
    

    This works, but it is searching across the entire site, instead of just in my blog.

    Is there a way to have Examine search only under a specific node? Maybe a way to add parentNodeId as a IndexAttributeFields in ExamineIndex.config and have a criteria for that?

  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Dec 18, 2017 @ 03:49
    Nathan Woulfe
    100

    Hi Steven

    What we've done for these cases is to index a searchablePath field, which lets us then search only nodes with a particular parent somewhere in their path.

    Since we delimit each path segment with a pipe, we can then search like so:

    var query = searchCriteria.Field("searchablePath", "|1045|").Compile();
    

    To find all descendant nodes of 1045.

    Try this:

    • Create an event handler for the ExamineGatheringNodeData action
    • Override the ApplicationStarted function, to register your custom code for a particular index
    • In that custom code, grab the path
    • Add a new field to the index with the path formatted as a searchable string

    Something like the below:

        public class ExamineGatheringNodeDataAction : ApplicationEventHandler {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
                    var myIndexer = ExamineManager.Instance.IndexProviderCollection[myIndexerAlias];
                    myIndexer.GatheringNodeData += IndexerOnGatheringNodeData;
            }
    
             private void IndexerOnGatheringNodeData(object sender, IndexingNodeDataEventArgs e) {
    
                 var path = e.Fields["path"];
                 var searchablePath = String.Join(" ", path.Split(',').Select(x => String.Format("{1}{0}{1}", x.ToLower(), '|')));
    
                 if (e.Fields.ContainsKey("searchablePath"))
                 {
                     e.Fields["searchablePath"] = searchablePath;
                 }
                 else
                 {
                     e.Fields.Add("searchablePath", searchablePath);
                 }
             }
        }
    
  • 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