Copied to clipboard

Flag this post as spam?

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


  • Euan Rae 105 posts 135 karma points
    Oct 07, 2011 @ 13:39
    Euan Rae
    0

    Searching csv fields using examine

    Hi,

    I am using examine for searching some custom fields in umbraco.  I have added the custom fields the ExamineIndex.config like so

     

     <IndexUserFields>

            <add Name="contentTags" />

    ...

     

     

    And am doing a basic search like this:

    IEnumerable<SearchResult> results = ExamineManager.Instance.Search(cleanedSearchTerm, true);

    The issue is that the contentTags field stores data in a comma seperated list, and it won't return nodes when there is more than one item in that field.

    e.g.

    I add 'testasdf123' to the field and do a save and publish.  When I search for  'testasdf123' the node is returned.  However, if I add another tag, so that the field is saved in umbraco as 'testasdf123,SEO'  then the node IS NOT returned.  

    Note that this property is a custom data type that is a usercontrol using the umbraco usercontrol wrapper and the Database datatype is Ntext (in case that makes a difference).

    Any clues?

     

  • elspiko 133 posts 302 karma points
    Oct 13, 2011 @ 17:32
    elspiko
    1

    There is a known issue with comma seperated values and examine/lucene. The work around we use is to hook into Examine's GatheringNodeData event and split the csv by spaces rather than with commas:

    using System;
    using System.Text;
    using System.Xml.Linq;
    using Examine;
    using umbraco.BusinessLogic;
    using umbraco.MacroEngines;
    using umbraco.presentation.nodeFactory;
    using Umbraco_Site_Extensions.automation;
    using UmbracoExamine;
    
    namespace Umbraco_Site_Extensions.examineExtensions
    {
        ///
        /// handle any lucene data injection here
        ///
        public class ExamineEvents:ApplicationBase
        {
    
            public ExamineEvents()
            {
                ExamineManager.Instance.IndexProviderCollection[INDEX_NAME].GatheringNodeData += ExamineEvents_GatheringNodeData;
            }
    
            void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
            {
                if (e.IndexType == IndexTypes.Content)
                {
                    var node = new Node(e.NodeId);                
                    InjectNewsCategoriesWithOutComma(e, node);                
                }
            }
    
            ///
            /// cannot do searching with comma in values properly
            ///
            ///
            ///
            private void InjectNewsCategoriesWithOutComma(IndexingNodeDataEventArgs e, Node node)
            {
                if (node.NodeTypeAlias == Constants.NewsItemTypeAlias &&
                   node.GetPropertyValue(Constants.NewsCategoryFieldAlias) != string.Empty)
                {
                    e.Fields.Add( Constants.NewsCategoryFieldAlias +"Searchable", node.GetPropertyValue(Constants.NewsCategoryFieldAlias).Replace(","," "));
                }
            }
        }
    }

    You'll also need to add the new field into the IndexUserFields element as well but otherwise you should be good to go

  • Euan Rae 105 posts 135 karma points
    Oct 13, 2011 @ 17:38
    Euan Rae
    0

    Hey, I eventually found this in another blog.  Thanks anyway!

  • Pavel Gurecki 55 posts 158 karma points
    Apr 25, 2014 @ 15:13
    Pavel Gurecki
    0

    Couple small notes here:

    1. "INDEX_NAME" is actually your INDEX_PROVIDER_NAME, ex.: "ExternalIndexer"
    2. Sometimes e.NodeId can be -1, so you should add additional check so you won't get unexpected errors:
    if (e.IndexType == IndexTypes.Content && e.NodeId > 0) 
Please Sign in or register to post replies

Write your reply to:

Draft