Пример #1
0
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
}
Пример #2
0
func (fb *TermsFacetBuilder) Result() search.FacetResult {
	rv := search.FacetResult{
		Field:   fb.field,
		Total:   fb.total,
		Missing: fb.missing,
	}

	// FIXME better implementation needed here this is quick and dirty
	topN := list.New()

	// walk entries and find top N
OUTER:
	for term, count := range fb.termsCount {
		tf := &search.TermFacet{
			Term:  term,
			Count: count,
		}

		for e := topN.Front(); e != nil; e = e.Next() {
			curr := e.Value.(*search.TermFacet)
			if tf.Count < curr.Count {

				topN.InsertBefore(tf, e)
				// if we just made the list too long
				if topN.Len() > fb.size {
					// remove the head
					topN.Remove(topN.Front())
				}
				continue OUTER
			}
		}
		// if we got to the end, we still have to add it
		topN.PushBack(tf)
		if topN.Len() > fb.size {
			// remove the head
			topN.Remove(topN.Front())
		}

	}

	// we now have the list of the top N facets
	rv.Terms = make([]*search.TermFacet, topN.Len())
	i := 0
	notOther := 0
	for e := topN.Back(); e != nil; e = e.Prev() {
		rv.Terms[i] = e.Value.(*search.TermFacet)
		i++
		notOther += e.Value.(*search.TermFacet).Count
	}
	rv.Other = fb.total - notOther

	return rv
}