Tuesday, March 8, 2016

Sitecore Lucene search index when an item was last updated

With your Sitecore search it can be useful to the end user to provide sorting based on the last updated date of the Sitecore item. A good use case of this is actually when using Google to search for Sitecore help - if you set the results to show from the past year, you are more likely to get results relating to the current version of Sitecore.

To enable sorting by updated date in Sitecore you will need to first, add the __smallupdateddate field to your search Model. Please note that this is small updated date... and is a default field that is included in the Lucene index.
public DateTime LastUpdated { get; set; }
Once this has been added, you can then sort by this with your search query:
searchResults = searchContext.GetQueryable<SearchModel>().Filter(searchPredicate).OrderBy(x => x.LastUpdated);
For more complex logic such as Google's last year filtering, you could use the where clause in your search logic:
.Where(x => x.LastUpdated >= DateTime.Now.AddYears(-1))

No comments:

Post a Comment