Search in sources :

Example 6 with FacetResult

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

the class ConcurrentSortedSetDocValuesFacetCounts method getAllDims.

@Override
public List<FacetResult> getAllDims(int topN) throws IOException {
    List<FacetResult> results = new ArrayList<>();
    for (Map.Entry<String, OrdRange> ent : state.getPrefixToOrdRange().entrySet()) {
        FacetResult fr = getDim(ent.getKey(), ent.getValue(), topN);
        if (fr != null) {
            results.add(fr);
        }
    }
    // Sort by highest count:
    Collections.sort(results, new Comparator<FacetResult>() {

        @Override
        public int compare(FacetResult a, FacetResult b) {
            if (a.value.intValue() > b.value.intValue()) {
                return -1;
            } else if (b.value.intValue() > a.value.intValue()) {
                return 1;
            } else {
                return a.dim.compareTo(b.dim);
            }
        }
    });
    return results;
}
Also used : OrdRange(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState.OrdRange) ArrayList(java.util.ArrayList) FacetResult(org.apache.lucene.facet.FacetResult) Map(java.util.Map)

Example 7 with FacetResult

use of org.apache.lucene.facet.FacetResult 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)

Example 8 with FacetResult

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

the class IntTaxonomyFacets 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;
    }
    TopOrdAndIntQueue q = new TopOrdAndIntQueue(Math.min(taxoReader.getSize(), topN));
    int bottomValue = 0;
    int ord = children[dimOrd];
    int totValue = 0;
    int childCount = 0;
    TopOrdAndIntQueue.OrdAndValue reuse = null;
    while (ord != TaxonomyReader.INVALID_ORDINAL) {
        if (values[ord] > 0) {
            totValue += values[ord];
            childCount++;
            if (values[ord] > bottomValue) {
                if (reuse == null) {
                    reuse = new TopOrdAndIntQueue.OrdAndValue();
                }
                reuse.ord = ord;
                reuse.value = values[ord];
                reuse = q.insertWithOverflow(reuse);
                if (q.size() == topN) {
                    bottomValue = q.top().value;
                }
            }
        }
        ord = siblings[ord];
    }
    if (totValue == 0) {
        return null;
    }
    if (dimConfig.multiValued) {
        if (dimConfig.requireDimCount) {
            totValue = values[dimOrd];
        } else {
            // Our sum'd value is not correct, in general:
            totValue = -1;
        }
    } else {
    // Our sum'd dim value is accurate, so we keep it
    }
    LabelAndValue[] labelValues = new LabelAndValue[q.size()];
    for (int i = labelValues.length - 1; i >= 0; i--) {
        TopOrdAndIntQueue.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, totValue, labelValues, childCount);
}
Also used : TopOrdAndIntQueue(org.apache.lucene.facet.TopOrdAndIntQueue) FacetResult(org.apache.lucene.facet.FacetResult) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DimConfig(org.apache.lucene.facet.FacetsConfig.DimConfig)

Example 9 with FacetResult

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

the class SortedSetDocValuesFacetCounts method getDim.

private final FacetResult getDim(String dim, OrdRange ordRange, int topN) throws IOException {
    TopOrdAndIntQueue q = null;
    int bottomCount = 0;
    int dimCount = 0;
    int childCount = 0;
    TopOrdAndIntQueue.OrdAndValue reuse = null;
    //System.out.println("getDim : " + ordRange.start + " - " + ordRange.end);
    for (int ord = ordRange.start; ord <= ordRange.end; ord++) {
        //System.out.println("  ord=" + ord + " count=" + counts[ord]);
        if (counts[ord] > 0) {
            dimCount += counts[ord];
            childCount++;
            if (counts[ord] > bottomCount) {
                if (reuse == null) {
                    reuse = new TopOrdAndIntQueue.OrdAndValue();
                }
                reuse.ord = ord;
                reuse.value = counts[ord];
                if (q == null) {
                    // Lazy init, so we don't create this for the
                    // sparse case unnecessarily
                    q = new TopOrdAndIntQueue(topN);
                }
                reuse = q.insertWithOverflow(reuse);
                if (q.size() == topN) {
                    bottomCount = q.top().value;
                }
            }
        }
    }
    if (q == null) {
        return null;
    }
    LabelAndValue[] labelValues = new LabelAndValue[q.size()];
    for (int i = labelValues.length - 1; i >= 0; i--) {
        TopOrdAndIntQueue.OrdAndValue ordAndValue = q.pop();
        final BytesRef term = dv.lookupOrd(ordAndValue.ord);
        String[] parts = FacetsConfig.stringToPath(term.utf8ToString());
        labelValues[i] = new LabelAndValue(parts[1], ordAndValue.value);
    }
    return new FacetResult(dim, new String[0], dimCount, labelValues, childCount);
}
Also used : TopOrdAndIntQueue(org.apache.lucene.facet.TopOrdAndIntQueue) FacetResult(org.apache.lucene.facet.FacetResult) LabelAndValue(org.apache.lucene.facet.LabelAndValue) BytesRef(org.apache.lucene.util.BytesRef)

Example 10 with FacetResult

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

the class TestOrdinalMappingLeafReader method verifyResults.

private void verifyResults(Directory indexDir, Directory taxoDir) throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);
    FacetsCollector collector = new FacetsCollector();
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, collector);
    // tag facets
    Facets tagFacets = new FastTaxonomyFacetCounts("$tags", taxoReader, facetConfig, collector);
    FacetResult result = tagFacets.getTopChildren(10, "tag");
    for (LabelAndValue lv : result.labelValues) {
        if (VERBOSE) {
            System.out.println(lv);
        }
        assertEquals(NUM_DOCS, lv.value.intValue());
    }
    // id facets
    Facets idFacets = new FastTaxonomyFacetCounts(taxoReader, facetConfig, collector);
    FacetResult idResult = idFacets.getTopChildren(10, "id");
    assertEquals(NUM_DOCS, idResult.childCount);
    // each "id" appears twice
    assertEquals(NUM_DOCS * 2, idResult.value);
    BinaryDocValues bdv = MultiDocValues.getBinaryValues(indexReader, "bdv");
    BinaryDocValues cbdv = MultiDocValues.getBinaryValues(indexReader, "cbdv");
    for (int i = 0; i < indexReader.maxDoc(); i++) {
        assertEquals(i, bdv.nextDoc());
        assertEquals(i, cbdv.nextDoc());
        assertEquals(Integer.parseInt(cbdv.binaryValue().utf8ToString()), Integer.parseInt(bdv.binaryValue().utf8ToString()) * 2);
    }
    IOUtils.close(indexReader, taxoReader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LabelAndValue(org.apache.lucene.facet.LabelAndValue) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Aggregations

FacetResult (org.apache.lucene.facet.FacetResult)68 Facets (org.apache.lucene.facet.Facets)47 FacetsCollector (org.apache.lucene.facet.FacetsCollector)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)36 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)29 LabelAndValue (org.apache.lucene.facet.LabelAndValue)28 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)23 DirectoryReader (org.apache.lucene.index.DirectoryReader)22 ArrayList (java.util.ArrayList)21 Directory (org.apache.lucene.store.Directory)21 FacetsConfig (org.apache.lucene.facet.FacetsConfig)19 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)19 Document (org.apache.lucene.document.Document)18 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)14 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)13 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)13 SortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState)13 IOException (java.io.IOException)12 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)12 FacetField (org.apache.lucene.facet.FacetField)11