Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Sorting by publish date doesn't work

Vote:
 

Hi,

I have a problem with sorting my search results. Below you have two types of searching:

Using Unified search:

var unifiedSearch = searchClient.UnifiedSearch()
                .For(model.Query)
                .OrderBy(x => x.SearchPublishDate)
                .GetResult();

Using Search:

var typeSearch = searchClient.Search()
                .For(model.Query)
                .OrderBy(x => x.PublishDate).
                IncludeType(x => new SearchViewModel
                {
                    Name = x.Name,
                    PublishDate = x.StartPublish
                }).GetResult();

In unified search everything works fine. My results are sorted by SearchPublishDate as expected. The problem is in the second query. It returns the same results as the first one but the order is different. It seems to be ordered by score instead of PublishDate. Any sugestions what am I doing wrong?

#189973
Mar 28, 2018 15:05
Vote:
 

The problem is that the property PublishDate doesn't exist in the index. You ned to change your search to search for SitePageData instead, something like this:

var typeSearch = searchClient.Search<SitePageDatal>()
                .For(model.Query)
                .OrderBy(x => x.StartPublish)
                .GetContentResult();

And then do the mapping to SearchViewModel or do a projection directly in the Find query by using the Select extension method.

#189986
Mar 28, 2018 18:20
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.