10

I'm working on a Lucene-based search page on Sitecore 8.1. I'd like to be able to display all facets and all facet values, even when there are 0 aggregate results for it.

Currently, what I get back from a Lucene query is

{
  "Facets": {
    "Categories": [
        {
        "Name": "facetOne",
        "Values": [
            {
            "Name": "F1Value1",
            "AggregateCount": 1,
            "Aggregate": 1
            }
        ]
        },
        {
        "Name": "facetTwo",
        "Values": [
            {
            "Name": "F2Value1",
            "AggregateCount": 2,
            "Aggregate": 2
            }
        ]
        }
    ]
    }
}

However, I'd like to get back facets with 0 values as well, such as facetThree and F2Value2 in this example:

{
  "Facets": {
    "Categories": [
        {
        "Name": "facetOne",
        "Values": [
            {
            "Name": "F1Value1",
            "AggregateCount": 1,
            "Aggregate": 1
            }
        ]
        },
        {
        "Name": "facetTwo",
        "Values": [
            {
            "Name": "F2Value1",
            "AggregateCount": 2,
            "Aggregate": 2
            },
            {
            "Name": "F2Value2",
            "AggregateCount": 0,
            "Aggregate": 0
            }
        ]
        },
        {
        "Name": "facetThree",
        "Values": [
            {
            "Name": "F3Value1",
            "AggregateCount": 0,
            "Aggregate": 0
            }
        ]
        }
    ]
    }
}

Is it possible to do this within the ContentSearch API? If so, how?

edit: here's (a simplified version of) the back-end code, in case it helps

using (IProviderSearchContext context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
{
    var query = context.GetQueryable<TQueryable>();
    query = query
                .FacetOn(f => f.FacetOne)
                .FacetOn(f => f.FacetTwo)
                .FacetOn(f => f.FacetThree);

    var results = query.GetResults();
}

Update: I've learned that there is an override to .FacetOn that takes a "minimumResultCount" argument. That does not appear to be doing anything, however. I'm not sure if that functionality is broken, or if I'm doing something wrong.

Update 2: I submitted a support ticket to Sitecore. They have confirmed there is a bug. Essentially, the Lucene provider doesn't process the minimum result count property of the query. Sitecore has opened a bug for it and is investigating. They confirmed that it is NOT an issue for Solr, and is only limited to the Lucene implementation.

  • I've come up with a hacky workaround for now. I can call query.GetFacets() before applying any sort of .Where clause. That lets met get all possible facets and all possible values. For now, I'll have the front-end code load all facets through GetFacets() and "zero-out" anything that doesn't come back in the results. It feels very hacky, and I'd much rather have a clean solution to do this. – Ben Lipson Oct 10 '16 at 15:16
  • 1
    As far as I know, the two-query approach is the only way to do this. – techphoria414 Oct 10 '16 at 20:49
6

You need to create on /sitecore/system/Settings/Buckets/Facets a new facets add field name (must be lowercase).

You have the field Minimum Number ( Minimum Number of Items - The minimum number of items that the facet must appear in before it is shown in the search results [shared, standard value].

Set the value 0 and it will appear in your search results.

enter image description here

| improve this answer | |
  • 1
    I'm actually building a search page for the front-end, not in Sitecore. Based off your comment, I checked and found an overload for .FacetOn which has a minimum number, but it doesn't seem to do anything: .FacetOn(f => f.FacetOne, 0). I wonder if there's a difference between how Minimum Number works in Sitecore vs how it works in the ContentSearch API – Ben Lipson Oct 10 '16 at 15:34
  • As far I remember it works fine for me but it was 1 year ago, you can try this solution. Doesn't matter if is backend or frontend. – Vlad Iobagiu Oct 10 '16 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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