func (fb *TermsFacetBuilder) Result() *search.FacetResult { rv := search.FacetResult{ Field: fb.field, Total: fb.total, Missing: fb.missing, } rv.Terms = make([]*search.TermFacet, 0, len(fb.termsCount)) for term, count := range fb.termsCount { tf := &search.TermFacet{ Term: term, Count: count, } rv.Terms = append(rv.Terms, tf) } sort.Sort(rv.Terms) // we now have the list of the top N facets trimTopN := fb.size if trimTopN > len(rv.Terms) { trimTopN = len(rv.Terms) } rv.Terms = rv.Terms[:trimTopN] notOther := 0 for _, tf := range rv.Terms { notOther += tf.Count } rv.Other = fb.total - notOther return &rv }