November Happy Hour will be moved to Thursday December 5th.

Conditional Sorting Issue

Vote:
 

I have a requirement to sort products using a condition.

For ex -

First, products need to be sorted from the boolean property Featured

Then by a dynamic product category (coming from a algorithm which decides the category which the user has mostly bought)

Then by the created date

Following is the search query:

Please note that desiredCategory is the product category which describes above (This value is dynamic). 

MainCategoryForFind is the top category of the product

Following exception throws from the query when trying to get the result.

Error - System.ApplicationException: 'Unable to retrieve the field type (such as return value) from expression of type MethodBinaryExpression.'

Then Modified code as below.

it also didn't work. Following error throws when trying to get the result.

System.NotSupportedException: 'Sorting by expressions of type EPiServer.Find.DelegateFilterBuilder is not supported. Change the sorting expression x => x.CourseTypeForFind.Match(value(EPiServer.UTS.Web.UTS.Features.ContentListing.ContentListingService+<>c__DisplayClass5_0`1[EPiServer.UTS.Web.UTS.Features.CourseProduct.CourseProduct]).courseType). Supported types to sort by are native value types such as int, DateTime, nullable versions of the same and strings.'

Is there any other proper way to fulfill this requirement ?

#207525
Edited, Sep 24, 2019 6:26
Vote:
 

Hi,

First, create a new property on the page and fill it with the value that you have for the selected category.

public virtual IEnumerable<string> SearchCategories {
            get {
                var main = !ContentReference.IsNullOrEmpty(this.MainCategory) ? new List<string> { this.MainCategory.ID.ToString() } : new List<string>();
                return main;
            }
        }    

Then on the search function -  you can use "BuildFilter" to do this task.

var categoriesFilterBuilder = _client.BuildFilter<BaseFeedPage>();

if (categories != null)
            {
                foreach (var filter in categories)
                {
                    categoriesFilterBuilder = categoriesFilterBuilder.Or(x => x.SearchCategories.Match(filter));
                }
            }

var searchQuery = this._client.Search<T>();
 searchQuery = searchQuery.Filter(categoriesFilterBuilder);
var batch = searchQuery.GetContentResult();

Regards

Ravindra

#207526
Sep 24, 2019 8:05
Muvindu Amarasinghe - Sep 24, 2019 8:37
Hi Ravindra,
Thanks for your answer.
But I don't need to filter the categories. What I want to do is I need to sort the products by main category (which is indexed) depending on a main category value supply to the query as a parameter. The result should FIRST have the products with the category which was passed as the parameter and then have the products with other categories.
Ravindra S. Rathore - Sep 24, 2019 10:40
Is the main category property not hold the Episerver category?
Muvindu Amarasinghe - Sep 24, 2019 11:06
Main category is a property hold in product.

public abstract class CourseProduct : ProductContent
{
...
.....
public string MainCategoryForFind => this.GetMainCategory();
Ravindra S. Rathore - Sep 24, 2019 11:40
I think then you have apply the orderby after you get the result.
search.GetResult().orderby()
Muvindu Amarasinghe - Sep 24, 2019 11:43
Data load will be high if we do the sorting after getting the result. We only need to take first 4 or 5 products from the sorted result.
Vote:
 

You can't sort a list using a bool by passing it to OrderBy. Like you did in your code

ThenByDescending(x=>x.MainCategoryForFind.Match(desiredcategory))

You can filter the query first and then sort it

searchclient.Search<CourseProduct.CoursProduct>()
.Filter(x=> x.MatchTypeHierarchy(typeof(T)))
.FilterForVisitor()
.Filter(x=>x.MainCategoryForFind.Match(desiredcategory))
.OrderByDescending(x=> x.Featured)
.OrderByDescending(x=> x.MainCategoryForFind)
.ThenByDescending(x=>x.Created);
#207546
Edited, Sep 24, 2019 12:17
Muvindu Amarasinghe - Sep 24, 2019 12:19
Is there any other way to do this ?
Ravindra S. Rathore - Sep 24, 2019 12:52
i have updated the answer
Muvindu Amarasinghe - Sep 24, 2019 13:38
The problem is we cannot filter by the desired category. we only need to sort by desired category, since we need products with other categories as well.
* 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.