Copied to clipboard

Flag this post as spam?

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


  • jake williamson 144 posts 625 karma points
    1 week ago
    jake williamson
    0

    how to enable lucene sorting on date fields in umbraco 8?

    hey out there,

    so... i'm trying to get to grips with the new examine set up in v8. i'm getting there but hit a blocker when it comes to dates and date sorting.

    back in the v7 days we'd add something like this to <IndexAttributeFields> in config\ExamineIndex.config:

    <add Name="postDate" EnableSorting="true" Type="DateTime" />
    

    trick is, we no longer have the config\ExamineIndex.config file in v8!

    i've worked through the code at https://our.umbraco.com/documentation/reference/searching/examine/examine-events and it feels like it's something i maybe have to do here now?

    the other issue is the way the dates are stored. interestingly, date picker fields are being saved in a different format to v7:

    enter image description here

    in v7 it looks like:

    enter image description here

    at the moment i'm getting String was not recognized as a valid DateTime errors when attempting to format my postDate field.

    ultimately i want to be able to sort on the postDate field and i'm wondering if once i get that sorted the format will change?

    as ever, any suggestions are greatly received ;)

  • Shaishav Karnani from digitallymedia.com 273 posts 1198 karma points
    1 week ago
    Shaishav Karnani from digitallymedia.com
    1

    Hi Jake,

    I have created a field postDate and tried below query. It is returning me results in postDate descending order. So, by default, it seems to work fine and no need to have additional EnableSorting="true".

    var results = examineSearcher.CreateQuery("content").Field("__IndexType", "content").OrderByDescending(new SortableField[] { new SortableField("postDate") }).Execute();
    

    Please try and let us know your views.

    Regards,

    Shaishav

  • Nik 1108 posts 4610 karma points MVP 2x c-trib
    1 week ago
    Nik
    1

    Hi Jake,

    By default Umbraco's external index is storing all fields as a text string that is "non-sortable". Shaishav's method kinda overrides that a little bit but I believe it is only sorting it in an alphabetical approach.

    Because in V8 of Umbraco the config files for Examine are gone, you now need to write C# code to modify how indexing occurs, such as naming a specific field to be a specific type etc.

    In order to do this you need a custom Component and Composer.

    public class IndexerComponent : IComponent
    {
        private readonly IExamineManager examineManager;
    
        public IndexerComponent(IExamineManager examineManager)
        {
            this.examineManager = examineManager; 
        }
    
        public void Initialize()
        {
            var externalIndex = examineManager.Indexes.FirstOrDefault(i=>i.Name == "ExternalIndex");
            if(externalIndex != null)
            {
                externalIndex.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("postDate", FieldDefinitionType.Long);
            }
        }
    
        public void Terminate(){}
    
    }
    

    You might get away with just this, but I believe you also still need Shaishav's code to tell the search to sort by it.

    Thanks

    Nik

  • jake williamson 144 posts 625 karma points
    1 week ago
    jake williamson
    100

    thank you the replies chaps ;)

    shaishav, you and i had come to the same conclusion! i'd been messing around with the OrderByDescending stuff and came up with identical code which i thought was working...

    however i realised that all the test items i had used dates that were organically ordering rather than being in a correct order...

    nik, interestingly i found a post from you back in july https://our.umbraco.com/forum/umbraco-8/98167-using-rangequery-on-custom-dynamically-added-fields-umbraco-8 outlining the externalIndex.FieldDefinitionCollection.AddOrUpdate solution.

    so i've implemented the above and it appears to be working - happy days!

    however...

    the weird thing is that the postDate field no longer appears in the back office:

    enter image description here

    and in luke it doesn't appear to have a value:

    enter image description here

    to get it actually outputting a value i can see, i've changed the property name to postDateForSorting and then set the value as part of calling IndexProviderTransformingIndexValues. the date value is formatted using an idea taken from https://stackoverflow.com/questions/4565303/lucene-net-how-can-i-add-a-date-filter-to-my-search-results

    e.ValueSet.TryAdd("postDateForSorting", ((DateTime)value).ToString("yyyyMMddHHmmssfff"));
    

    i can now see the value in the backoffice and in luke:

    enter image description here

    and sorting on the new postDateForSorting also appears to be working.

    i'm still slightly mystified as to what formatting is being applied to the createdDate and updateDate fields that the core creates?!

    seems dates and lucene are a bit of a pain one way and another though!

    this feels like it might be solved, if anything else comes up with the dates i'll post a (ahem) update.

    thanks for your help chaps ;)

Please Sign in or register to post replies

Write your reply to:

Draft