Developer forum

Forum » Ecommerce - Standard features » Sort products by productnumber with custom value

Sort products by productnumber with custom value

Andreas Rundgren
Reply

Hi,
We want to sort the products in frontend after productnumber. The problem is our productnumber can look like this: 737373_Model1 and 848949_Model2 for example.

So when we now sort on productnumber it does not seems that it can handle it correct due to the underscore and text. Anyone have some tips if I can get it work somehow? 
Or do i need to save the productnumber in an new customfield with only the number and then use that to sort on in frontend?

Regards
Andreas


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Andreas

How are they sorted when you sort by name - and how should they be sorted?

If you are sorting using the index, and the field is analyzed, it will sort wrong. So also check if fields you are sorting by are not analyzed in the index.

BR Nicolai

Votes for this answer: 1
 
Andreas Rundgren
Reply

Hi,

Sort by productname seem to work correct.

In my index product name and product number look like this. So i should remove "analyzed" from product number field?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Yes. product number should not be analyzed.

There is some background info here: https://doc.dynamicweb.dev/documentation/extending/indexing/howluceneworks.html

You shouldn’t rely on analyzed fields for sorting, because sorting expects one stable value per document, not multiple tokens.

For example, if you index a product number as a TextField ("ABC-123"), the analyzer might split it into tokens ("abc", "123"), which makes sort behavior undefined. If instead you use a StringField (not analyzed) or add a SortedDocValuesField with the same value, sorting will be consistent and correct. In practice, "12345", "23456", "34567" will sort identically regardless of analysis if stored as keywords, but once you introduce analyzers that tokenize or normalize values like "ABC-123" vs "ABC 123", analysis no longer affects sorting directly—it just breaks the assumption that there’s only one term to sort on. The safe pattern is: use analyzed fields for searching, and separate non-analyzed fields for sorting.

 
Andreas Rundgren
Reply

Hi,

Thank you for that. Works like a charm now with my new not analyzed productnumber field.

Regards
Andreas

 

You must be logged in to post in the forum