Copied to clipboard

Flag this post as spam?

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


  • Hugo Migneron 32 posts 105 karma points
    Jan 08, 2014 @ 16:10
    Hugo Migneron
    1

    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 : 

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

    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 :

  • Karl Kopp 121 posts 227 karma points
    May 07, 2014 @ 07:38
    Karl Kopp
    0

    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);
    
  • Hugo Migneron 32 posts 105 karma points
    May 07, 2014 @ 15:22
    Hugo Migneron
    0

    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!

  • Andrei 68 posts 130 karma points
    Dec 22, 2014 @ 11:28
    Andrei
    0

    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?

  • Hugo Migneron 32 posts 105 karma points
    Dec 22, 2014 @ 15:53
    Hugo Migneron
    0

    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...

  • Karl Kopp 121 posts 227 karma points
    Dec 24, 2014 @ 04:22
    Karl Kopp
    2

    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:

    var query = searchCriteria.RawQuery(luceneString);
    var results = searcher.Search(query).OrderByDescending(x => x.Fields["featured"]).ThenByDescending(x => x.Fields["updateDate"]);
    
  • Mike B 14 posts 57 karma points
    Dec 27, 2015 @ 14:40
    Mike B
    3

    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.

     var sc1 = sc.ParentId(1143).And().OrderBy(new
         SortableField("sortOrder", SortType.Int)).Compile();
    
  • Rasmus Eeg 91 posts 457 karma points c-trib
    Aug 07, 2019 @ 07:32
    Rasmus Eeg
    0

    I found the following video very helpful. Posting here in case it might help others: https://vimeo.com/274063980

Please Sign in or register to post replies

Write your reply to:

Draft