How to sorting by selected category

Hello

I have a product model and in this model I have categories of array type. Within these categories, each product has a separately assigned displayOrder. How can I sort by the displayOrder property of the selected category when sorting in search results on Algolia.

Thank you

Hi @technical.resources

Can you give us more details about the use case you are trying to achieve with this model? Maybe also you can share some records sample to help us visualise what you’re trying to do?

Also, to achieve the best performance possible, we pre-compute your sort criteria at indexing-time. It means that each index has a unique sorting strategy. You can find more information about this topic here: https://www.algolia.com/doc/guides/relevance/sorting/#multiple-sorting-strategies https://www.algolia.com/doc/faq/index-configuration/what-are-replicas-indices-and-what-are-their-benefits/#faq-section

Thank you for response.

Sample data as follows:

Active url: ?dFR[CategoryModels.Name][0]=Blouses&

The type of sort I want: CategoryModels.Name == ‘Blouses’ && CategoryModels.Name[“Blouses”].DisplayNumber

In the project, products are displayed in different categories in different orders. For example, I would like to see 1st rank in clothing category A and 15th rank in dress category. In other words, I index Algolia with the knowledge that the product and the category relation appear in the order of the related category.

So I would like to have product sorting on selected category basis.

{
“Id”: 27639,
“LanguageId”: 0,
“Name”: “BLOUSE”,
“NameAlternative”: null,
“SeName”: “blouse”,
“Gender”: null,
“Url”: “http://www.adl.com.tr/en/p/blouse-27639”,
“ViewCount”: 0,
“SaleCount”: 0,
“Sku”: “8681249770507”,
“Barcode”: null,
“ModelNumber”: “91532339000”,
“CreatedDate”: “2017-06-01T08:51:15.33”,
“UpdatedDate”: “0001-01-01T00:00:00”,
“Published”: true,
“SwatchImageCode”: null,
“ShortDescription”: “”,
“FullDescription”: “Tanımsız”,
“DetailedDescription”: null,
“MetaKeywords”: null,
“MetaTitle”: null,
“MetaDescription”: null,
“Badge1”: null,
“Badge2”: null,
“Badge3”: null,
“Badge4”: null,
“Badge5”: null,
“Text1”: null,
“Text2”: null,
“Text3”: null,
“Text4”: null,
“Text5”: null,
“Attr1”: [
“adL EXTRA”
],
“Attr2”: [
“Striped Black”
],
“Attr3”: [
“L”,
" M",
" S",
" XS"
],
“Attr4”: [
“2017 SUMMER”
],
“Attr5”: [
“TOPS”
],
“Attr6”: [
“Blouse”
],
“Attr7”: [
“ADİLIŞIKextra”
],
“Attr8”: ,
“Attr9”: ,
“Attr10”: ,
“Attr11”: ,
“Attr12”: ,
“Attr13”: ,
“Attr14”: ,
“Attr15”: ,
“Attr16”: ,
“Attr17”: ,
“Attr18”: ,
“Attr19”: ,
“Attr20”: ,
“Price”: 39.9,
“OldPrice”: 39.9,
“SpecialPrice”: 0,
“SpecialOldPrice”: 0,
“PriceFormatted”: “39,90 TL”,
“OldPriceFormatted”: “39,90 TL”,
“SpecialPriceFormatted”: null,
“SpecialOldPriceFormatted”: null,
“SpecialPriceStartDate”: “0001-01-01T00:00:00”,
“SpecialPriceEndDate”: “0001-01-01T00:00:00”,
“TotalStockQuantity”: 272,
“HasStock”: true,
“DiscountAmount”: 0,
“DiscountPercentage”: 0,
“ManufacturerId”: 0,
“StoreId”: 0,
“VendorId”: 0,
“WarehouseId”: 0,
“VisibleIndividuallyOnly”: false,
“MarkedAsNewOnly”: false,
“FeaturedProducts”: null,
“PictureModels”: [
{
“Id”: 109841,
“Type”: 0,
“ImageUrl”: “https://img-adl.mncdn.com/thumbs/kolsuz_atlet_csiyah_109841_500.jpeg”,
“ThumbnailUrl”: “https://img-adl.mncdn.com/thumbs/kolsuz_atlet_csiyah_109841_370.jpeg”,
“FullSizeUrl”: “https://img-adl.mncdn.com/Assets/Base/Originals/109841_0.jpeg”,
“Title”: null,
“AlternateText”: null,
“DisplayOrder”: 0
},
{
“Id”: 109842,
“Type”: 0,
“ImageUrl”: “https://img-adl.mncdn.com/thumbs/kolsuz_atlet_csiyah_109842_500.jpeg”,
“ThumbnailUrl”: “https://img-adl.mncdn.com/thumbs/kolsuz_atlet_csiyah_109842_370.jpeg”,
“FullSizeUrl”: “https://img-adl.mncdn.com/Assets/Base/Originals/109842_0.jpeg”,
“Title”: null,
“AlternateText”: null,
“DisplayOrder”: 0
}
],
“FilterModels”: ,
“CombinationModels”: null,
“CategoryModels”: [
{
“Id”: 250895,
“Name”: “Clothıngs”,
“SeName”: null,
“DisplayNumber”: 30
},
{
“Id”: 250908,
“Name”: “Blouses”,
“SeName”: null,
“DisplayNumber”: 60
},
{
“Id”: 250816,
“Name”: “New Arrıvals”,
“SeName”: null,
“DisplayNumber”: 40
}
],
“ManufacturerModels”: ,
“TagModels”: ,
“ImageUrl”: “https://img-adl.mncdn.com/thumbs/kolsuz_atlet_csiyah_109841_500.jpeg”,
“HierarchicalCategories”: {
“Lvl0”: “Clothıngs”,
“Lvl1”: “Clothıngs > New Arrıvals”,
“Lvl2”: “Clothıngs > New Arrıvals > Blouses”,
“Lvl3”: “”,
“Lvl4”: “”
},
“CustomDisplayOrder”: 73
}

You’ll need to duplicate records to order products differently depending on the category the user is looking at.

It means that you will have one record per product, and one additional record per product per category it has a special position for.

Let’s say you sell clothes online and you have a pair of sports shoes for men. You want those shoes to be gradually ranked higher when going down from a global search to a search within the “Shoes” category and then within the “Shoes > Sports” category.

You’ll need three records for those shoes.
The “main” one that will address the global search case and all categories other than “Shoes” and “Shoes > Sports”:

{
  "sku": "4597310",
  "name": "Sports shoes",
  "ranked_category": "none",
  "sales_count": 852
}

The one for the “Shoes” category:

{
  "sku": "4597310",
  "name": "Sports shoes",
  "ranked_category": "Shoes",
  "category_rank": 20,
  "sales_count": 852
}

And the one for the “Shoes > Sports”:

{
  "sku": "4597310",
  "name": "Sports shoes",
  "ranked_category": "Shoes > Sports",
  "category_rank": 1,
  "sales_count": 852
}

All products will have at least one record like the “main” first one. Only if they are ranked specifically for a category will they have extra records such as the second or third ones.

Then you need some configuration on your index:

  1. In your index configuration, add category_rank (asc) on top of your other custom ranking attributes. Here a lower value means a higher position in the results.

  2. Add ranked_category to the attributes for faceting, using the onlyFilter modifier to tell the engine we will not need the facet values and counts for this facet => onlyFilter(ranked_category).

  3. Set Distinct to true.

Finally you’ll query differently your index depending on the category you are on:

If there’s no category filter in effect, you can use the filters search parameters like so:

filters: "ranked_category:none"

If there’s a category filter in effect you can use the filters search parameters like so:

filters: `category:${currentCategory} AND (ranked_category:${currentCategory} OR ranked_category:none)`

Because you set Distinct to true, the specialized record will always be ranked higher than the other one and thus kept by distinct.

Let us know if this help, and I would take this occasion to thanks @olance a lot for this guide.

1 Like