Gravatar for mark.angus@sitesystems.ca

Question by mark, Sep 12, 2014 5:16 PM

Resorting Facet by Occurrences after setting the selected values programmatically.

Hi, I am setting the facet values programmatically based on user preferences using asp.net c#. Now the occurrences order in both selected and unselected facet items no longer sort in any particular order. Is there a way that I can either pre select the facet values without disrupting the facet native occurrences sorting or programmatically sort the facet values after I have applied the pre-selected values?

Thanks,

protected override void OnInit(EventArgs e) { base.OnInit(e);

        try
        {
            SearchControl sc = sh.ActiveInterface.SearchObject;
            sc.InitializeSettings += this.Search_InitializeSettings; 
        }
        catch (Exception ex)
        {

        }
    }

private void SearchInitializeSettings(object pSender, EventArgs pArgs) { SearchControl sc = pSender as SearchControl; FieldValueFacet item = FindFacet(FacetName);

        Coveo.CES.Web.Search.Settings.FieldValueFacetSetting setting = 
            new Coveo.CES.Web.Search.Settings.FieldValueFacetSetting(item.Field, item.FieldSuffix, false);

        foreach (var country in CountryGUIDs)
        {
            if (!setting.SelectedValues.Contains<string>(country, StringComparer.InvariantCultureIgnoreCase))
            {
                setting.SelectedValues.Add(country);
            }
        }
        sc.State.Staging.Add(setting);

    }
1 Reply
Gravatar for jfcadrin@coveo.com

Answer by JFCG, Sep 22, 2014 8:49 AM

This comes from our engineering team's Ronald Quirion. All credit goes to him.


The effect of your code is to pre-select values in a facet. When a user manually selects values in a facet and finishes working with the facet (e.g. by selecting items in another facet), the selected items are grouped together at the top of the list, and the non-selected items are displayed after. As of today, the ordering within the selected items corresponds to the order in which the items were selected by the user. In your code, that corresponds to the order in which the items are added to setting.SelectedValues. From the InitializeSettings event, it is not possible to sort by document counts, as the query has not been executed yet, so the counts are not known. One way to control how the selected items are sorted is by deriving a class from FieldValueFacet and by overriding:

  • either the GetSelectedValues() method, if you want the selected values to be kept together at the top of the list,

  • or the GetValuesFromResults() method, if you want the selected values to be mixed with the non-selected ones.
    Here is some code that could be added to the CoveoSearch.ascx file of your skin that does that.

Depending on your needs, a simpler solution could be to set the AllowedValues property on the FieldValueFacet instead as shown below:

<script runat="server">
    protected override void OnLoad(EventArgs p_Args) {
        FieldValueFacet facet = (FieldValueFacet) Facet.GetFacetBoundToSetting(this, FieldValueFacetSetting.ComputeName(yourfieldname, yoursuffix));
        facet.AllowedValues = "value1,value2,value3"; // Your foreach loop here...
        base.OnLoad(p_Args);
    }
</script>

The advantage of this technique is that the items would be sorted as configured in the interface editor (e.g. by number of occurrences). But the effect would not be exactly the same. The facet would only contain the items added to the facet's AllowedValues list, and none would be automatically selected. But the default result page would also contain only the ones corresponding to your original code i.e. the ones added to setting.SelectedValues.


Gravatar for jfcadrin@coveo.com

Comment by JFCG, Sep 22, 2014 8:51 AM

Watch out, some of the code somehow got excluded from the code block, near the end…

Gravatar for mark.angus@sitesystems.ca

Comment by mark, Sep 22, 2014 1:14 PM

Hi, this resolved my sorting by occurrences on facet selected values issue.

Thanks a lot,

Mark

Ask a question