Gravatar for puneetpande@gmail.com

Question by ppande, Jun 19, 2015 4:24 PM

OrderByDescending and Date fields

Hi,

We're trying to sort on a datefield in our search results, but it's not working. Here's the code:

var results = context.GetQueryable<SearchResultItem>().Where(
    item => item.Paths.Contains(articleFolderId) &&
            item["categorylist"] == topicID &&
            item.Language == Sitecore.Context.Language.Name).ToList();

results = results.Where(x => x.CreatedDate != null).OrderByDescending(y => y.CreatedDate).ToList();

We've added this field in the fieldMap section of the search provider config.

<fieldType fieldName="CreatedDate" isFacet="false" isMultiValue="false" isSortable="true" settingType='Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework'/>

Is there something we're missing?

Thanks.

2 Replies
Gravatar for lbergeron@coveo.com

Answer by Luc Bergeron, Jun 19, 2015 5:07 PM

Hi,

I guess you should call the .OrderByDescending() method before doing the first call to .ToList(). With your actual code, the first call to .ToList() will call the search index and retrieve the results. Note that, with Coveo, the index will return 10 results only. When you call .ToList() the second time, the results are coming from an internal list, not the search index. So it sorts only the 10 results that were returned previously.

I hope this helps

Gravatar for puneetpande@gmail.com

Comment by ppande, Jun 19, 2015 5:23 PM

We've set the 1000

So, ToList() should be returning all the results.

Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Jun 22, 2015 8:51 AM

Yes indeed and it works fine for me. Can you be more specific regarding what is not working? Did you try sorting on another field?

Under the hood, the CreatedDate is using the __smallcreateddate index field. In the latest releases of Sitecore, a format string was applied on this field.

  • Which product version are you using (Sitecore and Coveo for Sitecore)?
  • Can you validate that the __smallcreateddate field appear as a Date/time in the fieldset. To find it, open the CES Admin Tool > Configuration > Fields and then select the fieldset for the source you are using.

Thanks

Gravatar for sergio.bazan@accenture.com

Answer by sergio bazan, Jun 23, 2015 2:21 PM

Hi guys,

The thing is we have a custom field to order the collection, called "createddate". We tested the solution of ordering before the call to the ToList() method and still the same order. Also we thought it was something related to the DateTime type but with the .Ticks property is still the same order. We looked into the CES Admin Tool > Configuration and the field is set to IsSortable true. Any thoughts?

Thanks! Regards, Sergio

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jun 23, 2015 2:55 PM

Is the field also of type "Date/time" in CES?

Gravatar for sergio.bazan@accenture.com

Comment by sergio bazan, Jun 23, 2015 3:57 PM

Yes, is type Date/time. "CreatedDate Date/time"

Gravatar for fcote@coveo.com

Comment by fcote, Jun 25, 2015 11:27 AM

What are the versions of Sitecore, Coveo for Sitecore and CES you're using in that environment?

Gravatar for sergio.bazan@accenture.com

Comment by sergio bazan, Jun 25, 2015 3:24 PM

Hello,

We are using:

Sitecore 7.2 Coveo 7.0 x64 7402.0

Regards.

Gravatar for nicolas.a.slavik@accenture.com

Comment by yonico, Jun 25, 2015 4:04 PM

the field you are looking for is inside Fields (there appear the ones custom). It's a coincidence to have the same one. Do you know what the best way to access to Field collection and use it in the orderby?

                results = results.Where(x => x["createddate"] != null).OrderByDescending(item => item["createddate"]).ToList();
Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Jun 26, 2015 9:50 AM

As @yonico pointed out, one can get confused with the SearchResultItem.CreatedDate property and the CreatedDate custom field.

The snippet below will sort on the __smallcreateddate field.

results = queryable.OrderByDescending(x => x.CreatedDate)

This snippet will use the custom field.

results = queryable.OrderByDescending(x => x["CreatedDate"])

I hope this helps.

Gravatar for nicolas.a.slavik@accenture.com

Comment by yonico, Jul 1, 2015 10:17 AM

didnt return the sorted values and no runtime errors.

           var results = context.GetQueryable<SearchResultItem>().Where(
            item => item.Paths.Contains(articleFolderId) &&
            item["categorylist"] == topicID &&
               item.Language == Sitecore.Context.Language.Name
              ).OrderByDescending(item=>item["CreatedDate"]).ToList();

I checked in CES and the field appears as Sorted. What else could be missing?

Field configuration: fcreateddate86456 CreatedDate Date/time Sorted

Should we cast this to date? is there a way?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 5, 2015 11:35 AM

Did you re-index all your Sitecore indexes after setting the field as a sortable field? CES documents that were indexed before the field was set as sortable will not be sortable until they get re-indexed completely because CES fills its sortable data structures at indexing time.

Ask a question