0

I'm using Sitecore SXA with a search results component and some out of the box facets. I created a custom token for the search scope used on these category pages. The token looks at a multilist on the current category page and displays the items in the list as search results. It works great. However, I'm trying to maintain the order of items in the multilist on the search results page. I can't seem to figure out the best way to have the sort order just use the order produced by the token below. If I select nothing for the default sort order on the search results component it seems to order them by title.

Any help would be appreciated. Sample of token code below if that is helpful but I assume the sort is overridden elsewhere. Thanks!

foreach (var id in targetIDs)
{
    args.Models.Insert(index, BuildModel(id));
}

protected virtual SearchStringModel BuildModel(string fieldValue)
{
    return new SearchStringModel("id", FormattableString.Invariant(FormattableStringFactory.Create("{0}", fieldValue)))
    {
        Operation = "should"
    };
}

To add to the above. I looked at the component-search-base-model.js file but it appears that works with the sort order for page list, etc, and not search results.

3 Answers 3

1

You can use Boosting to sort results as per the order specified in the multilist using below helper function.

using SolrNet;
using SolrNet.Commands.Parameters;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SolrProvider.SolrNetIntegration;
using Sitecore.Data.Items;

public static void SortItems(Item contextItem)
{
   using (var context = ContentSearchManager.GetIndex("{{Index_Name}}").CreateSearchContext())
   {
       Sitecore.Data.Fields.MultilistField multiselectField = contextItem.Fields["{{MultiList_FieldName}}"];
       Item[] contentListItems = multiselectField.GetItems();

       int totalCount = contentListItems.Length;
       List<ISolrQuery> sqList = new List<ISolrQuery>();
       var queryOptions = new QueryOptions();
       foreach (Item contentListItem in contentListItems)
       {
         var itemId = contentListItem.ID.Guid.ToString("N");
         float boost = (float)totalCount;
         var fieldQuery = new SolrQueryByField("_group", itemId).Boost(boost);
         sqList.Add(fieldQuery);
         totalCount--;
       }

       var solrQuery = new SolrMultipleCriteriaQuery(sqList, SolrMultipleCriteriaQuery.Operator.OR);
       var result = context.Query<SearchResultItem>(solrQuery, queryOptions);
    }
}
                    

Using above function, boost is assigned in the decreasing order to the item ids in the multlist and in turn, query sent to Solr will be in below format.

?q=((_group:(cb9584db34bb43c182cd2b4cf8f80cc3))^3 OR (_group:(8ff7a3edaa4649f1a711ac95756338d8))^2 OR (_group:(3b518a641d1943c3be3c258e2ac41041))^1)

This will returns the items in the order that exist in the multilist field with the help of Boosting factor.

Hope this helps!!! Let me know in case you have any doubts.

6
  • Thanks for the response. I'm aware of the default order selection and adding fields as an option. Unfortunately, what I'm looking for is to order the results by the order they are entered into a mulitlist. A pipe delimited list of guids. If I select nothing for the "default sort order" option in the search results component it defaults to name. What I need is for it to not order the results and use them as they are entered into the search model. If that is not possible, I hoping there is a way to use the order in the multilist as an ordering search facet. But I don't know how that would work. Commented Jun 15 at 17:26
  • @creativeflood You can use boosting to order the result as per the order inside multilist field. I have updated the answer above, that assign boost in decreasing order to the ids in the multilist and results are sorted according to that. Let me know in case you have any doubts.
    – ckhanna
    Commented Jun 16 at 11:20
  • thank you for update. Unfortunately, this will not work in my case. The items in the mulitlist can appear in multiple categories so managing boost would mean creating unique scopes for every category multilist. Plus, I've found that boost is not 100% when it comes to ordering. Odd that it is so difficult to stop Sitecore from forcing an order. The items are added to the model in the correct order but Sitecore orders them by Name even with no default order selected. I will keep looking for a solution and update here if I find one. Thank you again for your suggestions. Commented Jun 16 at 12:51
  • If the items are in a multilist, why are you using the search index to retrieve them? Just get them from the multilist field.
    – Richard Seal
    Commented Jun 16 at 12:54
  • @RichardSeal I'm using many of the built-in SXA facets on these pages with the results. The results were originally coming in through a different scope query but requirements changed. This appeared to be an easy update while still keeping the existing facets in place. It's just the ordering that ended up being the issue. Commented Jun 16 at 13:01
1

As you are using the Sitecore SXA OOTB search component, there are some limitations regarding sorting in Sitecore SXA OOTB component, to sort it out you can use the boosting option in the search scope where you can define to sort order as per your field on top. Another option to sort is to make a computed field and use that to sort your result on the page.

enter image description here

1
  • Thank you for the response. As noted, the items exist in many categories and have different orders in each. Boosting wouldn't work with a single scope and computed fields would get difficult to manage across all the categories. The cleanest and most efficient option was to use processSearchItems as the accepted answer below. Commented Jun 20 at 12:17
0

The answer was the processSearchItems pipeline. Results can be manipulated there.

3
  • Which processor did you patch your custom one after? I've been trying patch:after="processor[@type='Sitecore.XA.Feature.Search.Pipelines.ProcessSearchItems.PrepareSearchItems, Sitecore.XA.Feature.Search'] with no luck Commented Jun 24 at 8:31
  • @MieElMansy my patch after is identical to the one you are using above. Your process code isn't getting hit on debug? Commented Jun 24 at 12:16
  • I had to add this tag to the patch for it to work -> <group groupName="experienceAccelerator">, it worked after that thank you Commented Jun 24 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.