Search in sources :

Example 46 with FacetResult

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

the class TaxonomyFacets method getAllDims.

@Override
public List<FacetResult> getAllDims(int topN) throws IOException {
    int ord = children[TaxonomyReader.ROOT_ORDINAL];
    List<FacetResult> results = new ArrayList<>();
    while (ord != TaxonomyReader.INVALID_ORDINAL) {
        String dim = taxoReader.getPath(ord).components[0];
        FacetsConfig.DimConfig dimConfig = config.getDimConfig(dim);
        if (dimConfig.indexFieldName.equals(indexFieldName)) {
            FacetResult result = getTopChildren(topN, dim);
            if (result != null) {
                results.add(result);
            }
        }
        ord = siblings[ord];
    }
    // Sort by highest value, tie break by dim:
    Collections.sort(results, BY_VALUE_THEN_DIM);
    return results;
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) ArrayList(java.util.ArrayList) FacetResult(org.apache.lucene.facet.FacetResult) DimConfig(org.apache.lucene.facet.FacetsConfig.DimConfig)

Example 47 with FacetResult

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

the class ConcurrentSortedSetDocValuesFacetCounts 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.get(ord) > 0) {
            dimCount += counts.get(ord);
            childCount++;
            if (counts.get(ord) > bottomCount) {
                if (reuse == null) {
                    reuse = new TopOrdAndIntQueue.OrdAndValue();
                }
                reuse.ord = ord;
                reuse.value = counts.get(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 48 with FacetResult

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

the class TestSimpleSortedSetFacetsExample method testDrillDown.

@Test
public void testDrillDown() throws Exception {
    FacetResult result = new SimpleSortedSetFacetsExample().runDrillDown();
    assertEquals("dim=Author path=[] value=2 childCount=2\n  Bob (1)\n  Lisa (1)\n", result.toString());
}
Also used : FacetResult(org.apache.lucene.facet.FacetResult) Test(org.junit.Test)

Example 49 with FacetResult

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

the class SimpleFacetsExample method facetsWithSearch.

/** User runs a query and counts facets. */
private List<FacetResult> facetsWithSearch() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    FacetsCollector fc = new FacetsCollector();
    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
    // Retrieve results
    List<FacetResult> results = new ArrayList<>();
    // Count both "Publish Date" and "Author" dimensions
    Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
    results.add(facets.getTopChildren(10, "Author"));
    results.add(facets.getTopChildren(10, "Publish Date"));
    indexReader.close();
    taxoReader.close();
    return results;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FastTaxonomyFacetCounts(org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) ArrayList(java.util.ArrayList) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 50 with FacetResult

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

the class SimpleSortedSetFacetsExample method search.

/** User runs a query and counts facets. */
private List<FacetResult> search() throws IOException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader);
    // Aggregatses the facet counts
    FacetsCollector fc = new FacetsCollector();
    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
    // Retrieve results
    Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
    List<FacetResult> results = new ArrayList<>();
    results.add(facets.getTopChildren(10, "Author"));
    results.add(facets.getTopChildren(10, "Publish Year"));
    indexReader.close();
    return results;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) ArrayList(java.util.ArrayList) SortedSetDocValuesFacetCounts(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) 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