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.
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 throughGetFacets()
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