Smart search sort order multiple columns

Alistair DeJonge asked on April 27, 2018 15:04

I am trying to sort my search index results by multiple columns, as in sort by name ASC, then by date DESC. Is this possible?

I am passing SearchParameters into the SearchHelper.Search() method like this:

SearchParameters parameters = new SearchParameters()
        {

            SearchFor = searchQuery,
            SearchSort = customSortMethod,
            Path = "/%",
            ClassNames = "",
            CurrentCulture = "EN-US",
            DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
            CombineWithDefaultCulture = false,
            CheckPermissions = false,
            SearchInAttachments = false,
            User = (UserInfo)MembershipContext.AuthenticatedUser,
            SearchIndexes = index.IndexName,
            StartingPosition = ((currentCustomPage - 1) * (SearchResultsPageSize ?? 18)),
            DisplayResults = displayResults,
            NumberOfProcessedResults = 10000,
            NumberOfResults = 0,
            AttachmentWhere = String.Empty,
            AttachmentOrderBy = String.Empty

        };

Correct Answer

Peter Mogilnitski answered on April 27, 2018 15:56

The syntax seems correct. Make sure that they are searchable (and uncheck tokenized if it is checked) and rebuild the index. Check if they work correctly separately. Probably you need to specify type for productdate

0 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on April 27, 2018 15:25 (last edited on April 27, 2018 15:35)

I guess your are looking for SearchParameters.SearchSort, defines the order in which search results are displayed. by default it is ##SCORE## DESC. You can specify one or more search fields (separated by commas) according to which the results will be sorted. The default order is ascending — you can reverse the order by adding the DESC keyword (e.g. articleid DESC).

Note: Fields must be set as Searchable in the smart search field configuration of individual object types to be usable in search sort expressions.

Yes you can SearchSort = "name ASC, then by date DESC", but name and date must be searchable

P.S. If you encounter the "Field <fieldname> does not appear to be indexed" error when using multiple indexes, try specifying the type of the field, for example: (date)documentcreatedwhen

0 votesVote for this answer Mark as a Correct answer

Alistair DeJonge answered on April 27, 2018 15:28

Yes, but I need to sort by one field, then by the other field. I have tried a comma-separated value like:

productname ASC, productdate DESC

That does not seem to work.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on April 27, 2018 20:29

if you are sorting by productname first, then productdate. Do you have multiple product with the same product name? If not, then the productdate sort is pointless.

0 votesVote for this answer Mark as a Correct answer

Vinod Vutla answered on April 30, 2018 07:27

Hi Peter,

I have created a custom smart search index for one of my custom table using https://docs.kentico.com/k10/custom-development/miscellaneous-custom-development-tasks/smart-search-api-and-customization/creating-custom-smart-search-indexes. I am facing a similar issue where I want to do the sorting by ##Score## desc first and then by date desc. No matter what field name you provide after the score, simply it doesn't work.

I have enabled my fields as searchable, I have written the code to index all of the required fields. I couldn't figure out what's going wrong. Please check.

VinodV

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 30, 2018 16:45 (last edited on April 30, 2018 19:26)

Hi Vinod,

It does work it just your score has priority and it is a decimal number. You must have two quite identical records in order to get 2 identical scores, in real life it is quite unlikely to have 2 identical scores. I suggest you output the scores in your transformation: <%# Eval("Score") %> you can also output <%# Convert.ToInt32(ValidationHelper.GetDouble(Eval("Score"), 0) * 100) %>. I did a test and here is the result. So it is all depends on the nature of data (similarity) you are indexing. My example is not from real life (I have 4 strings: "Google URL", "Google URL", "Google URL 3" and "Google URL 4" and they all have different ItemModifiedWhen), i think in order to be sure you need to round scores.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.