Facets allow you to include aggregated information about the documents matching your query.
In the beer-sample dataset each beer is assigned a style. This would be a common field to produce a facet on since it has low cardinality and can be helpful in refining your search. To start, each facet requires a name and a size.
stylesFacet := bleve.NewFacetRequest("style", 3)
searchRequest.AddFacet("styles", stylesFacet)
Here we have named the facet styles
and requested the result to include the top 3 categories or buckets in the facet. We have set the field to style
which corresponds to the name we used when mapping the data.
Without providing any other configuration options this will produce what is known as a Terms Facet. That means each term indexed for this field is treated as a unique bucket. For each result document, we include the document in the appropriate bucket, as determined by the configured field. (For the results to make the most sense, this type of facet is normally used for fields indexed as a single term)
In the beer-sample dataset each beer has a numeric abv
or alcohol by volume field. With numeric range facets we can defined buckets to be bounded by numeric ranges. In this example we define 3 buckets which cover all numeric values, corresponding to low, medium, and high alcohol content.
var lowToMidAbv = 3.5
var midToHighAbv = 9.0
abvFacet := bleve.NewFacetRequest("abv", 3)
abvFacet.AddNumericRange("low", nil, &lowToMidAbv)
abvFacet.AddNumericRange("medium", &lowToMidAbv, &midToHighAbv)
abvFacet.AddNumericRange("high", &midToHighAbv, nil)
searchRequest.AddFacet("abv", abvFacet)
In the beer-sample dataset each beer and brewery has a date updated
describing when the document was updated. With date range facets we can defined buckets to be bounded by date ranges. In this example we define 2 buckets which cover all date values, corresponding to those updated this year, and those updated prior to that.
var cutOffDate = time.Now().Add(-365*24*time.Hour)
updatedFacet := bleve.NewFacetRequest("updated", 2)
updatedFacet.AddDateTimeRange("old", time.Unix(0, 0), cutOffDate)
updatedFacet.AddDateTimeRange("new", cutOffDate, time.Unix(999999999, 999999999))
searchRequest.AddFacet("updated", updatedFacet)
For each facet you build, a FacetResult is returned containing the following: