Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Sep 29, 2015 @ 14:26
    Jason Espin
    0

    Examine fails to index custom fields correctly

    Hi all,

    I have a large number of fields across different document types that should be indexed in my custom indexer but Examine is not indexing them correctly.

    enter image description here

    It is very strange as the Umbraco back end suggests the fields are being indexed (as shown above - visitedCities, visitedCountries) however when I run the following code and look for the key "visitedCities" I get a key not found exception.

    public static class ExamineEvents
        {
            public static void GatheringContentData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
            {
                IPublishedContent content = null;
                IPublishedContent homepage = null;
    
                try
                {
                    content = helper.TypedContent(e.NodeId);
                    homepage = content.AncestorOrSelf("homepage");
                    e.Fields.Add("siteNode", homepage.Id.ToString());
                }
                catch (System.NullReferenceException ex)
                {
                    if (e.Fields["level"] != "1")
                    {
                        IContentService cs = ApplicationContext.Current.Services.ContentService;
                        IContent[] unpublishedPageAncestors = cs.GetAncestors(e.NodeId).ToArray();
                        if (unpublishedPageAncestors.Where(x => x.ContentType.Alias.ToLower() == "homepage").Count() == 1)
                        {
                            int id = unpublishedPageAncestors.First().Id;
                            e.Fields.Add("siteNode", id.ToString());
                        }
                    }
                    else
                    {
                        e.Fields.Add("siteNode", e.Fields["id"]);
                    }
                }
                catch (Exception ex)
                {
                    // Do Nothing
                }
    
                if (e.Fields["nodeTypeAlias"] == "package")
                {
                    try
                    {
                        if (!e.Fields["visitedCities"].IsNullOrWhiteSpace())
                        {
                            List<string> countries = new List<string>();
                            List<string> cities = e.Fields["visitedCities"].Split(',').ToList();
                            foreach (string city in cities)
                            {
                                IEnumerable<IPublishedContent> nodes = homepage.Descendants("city").Where(x => x.GetPropertyValue<string>("cityCode") == city);
                                if (nodes.Count() > 0)
                                {
                                    try 
                                    {           
                                        IPublishedContent page = nodes.First();
                                        IPublishedContent country = page.Ancestor("country");
                                        countries.Add(country.GetPropertyValue<string>("countryCode"));
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
    
                            string csv = String.Join(",", countries.Distinct().ToArray());
                            e.Fields["visitedCountries"] = csv;
    
                        }
                    }
                    catch (Exception ex)
                    {
    
                    }
                }
            }
        }
    

    Why is examine not indexing these fields correctly and how can I force it to do so?

    By default, Umbraco seems to index all custom fields but some it just randomly ignores. I have tried explicitly adding the fields that are missing to the ExamineIndex.config file using the IndexUserFields field however this then causes all other custom fields to be ignored and it still fails to index the fields I have specified.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 29, 2015 @ 14:42
    Ismail Mayat
    0

    Jason,

    Add break point to your code then publish just one item that has the fields and see if it hits your break point then step through.

    Regards

    Ismail

  • Jason Espin 368 posts 1335 karma points
    Sep 29, 2015 @ 15:29
    Jason Espin
    0

    Hi Ismail,

    I have already done that and unfortunately it never hits because it is not detecting that field. I have checked the index with Luke and the fields are being indexed but Umbraco doesn't seem to be detecting them properly in Examine.

    Cheers,

    Jason

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 29, 2015 @ 15:44
    Ismail Mayat
    0

    What kind of data should be in there is it country / city codes?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 29, 2015 @ 15:46
    Ismail Mayat
    0

    Also you should always do check for the field because if you have content that does not have the value then the field will not present in the dictionary.

  • Jason Espin 368 posts 1335 karma points
    Sep 30, 2015 @ 09:04
    Jason Espin
    0

    Hi Ismail,

    It is a CSV string of values that list country, city nodes. I can see the field is there in the index but Umbraco fails to even look for it.

    Cheers,

    Jason

  • Francis 12 posts 43 karma points
    Apr 10, 2017 @ 09:48
    Francis
    0

    Was a fix ever found for this? I get the same issue on Umbraco 7.5.11: indexed field shows in Luke from a custom indexer, but when searching, it is not found.

  • Andy Carlier 16 posts 74 karma points
    Aug 16, 2017 @ 08:11
    Andy Carlier
    0

    I just had this issue and (thanks to Ismail's comment) added a field check for the field I was looking for....

    if (result.Fields.Where(f => f.Key == "myField").Any()){
          // do stuff here
    }
    

    It only take a single node without that field to kill the loop so even if you can see results in Luke or in the Examine Management panel unless they ALL do you're in trouble.

Please Sign in or register to post replies

Write your reply to:

Draft