Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 12, 2018 @ 11:20
    Owain Williams
    0

    Exclude Parent and Children from Examine search

    Hi everyone! I'm looking for a way that I can exclude a parent and it's children from Examine. My current thinking is that I put a toggle on the parent and then when the index is being built, I somehow get it to check what the value of it's root parent is.

    enter image description here

    So, if I set a toggle on Campaign Pages, non of it's children would be indexed. Does that make sense? Is there a better way of doing it?

    I think my code will need to be placed in here:

    private void ExternalIndexerGatheringNodeData(object sender, IndexingNodeDataEventArgs e)
        {
            if (e.IndexType == IndexTypes.Content)
            {
                try
                {
                    var fields = e.Fields;
                    var combinedFields = new StringBuilder();
                    foreach (var keyValuePair in fields)
                    {
                        combinedFields.AppendLine(keyValuePair.Value);
                    }
                    e.Fields.Add("contents", combinedFields.ToString());
                    e.Fields.Add("searchablePath", e.Fields["path"].Replace(",", " "));
    
                }
                catch (Exception ex)
                {
                    LogHelper.Error<Exception>("error munging fields for " + e.NodeId, ex);
                }
    
            }
    

    I hope this makes sense but if it doesn't, feel free to ask me to clarify something. Thanks.

    O.

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 12, 2018 @ 12:08
    Owain Williams
    0

    I should probably add, these campaign pages / content pages aren't unique document types, otherswise I'd just exclude those doctypes from the index config.

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 12, 2018 @ 15:58
    Owain Williams
    0

    Been trying to do something like this today but it's still indexing the children of the parent.

    foreach(var ancestor in e.Node.AncestorsAndSelf())
            {
                if(ancestor.Name.LocalName == "campaignPages")
                {
                    e.Cancel = true;
                    return;
                }
    
            }
    
  • Ismail Mayat 4229 posts 8940 karma points MVP admin c-trib
    Nov 13, 2018 @ 08:10
    Ismail Mayat
    2

    Owain,

    Cancelling in gatheringnode event does not work however it does work in NodeIndexing event so implement that one instead.

    Regards

    Ismail

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 13, 2018 @ 11:17
    Owain Williams
    0

    Does that mean change this section?

            ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData +=
               ExternalIndexerGatheringNodeData;
    

    To something different? Sorry, really not sure what I'm doing on this one.

  • Rasmus Eeg 82 posts 434 karma points c-trib
    Nov 12, 2018 @ 17:03
    Rasmus Eeg
    0

    Well first of, why not exclude them when searching/query build? This way you won't have to do any logic when indexing. Which is much better.

    Oh and is it a custom indexer, or one of the default ones?

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 12, 2018 @ 17:17
    Owain Williams
    0

    Not sure what you mean Rasmus about excluding them from searching / query build. As I mentioned in another post, I can't exclude the docType because there are other pages using the same docType in other parts of the site. Is that what you meant?

    The index is the default ExternalIndexSet, is that what you were asking?

    O.

  • Ryan Casey 8 posts 98 karma points
    Nov 12, 2018 @ 22:01
    Ryan Casey
    0

    I think what Rasmus means is that when you are actually performing a search against your index, add a query that checks for the show/hide field you add. For instance, if you make a boolean field with the alias "hideInSearch" you could add a statement like:

    AND (hideInSearch:0)
    

    to the raw Lucene query being given to your searcher.

  • Rasmus Eeg 82 posts 434 karma points c-trib
    Nov 12, 2018 @ 20:36
    Rasmus Eeg
    0

    Hi Owain,

    I would try creating your own data service, which indexes only the documents you want. By looking in the umbraco cache.

    This way you can then create event handlers to delete or update all indexes when root property change has been published.

    Does that make sense?

    /R

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 12, 2018 @ 21:27
    Owain Williams
    0

    Hi, It does make sense but I want to still index some of the document types that are within the Campaign Pages e.g. Generic Content Pages, which are used in other parts of the site.

    I don't really want to go down the route of making a custom document type that is identical to another document type, just with a different alias so that I can ignore it in the index.

    Maybe I'm misunderstanding things?

    O.

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 13, 2018 @ 16:57
    Owain Williams
    101

    Thanks everyone.

    I got it to work by putting the following in my ExamineEvents.cs file, incase it helps anyone in the future.

      protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationInitialized(umbracoApplication, applicationContext);
    
            ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].NodeIndexing +=
               ExternalIndexerNodeIndexing;
    
        }
    
    
    
    
      private void ExternalIndexerNodeIndexing(object sender, IndexingNodeEventArgs e)
        {
    
            if (e.IndexType == IndexTypes.Content)
            {
    
               //Check for campaign pages and child
    
                if(e.Fields["path"].Contains("2459")) //This is the NodeId of my Campaign Pages node. 
                {
                    e.Cancel = true;
                    return;
                }
    
                try
                {
    
                    var fields = e.Fields;
                    var combinedFields = new StringBuilder();
    
                    foreach (var keyValuePair in fields)
                    {
                        combinedFields.AppendLine(keyValuePair.Value);
                    }
                    e.Fields.Add("contents", combinedFields.ToString());
                    e.Fields.Add("searchablePath", e.Fields["path"].Replace(",", " "));
                }
                catch (Exception ex)
                {
                    LogHelper.Error<Exception>("error munging fields for " + e.NodeId, ex);
                }
    
    
    
            }
        }
    

    I have found one other issue that I'm also indexing Author names so I need to exclude them now. I thought e.Fields.Remove("creatorName"); might work but it doesn't seem to.

  • Rasmus Eeg 82 posts 434 karma points c-trib
    Nov 13, 2018 @ 17:07
    Rasmus Eeg
    0

    Great, I think if you just assign it to a null value it will be removed?

  • Owain Williams 344 posts 1020 karma points MVP c-trib
    Nov 14, 2018 @ 11:33
    Owain Williams
    0

    I was just putting the e.Fields.Remove in the wrong place. It has to be before the ForEach loop.

    All working now :)

    O.

Please Sign in or register to post replies

Write your reply to:

Draft