getting dtsearch in sitecore to sort items by createdDate

351 Views Asked by At

The documentation for dtSearch is a bit confusing on this. I am trying to get items returned by dtSearch to return in order of of the date created descending(so newest first). Right now the engine.Search method appears to not contain any information about the date at all in the results returned.

I understand I need to use advanced options when creating the index to get the date field in there so I can sort by that, but how do I do that?

I see this: http://support.dtsearch.com/dts0150.htm But am not sure where or how to apply it. I don't have the referenced demo from the document could anyone out there show how I would add a date to the index?

1

There are 1 best solutions below

0
Younes On

To be able to sort on this (for dtSearch) custom field, you need to add a meta tag with the Creation Date to the pages that get indexed by dtSearch. You can get the dtSearch documentation and check how that's done in there.

You meta tag would look something like this:

<meta id="scDateCreated" name="scDateCreated" content="20100629" />

In the dtSearch crawler tool you can then specify that this meta tag (field) should be indexed. After dtSearch indexed this field, you can use this field to be able to sort your searchresults by the Date the item / page is created. Be aware that if you are using a wildcard setup (/* in url) to show items from a different datasource on a wildcard item, you have to get the created date from the item you are showing on your wildcard and not the Sitecore.Context.Item.

Example code to sort on date:

ISearch engine = this.GetEngine();

        // Search with the given searchPhrase and the set SearchOptions
        engine.Search(searchPhrase, this.searchOptions);      

        // If there are searchResults return the searchResults formatted
        if (engine.SearchResults != null)
        {
            return this.FormatSearchResults(engine.SearchResults, engine, searchPhrase, templateId, publishedFrom, publishedTo, specifiedPath, sortOrder);
        }

This will get your results. Now sorting (this.FormatSearchResults):

// If a templateId was given
            if (templateId != string.Empty)
            {
                list = xmlResult.SelectNodes("/sitecore/result/item[scWebsitePath='" + sitecoreContextItemPath + "' and scTemplateId='" + templateId + "' and scDateCreated > '" + publishedFrom + "' and scDateCreated < '" + publishedTo + "']");
            }
            else
            {
                list = xmlResult.SelectNodes("/sitecore/result/item[scWebsitePath='" + sitecoreContextItemPath + "' and scDateCreated > '" + publishedFrom + "' and scDateCreated < '" + publishedTo + "']");
            }  

As you see the meta tags will be present in the XML that this searchEngine will return. You can get your own class to be able to cast your dtSearchResult to a List and then use Linq to sort.