Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 445 posts 1059 karma points
    Nov 10, 2014 @ 16:46
    Ayo Adesina
    0

    Using Examine to search Umbraco.Tags

    Hey guys,

    I have a umbraco doctype with a field that is of type Umbraco.Tags.

    Using examine to search the field like this:

    var searchEngine = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var searchCriteria = searchEngine.CreateSearchCriteria(BooleanOperation.Or);
    
                var query = searchCriteria.Field("title", searchTerm)
                .Or().Field("topicTags", searchTerm).Compile();
    var results = searchEngine.Search(query);
    

    I know for a fact that the value is inside topicTags, but it result is 0...

    Any ideas?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 10, 2014 @ 23:26
    Alex Skrypnyk
    0

    Hi Ayo,

    Are you sure that your 'topicTags' property are indexed ?

    Try to use something like ExamineManager for checking data returned form the Examine Index.

    http://our.umbraco.org/documentation/Reference/Searching/Examine/examine-manager

    Thanks, Alex

  • Ayo Adesina 445 posts 1059 karma points
    Nov 11, 2014 @ 10:49
    Ayo Adesina
    2

    yes, it is indexed, the reason why I was getting 0 was the tags are stored like this: tag1,tag2,tag3. With no spaces so tag1,tag2,tag3 would result in a hit, but tag1 wouldn't.

    The solution was to hook in to the umbraco publish event and change the way that field is indexed. Solution below:

     public class ExamineEvents : ApplicationStartupHandler
    {
        public ExamineEvents()
        {
            ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData +=
                ExamineEvents_GatheringNodeData;
        }
    
    
        private void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
        {
                        if (e.IndexType != IndexTypes.Content) return;
    
            // Node picker values are stored as csv which will not be indexed properly 
            // We need to write the values back into the index without commas so they are indexed correctly
            var fields = e.Fields;
            var searchableFields = new Dictionary<string, string>();
            foreach (var field in fields)
            {
                switch (field.Key)
                {
                    case "topicTags":
    
                        var searchableFieldKey = "topicTagsIndexed";
                        var searchableFieldValue = field.Value.Replace(',', ' ');
                        if (!string.IsNullOrEmpty(searchableFieldValue))
                        {
                            searchableFields.Add(searchableFieldKey, searchableFieldValue);
                        }
                        break;
                }
            }
    
            foreach (var fld in searchableFields)
            {
                e.Fields.Add(fld.Key, fld.Value);
            }
        }
    

    Then when you create your search query you search in the field topicTagsIndexed

    SearchCriteria.Field("pagetitle", searchTerm).Or().Field("topicTagsIndexed", searchTerm).Compile();
    

    Hope this helps someone else.

Please Sign in or register to post replies

Write your reply to:

Draft