Copied to clipboard

Flag this post as spam?

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


  • glenatron 37 posts 132 karma points
    Jan 05, 2014 @ 01:33
    glenatron
    0

    Can I search for a Null field or provide a default index value with Examine?

    I have a field on some of my records ( in a site built with Umbraco 6.1.6 ) that is often null. One thing I want to be able to do is to search for all the records where that field is null.

    Is there a way to do this with Examine?

    It looks as though in the search results, the field is just completely absent in records where it is null in the underlying data store. Am I misinterpreting? If not, is there a search term or process that will allow me to find all records where this field is absent?

    If it isn't possible to find cases where it is absent, is there any way to configure the index so that it places a default value in the field when it is unset?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jan 06, 2014 @ 18:16
    Andy Butland
    0

    I believe that's correct - a null value will just be absent.  It could be you'll need to hook into the "GatheringNodeData" event, and test for null there.  If found, set it as some fixed value like "-1" and then it'll be in the index and you can search for that.

    Something like this (but note untested):

        public class UpdateNullValuesInExamineIndex : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext)
            {
            }
    
            public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext)
            {
                ExamineManager.Instance.IndexProviderCollection[CombinedIndexer].GatheringNodeData += UpdateNullValues;
            }
    
            public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)
            {
            }
    
            private static void UpdateNullValues(object sender, IndexingNodeDataEventArgs e)
            {
                if (e.Fields["nodeTypeAlias"] == "myDocType" && !e.Fields.ContainsKey("myField"))
                {
                    e.Fields.Add("myField", "-1");                  
                }
            }
        }
    
  • glenatron 37 posts 132 karma points
    Jan 08, 2014 @ 17:16
    glenatron
    0

    Thanks for that- useful to have it confirmed. I'm actually mapping the results into a viewmodel, so I guess I'll map all results and then use linq to sort the models rather than doing it all in the search query.

Please Sign in or register to post replies

Write your reply to:

Draft