Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1465 posts 1887 karma points
    Jan 10, 2013 @ 17:41
    Gordon Saxby
    1

    How do I sort Examine query by Date

    I have the following code but need to make it sort the results in descending date order. There is a field added to the index called "date" - 

    var sc = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].CreateSearchCriteria(global::Examine.SearchCriteria.BooleanOperation.Or);

    var terms = searchTerm.Split(' ');

    var query = sc

                .Field("title", searchTerm)

                .Or()

                .Field("introduction", searchTerm)

                .Or()

                .Field("shortintro", searchTerm);                        

     

        foreach (var term in terms)

        {

            if (term != "")

            {

                query = query.Or().NodeName(term);

            }

        }

        var results = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].Search(query.Compile()); 

     

    I have tried all sorts of things but everything seems to give the error:

    String was not recognized as a valid DateTime.

    I have seen a number of places via Google that sem to offer a solution but nothing seems to work ... ??!!

  • Mike Taylor 155 posts 353 karma points
    Jan 11, 2013 @ 11:28
    Mike Taylor
    104

    Hi Gordon

    First of all, in your /config/ExamineIndex.config you need to add your date field to the <IndexUserFields> section to make it sortable, like this:

    <IndexSet SetName="..." IndexPath="...">
    <IndexAttributeFields>
    ...
    </IndexAttributeFields>
    <IndexUserFields>
    <add name="dateField" EnableSorting="true" />
    </IndexUserFields>
    <IncludeNodeTypes>
    ...
    </IncludeNodeTypes>
    <ExcludeNodeTypes />
    </IndexSet> 

     

    Note the EnableSorting="true" parameter...

    I tend to use raw lucene syntax queries, rather than the dot notation, so I use this (where sb.ToString() is my query syntax):

    var results = searchProvider.Search(searchCriteria.RawQuery(sb.ToString())).OrderBy((r => r.Fields["myDateField"]));

    or

    var results = searchProvider.Search(searchCriteria.RawQuery(sb.ToString())).OrderByDescending((r => r.Fields["myDateField"]));

     

    Not sure if this works with the dot notation though...

    Mike

  • Gordon Saxby 1465 posts 1887 karma points
    Jan 11, 2013 @ 12:21
    Gordon Saxby
    0

    Hi Mike,

    thanks - it appears that the main reason I couldn't get it to work was that I had included Type="DateTime" on the index definition. I took that out and updated my code as below and it now seems to work :-)

    var results = Examine.ExamineManager.Instance.SearchProviderCollection["MySearcher"].Search(query.Compile()).OrderByDescending((r => r.Fields["date"]));


  • Mike Taylor 155 posts 353 karma points
    Jan 11, 2013 @ 12:31
    Mike Taylor
    0

    Excellent - glad you got it working :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies