How To
Facet Option Order
8min
by default, klevu will return the facet options in a sort order based on the number of records each option has in the result ("frequency") an alternative is to list the options in "alphabetical" order klevu's search api query can be modified to allow for alphabeticaly listing the facet options using "index" as the options order value modifying the request in this way will apply to all facets klevu modifyrequest("landing,catnav", function(data, scope){ klevu each(data request current recordqueries, function(key, query){ klevu setobjectpath(query, "filters filterstoreturn options order", "index"); }); }); parameter value description freq sort the options based on the number of corresponding records in the result set index sort the options alphabetically see modify request overview docid ndrepmgauhyltilxkg o for more details ordering options for a specific facet there may be instances where a specific facet can benefit from alphabetical sorting apart from the frequency default the below example will modify a specific facet and sort the filter options in alphabetical order for the filter key "category" klevu modifyresponse("landing,catnav", function(data, scope){ / loop through filters in the response / klevu each(data template query productlist filters, function(indx, filter) { if (filter key === 'category') { 	 	 / js sort object by 'name' value / 	 filter options sort(function (a, b) { 	 if (a name < b name) return 1; 	 if (a name > b name) return 1; 	 return 0; 	 }); 	 } }); }); predefined option ordering there may be instances when neither frequency ("freq") or alphabetical ("index") sorting is sufficient a common use case is a facet for size a predetermined option sorting for size can be set for an order in which retail shoppers are accustomed example 1 this example sets the filter options to a predetermined order for the filter key "size" important all available facet options should be included to accommodate variances within the results also, consider case sensitivity as it applies klevu modifyresponse("landing,catnav", function(data, scope){ / loop through filters in the response / klevu each(data template query productlist filters, function(indx, filter) { if (filter key === 'size') { 	 	 / set desired order for 'size' facet options / 	 const optionorder = \["x small","small","medium","large","x large"]; 	 optionorder reverse() foreach(function(elm) { 	 	 	 / js sort object by 'name' matching the optionorder value / 	 filter options sort(function (a, b) { 	 if (a name === elm) return 1; 	 if (a name !== elm && b name !== elm) return 0; 	 return 1; 	 }); 	 }); 	 } }); }); example 2 this example sets the facet options to a predetermined order for the filter key "size", but also considers any range of numerical sizes as well the following snippet separates the the alphabetic from the numeric filter option values (based on name ) sets the alphabetic object to the defined sort order ( optionsorder ) sorts the numeric object to numerical order concatenates the two objects prior to setting to the filter options important all available facet options should be included to accommodate variances within the results also, consider case sensitivity as it applies klevu modifyresponse("landing,catnav", function(data, scope){ / loop through filters in the response / klevu each(data template query productlist filters, function(indx, filter) { if (filter key === 'size') { 	 	 / set desired order for 'size' facet options / 	 const optionorder = \["x small","small","medium","large","x large"]; 		 	 / get the filter options that are not numeric / 		 	 const alphabetic = filter options filter(value => { 	 return isnan(value name) 	 }); 		 	 / sort object by 'name' value matching desired sortorder / 	 optionorder reverse() foreach(function(elm) { 		 	 alphabetic sort(function (a, b) { 	 if (a name === elm) return 1; 	 if (a name !== elm && b name !== elm) return 0; 	 return 1; 	 }); 		 	 }); 	 / get the filter options that are numeric / 	 const numeric = filter options filter(value => { 	 return !isnan(value name) 	 }); 	 	 / sort object by 'name' value ascending / 	 numeric sort(function (a, b) { 	 var numberoptiona = +a name ? +a name 1000; 	 var numberoptionb = +b name ? +b name 1000; 	 return numberoptiona numberoptionb 	 }); 	 / concatenate the two sorted objects / 	 const orderedoptions = numeric concat(alphabetic); 	 filter options = orderedoptions; } }); });