How in gods name do you sort by date using examine?
I've probably been googling for hours, and no matter what I tried my custom date NEVER sorted from Examine. First I tried using GatheringNodeData and creating a value that was the date in long format. That didn't work, so I tried hooking into DocumentWriting and adding my own Lucene field. This included NumericField or just Field, but none of these ever work. I also tried using SortableField or just the string name when doing OrderBy, but this also never does anything. When I try sortOrder it always works from Lucene. I don't understand the different. I even tried splitting the date part up so it would be an Integer instead, but this also did nothing. I've used Luke to analyze what's being stored but can't come to any conclusions. How is this so hard?
Sub ExamineEvents_DocumentWriting(ByVal o As Object, ByVal e As DocumentWritingEventArgs)
Dim doc = e.Document
If (e.Fields.ContainsKey("displayDate")) Then
Dim displayDate = CDate(e.Fields("displayDate"))
Dim contentDate As String = displayDate.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture)
Dim displayDateSort = New Field("__Sort_displayDate", contentDate, Field.Store.YES, Field.Index.NOT_ANALYZED)
doc.Add(displayDateSort)
End If
End Sub
And here is how I'm searching:
Dim query = searchCriteria.GroupedOr(docType, docTypeValues).And().OrderByDescending({"__Sort_displayDate"}).Compile()
but your injecting of a sortable field via Document Writing event should also work and looking at your code (other than written in VB - it's been a while), looks correct - if you set a break point on your DocumentWriting event is it firing ?
and do all the documents have a displayDate property ?, I'm wondering if you have a fall back to the date the node was created, to ensure the _SortdisplayDate field always gets added for each node, and always has a value...
I've knocked up a Sample project with latest Umbraco and Fanoe starter kit to replicate what you are doing.
I get the same issue
basically the DocumentWriting part (as you have it, and from the blog post) is all working fine, and as you can see from Luke, or from examine dashboard in Umbraco the new _SortdisplayDate field is getting added correctly to the index.
However when I try to sort on it using the syntax:
var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
ISearchCriteria criteria = searcher.CreateSearchCriteria();
var result = criteria.GroupedOr(new string[] { "__NodeTypeAlias" }, "blogpost");
result = result.And().OrderBy(new string[] {"__Sort_articleDate"});
var searchResults = searcher.Search(result.Compile());
The items are not sorted using the new _SortarticleDate
but obviously if you write something like:
@foreach (var item in searchResults.OrderByDescending(f=>f.Fields["__Sort_articleDate"]){
then they are sorted correctly, so the values of _SortarticleDate are correct...
However even if I sort by '__NodeId' or 'sortOrder' I'm finding that the results aren't sorted; so I think the issue likes in the .OrderBy clause rather than in the DocumentWriting code. very mysterious.
Lol not to beat a dead horse, but that actually didn't fix it. I had to create my own external index, basically following "Examining Examine". Then on the date field I did EnableSorting and Type=DateTime.
I think the issue is that the custom field added to the ExternalIndex wasn't vectorized for some reason, but I could be wrong.
How in gods name do you sort by date using examine?
I've probably been googling for hours, and no matter what I tried my custom date NEVER sorted from Examine. First I tried using GatheringNodeData and creating a value that was the date in long format. That didn't work, so I tried hooking into DocumentWriting and adding my own Lucene field. This included NumericField or just Field, but none of these ever work. I also tried using SortableField or just the string name when doing OrderBy, but this also never does anything. When I try sortOrder it always works from Lucene. I don't understand the different. I even tried splitting the date part up so it would be an Integer instead, but this also did nothing. I've used Luke to analyze what's being stored but can't come to any conclusions. How is this so hard?
This is using the ExternalIndex.
Hi Brad, what are your Examine Settings ?
Have you got in your Examine ExternalIndexSet configuration:
<IndexUserFields> <add Name="customDateField" EnableSorting="true" Type="DateTime" /> ... (other field names) ... </IndexUserFields>
ie for the field you want to sort by you have identified the type, and set EnableSorting to true ?
(you probably have, but just a sanity check)
and Ismail wrote a blog post on injecting a sortable field using Document Writing, http://thecogworks.co.uk/blog/2013/04/11/examiness-hints-and-tips-from-the-trenches-part-10-document-writing-redux if that's worth comparing with what you have tried ?
regards
Marc
I'm actually using the default External index.
Here is how I'm adding the field:
And here is how I'm searching:
Thank you,
Ahh the following didn't come through on my post above:
<add Name="updateDate" EnableSorting="true" Type="DateTime" />
in your examine settings: https://our.umbraco.org/documentation/Reference/Config/ExamineSettings/
but your injecting of a sortable field via Document Writing event should also work and looking at your code (other than written in VB - it's been a while), looks correct - if you set a break point on your DocumentWriting event is it firing ?
and do all the documents have a displayDate property ?, I'm wondering if you have a fall back to the date the node was created, to ensure the _SortdisplayDate field always gets added for each node, and always has a value...
Hi Marc,
They definitely all have the field added through DocumentWriting. Take a look at my screenshot.
You're right I'm not using ExamineSettings I'm doing this from DocumentWriting.
Not sure what to do.
Hi Brad
I've knocked up a Sample project with latest Umbraco and Fanoe starter kit to replicate what you are doing.
I get the same issue
basically the DocumentWriting part (as you have it, and from the blog post) is all working fine, and as you can see from Luke, or from examine dashboard in Umbraco the new _SortdisplayDate field is getting added correctly to the index.
However when I try to sort on it using the syntax:
The items are not sorted using the new _SortarticleDate
but obviously if you write something like:
then they are sorted correctly, so the values of _SortarticleDate are correct...
However even if I sort by '__NodeId' or 'sortOrder' I'm finding that the results aren't sorted; so I think the issue likes in the .OrderBy clause rather than in the DocumentWriting code. very mysterious.
Anyone else have an idea? Should I open a bug report?
Still trying to get this working. It's so much slower to do the sort in .NET then it would be to do it in Lucene. Especially for 10,000+ records.
In case anyone has this problem, I rebuilt the examine index from the developer section and that appears to have fixed it.
Lol not to beat a dead horse, but that actually didn't fix it. I had to create my own external index, basically following "Examining Examine". Then on the date field I did EnableSorting and Type=DateTime.
I think the issue is that the custom field added to the ExternalIndex wasn't vectorized for some reason, but I could be wrong.
Field.Index.NOT_ANALYZED :)
is working on a reply...