Search in sources :

Example 1 with TopOrdAndFloatQueue

use of org.apache.lucene.facet.TopOrdAndFloatQueue in project lucene-solr by apache.

the class FloatTaxonomyFacets method getTopChildren.

@Override
public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException {
    if (topN <= 0) {
        throw new IllegalArgumentException("topN must be > 0 (got: " + topN + ")");
    }
    DimConfig dimConfig = verifyDim(dim);
    FacetLabel cp = new FacetLabel(dim, path);
    int dimOrd = taxoReader.getOrdinal(cp);
    if (dimOrd == -1) {
        return null;
    }
    TopOrdAndFloatQueue q = new TopOrdAndFloatQueue(Math.min(taxoReader.getSize(), topN));
    float bottomValue = 0;
    int ord = children[dimOrd];
    float sumValues = 0;
    int childCount = 0;
    TopOrdAndFloatQueue.OrdAndValue reuse = null;
    while (ord != TaxonomyReader.INVALID_ORDINAL) {
        if (values[ord] > 0) {
            sumValues += values[ord];
            childCount++;
            if (values[ord] > bottomValue) {
                if (reuse == null) {
                    reuse = new TopOrdAndFloatQueue.OrdAndValue();
                }
                reuse.ord = ord;
                reuse.value = values[ord];
                reuse = q.insertWithOverflow(reuse);
                if (q.size() == topN) {
                    bottomValue = q.top().value;
                }
            }
        }
        ord = siblings[ord];
    }
    if (sumValues == 0) {
        return null;
    }
    if (dimConfig.multiValued) {
        if (dimConfig.requireDimCount) {
            sumValues = values[dimOrd];
        } else {
            // Our sum'd count is not correct, in general:
            sumValues = -1;
        }
    } else {
    // Our sum'd dim count is accurate, so we keep it
    }
    LabelAndValue[] labelValues = new LabelAndValue[q.size()];
    for (int i = labelValues.length - 1; i >= 0; i--) {
        TopOrdAndFloatQueue.OrdAndValue ordAndValue = q.pop();
        FacetLabel child = taxoReader.getPath(ordAndValue.ord);
        labelValues[i] = new LabelAndValue(child.components[cp.length], ordAndValue.value);
    }
    return new FacetResult(dim, path, sumValues, labelValues, childCount);
}
Also used : TopOrdAndFloatQueue(org.apache.lucene.facet.TopOrdAndFloatQueue) FacetResult(org.apache.lucene.facet.FacetResult) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DimConfig(org.apache.lucene.facet.FacetsConfig.DimConfig)

Aggregations

FacetResult (org.apache.lucene.facet.FacetResult)1 DimConfig (org.apache.lucene.facet.FacetsConfig.DimConfig)1 LabelAndValue (org.apache.lucene.facet.LabelAndValue)1 TopOrdAndFloatQueue (org.apache.lucene.facet.TopOrdAndFloatQueue)1