Hi I am using Examine to query on a fairly large number or nodes. I would like to sort them by date (a field in my document type) on the Lucene side (not by doing searchResults.OrderBy(result => result.Date) as it would be costly performance wise).
In my search IndexSet the field is declared like so :
In my search code, when I try to pass the OrderBy on the field like so :
var filter = criteria.NodeTypeAlias("Article") .And() .OrderByDescending(new string[] {"datePublication"});
The OrderBy doesn't work. The "Article" node type alias part works fine so I know my filter is at least passed properly to the query but not for the OrderBy. I also tried with "__Sort_datePublication" to no avail.
The only I have gotten this kind of work is by handling the ISearchCriteria object directly through reflection :
List sortFields = new List() { new SortField("datePublication", SortField.STRING, true) };
//criteria is my ISearchCriteria here
var luceneCriteria = criteria as LuceneSearchCriteria;
var sortFieldCollection = luceneCriteria.GetType().GetField("SortFields", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
sortFieldCollection.SetValue(luceneCriteria, sortFields);
This is super nasty.
How does sorting results actually work? How am I supposed to do it normally?
Add extra piece of information : When I look at the indexed content through Luke, I notice a new field "__Sort_datePublication" which seemed like good news except that it doesn't show any value when I query (is that normal?). Screenshot below :
Did u get this working? I managed to use the code below to sort by a DateTime field...
var searcher = ExamineManager.Instance.SearchProviderCollection["SpecialSearcher"];
var searchCriteria = searcher.CreateSearchCriteria();
var query = searchCriteria.Field("__IndexType", "special").And().OrderByDescending(new[] { "updated" }).Compile();
var results = searcher.Search(query);
I had tried with more or less exactly the same code as this. It did work in about 95% of the cases for me. Strangely though, directly after publishing an element it would no longer get sorted properly (sometimes, sometimes it would get sorted too...)
So because it was so unreliable, I ended up sticking with the nasty reflection solution I posted in the original message.
I don't unfortunately. Karl's solution was the closest I came to making it work properly (but it still didn't always work. It only worked on a fresh index -- editing and publishing items then broke the sort order) but I never managed to make it work perfectly.
I ended up using a comparer with a crappy performance...
Examine : Sorting / Ordering results
Hi I am using Examine to query on a fairly large number or nodes. I would like to sort them by date (a field in my document type) on the Lucene side (not by doing searchResults.OrderBy(result => result.Date) as it would be costly performance wise).
In my search IndexSet the field is declared like so :
In my search code, when I try to pass the OrderBy on the field like so :
The OrderBy doesn't work. The "Article" node type alias part works fine so I know my filter is at least passed properly to the query but not for the OrderBy. I also tried with "__Sort_datePublication" to no avail.
The only I have gotten this kind of work is by handling the ISearchCriteria object directly through reflection :
This is super nasty.
How does sorting results actually work? How am I supposed to do it normally?
Add extra piece of information : When I look at the indexed content through Luke, I notice a new field "__Sort_datePublication" which seemed like good news except that it doesn't show any value when I query (is that normal?). Screenshot below :
Did u get this working? I managed to use the code below to sort by a DateTime field...
I had tried with more or less exactly the same code as this. It did work in about 95% of the cases for me. Strangely though, directly after publishing an element it would no longer get sorted properly (sometimes, sometimes it would get sorted too...)
So because it was so unreliable, I ended up sticking with the nasty reflection solution I posted in the original message.
Thanks for the help though!
I tried the solution from Karl but it's not working:
var criteria = index.CreateSearchCriteria("content"); var filter = criteria.NodeTypeAlias(alias).And().OrderByDescending("publishDate");
Any idea how to do this without using a comparer that would have crappy performance due to calling GetPropertyValue too many times?
I don't unfortunately. Karl's solution was the closest I came to making it work properly (but it still didn't always work. It only worked on a fresh index -- editing and publishing items then broke the sort order) but I never managed to make it work perfectly.
I ended up using a comparer with a crappy performance...
Did u mark your publishDate as sortable in the Lucene index? Use luke to look at the Lucene index and make sure sorting is enabled for the field.
Also, another way I've done ordering is when building a raw lucene string and sorting the results object:
Resurrecting an old thread but got here through google and I thought this might help others too.
As seen in on Examine's Tests you can use
SortableField
to define the expected sort column and type.I found the following video very helpful. Posting here in case it might help others: https://vimeo.com/274063980
is working on a reply...