Gravatar for mark.angus@sitesystems.ca

Question by mark, Sep 22, 2014 4:02 PM

Respect 'Additional Values Increment' after programmatically setting facet selected values in Search_InitializeSettings

Hi, I have roughly 183 items which I am using to populate a facet. It is essential that the more/fewer options are available to the user. I also have to preselect facet values based on user preferences.

The issue that I am having is that in order to show all the selected values defined in the user profile, I have to programmatically set the number of additional values and number of values to 180. After doing so, the facet no longer respects the ‘Number of values’ and ‘Additional Values increment’ properties set in the Coveo front-end. Also when you expand the facet, all items are shown.

I am setting the values in the Search_InitializeSettings. I there a way to not only display all the selected items but respect the settings defined in Coveo front-end configuration?

Thanks

private void SearchInitializeSettings(object pSender, EventArgs pArgs) { SearchControl sc = pSender as SearchControl; FieldValueFacet item = null; FieldValueFacetSetting myFacetSetting = null;

        item = FindFacet(CountryFacetName);

        myFacetSetting = new FieldValueFacetSetting(CountryQueryKey, false);

        foreach (string id in UserCountries)
        {
            myFacetSetting.SelectedValues.Add(id.ToUpper());
        }
        sc.State.Staging.Add(myFacetSetting);

        if (FacetSettings == null)
        {
            item.NumberOfAdditionalValues = 183;
            item.NumberOfValues = 183;
            if (!item.IsCollapsed)
                item.IsCollapsed = true;
        }
        FacetSettings = Initizalized;
    }
Gravatar for mlaporte@coveo.com

Comment by Martin Laporte, Sep 23, 2014 3:15 AM

I have to programmatically set the number of additional values and number of values to 180

I'm not sure what you're referring to here --- can you include some code?

2 Replies
Gravatar for rquirion@coveo.com

Answer by ronald, Oct 1, 2014 4:17 PM

Hi Mark,

First, I'm looking at your code excerpt, and I see a detail which might be a problem.. The InitializeSettings event is called only when the search page is initially loaded (initial post back). During the next post backs (for example, if I ask to show the second results page, or if I click on a facet item), the InitializeSettings event will not be called again. That event is intended to create search settings, like the FieldValueFacetSetting that you are creating, and those settings are posted by the browser at every subsequent call backs (or until some code removes them). The problem that I see is that the code also sets properties on item which is a FieldValueFacet control. Those assignments will have an effect only for the first post back (initial load of the page). NumberOfAdditionalValues is persisted in the view state, so it may be correct to assign it in InitializeSettings. But NumberOfValues is not saved in the view state. This could potentially explain some of your problems.

Second, ootb, the simple answer to your question: Is there a way to not only display all the selected items? is no, unfortunately. Selected values are always shown without respect to NumberOfValues and NumberOfAdditionalValues, and even when the facet is collapsed. This is as designed. It's very rare that users manually select as many as 180 items in a facet. If another technique such as the ones mentioned in a previous note are not viable for you, one possibility would be to derive a class from FieldValueFacet, to override virtual methods like GetValuesFromResults(), HasMoreValuesThanRequested(), etc. and to manage the paging yourself. This technique is described in another post: https://answers.coveo.com/questions/708/resorting-facet-by-occurrences-after-setting-the-selected-values-programmatically

Here is a code sample showing how to restrict the number of shown selected values with respect to the number of requested values with the More and Fewer links. (This sample also includes the sorting of the selected values of the other question.)

public class CustomFieldValueFacet : FieldValueFacet {
    private int m_RenderedSoFar = 0;

    protected override void RenderValue(HtmlTextWriter p_Writer, FacetValue p_Value, int p_Index) {
        if (m_RenderedSoFar < NumberOfRequestedValues) {
            base.RenderValue(p_Writer, p_Value, p_Index);
            ++m_RenderedSoFar;
        }
    }
    protected override IEnumerable<FacetValue> GetSelectedValues() {
        List<FacetValue> retList = new List<FacetValue>(base.GetSelectedValues());
        retList.Sort(new Comparison<FacetValue>(CompareGroupByValues));
        return retList;
    }
    private int CompareGroupByValues(FacetValue p_Val1, FacetValue p_Val2) {
        return - p_Val1.Count.CompareTo(p_Val2.Count);
    }
}

Regards

Gravatar for mark.angus@sitesystems.ca

Comment by mark, Oct 2, 2014 10:58 AM

Hi, thanks a lot for your suggestion. I also asked that question. It resolved my sorting of occurrences issues but not the checking of all values to match profile settings. I will try to use the HasMoreValuesThanRequested() like you suggested.

Any code snippets you can provide would be appreciated.

Thanks

Gravatar for rquirion@coveo.com

Comment by ronald, Oct 3, 2014 10:44 AM

Hi Mark, I just added a code snippet. Hope this helps. Regards

Gravatar for rquirion@coveo.com

Answer by ronald, Sep 23, 2014 12:58 PM

Instead of assigning higher values to NumberOfValues and/or NumberOfAdditionalValues, try to modify the InjectionDepth. This can be done from the interface editor (Search Interfaces > Features > Facets and Related Results > (the facet) > Performance). This can also be done from your code:

item.InjectionDepth = RefineByFieldInfo.MAX_INJECTION_DEPTH;

Gravatar for mark.angus@sitesystems.ca

Comment by mark, Sep 29, 2014 2:16 PM

Hi, thanks for the suggestion but unfortunately it didn't help.

Gravatar for rquirion@coveo.com

Comment by ronald, Sep 30, 2014 10:15 AM

I may have wrongly understood your question. Could you explain why it is necessary to pre-select as many items in your facet? I'm wondering if another technique would better address your need. For example, using RefineByFieldInfo.AllowedValues to restrict the list of items shown, or programmatically building a query filter expression. Because the facets have been designed to always make visible the facet items that a user manually selects. And the effect of your code is to simulate a user that would manually select a high number of items, which rarely happens in the real life.

Gravatar for mark.angus@sitesystems.ca

Comment by mark, Oct 1, 2014 11:47 AM

Hi, the requirement was to pre-select all the facet values that coincide with the values set in the user profile. Also to ensure that these all profile values are visible in the facet to the user. What was happening is that only a few of the items where selected in the facet until you clicked the expand button then the more link after.

Gravatar for mark.angus@sitesystems.ca

Comment by mark, Oct 1, 2014 11:47 AM

So in order for all the values to be selected and shown correctly, I had to set the number of values to maximum number of values indexed. Note that the facet group-on field is not the same as display field.

I do not have to do this when the facet group-on field value is the same as the display field.

Ask a question