Search in sources :

Example 16 with TaxonomyReader

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

the class TestDirectoryTaxonomyReader method doTestReadRecreatedTaxonomy.

private void doTestReadRecreatedTaxonomy(Random random, boolean closeReader) throws Exception {
    Directory dir = null;
    TaxonomyWriter tw = null;
    TaxonomyReader tr = null;
    // prepare a few categories
    int n = 10;
    FacetLabel[] cp = new FacetLabel[n];
    for (int i = 0; i < n; i++) {
        cp[i] = new FacetLabel("a", Integer.toString(i));
    }
    try {
        dir = newDirectory();
        tw = new DirectoryTaxonomyWriter(dir);
        tw.addCategory(new FacetLabel("a"));
        tw.close();
        tr = new DirectoryTaxonomyReader(dir);
        int baseNumCategories = tr.getSize();
        for (int i = 0; i < n; i++) {
            int k = random.nextInt(n);
            tw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE);
            for (int j = 0; j <= k; j++) {
                tw.addCategory(cp[j]);
            }
            tw.close();
            if (closeReader) {
                tr.close();
                tr = new DirectoryTaxonomyReader(dir);
            } else {
                TaxonomyReader newtr = TaxonomyReader.openIfChanged(tr);
                assertNotNull(newtr);
                tr.close();
                tr = newtr;
            }
            assertEquals("Wrong #categories in taxonomy (i=" + i + ", k=" + k + ")", baseNumCategories + 1 + k, tr.getSize());
        }
    } finally {
        IOUtils.close(tr, tw, dir);
    }
}
Also used : TaxonomyWriter(org.apache.lucene.facet.taxonomy.TaxonomyWriter) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory)

Example 17 with TaxonomyReader

use of org.apache.lucene.facet.taxonomy.TaxonomyReader 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 18 with TaxonomyReader

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

the class MultiCategoryListsFacetsExample 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);
    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 author = new FastTaxonomyFacetCounts("author", taxoReader, config, fc);
    results.add(author.getTopChildren(10, "Author"));
    Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, fc);
    results.add(pubDate.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 19 with TaxonomyReader

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

the class SimpleFacetsExample method facetsOnly.

/** User runs a query and counts facets only without collecting the matching documents.*/
private List<FacetResult> facetsOnly() 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:
    searcher.search(new MatchAllDocsQuery(), 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 20 with TaxonomyReader

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

the class TestPerfTasksLogic method testIndexingWithFacets.

/**
   * Test indexing with facets tasks.
   */
public void testIndexingWithFacets() throws Exception {
    // 1. alg definition (required in every "logic" test)
    String[] algLines = { "# ----- properties ", "content.source=org.apache.lucene.benchmark.byTask.feeds.LineDocSource", "docs.file=" + getReuters20LinesFile(), "content.source.log.step=100", "content.source.forever=false", "directory=RAMDirectory", "doc.stored=false", "merge.factor=3", "doc.tokenized=false", "debug.level=1", "# ----- alg ", "ResetSystemErase", "CreateIndex", "CreateTaxonomyIndex", "{ \"AddDocs\"  AddFacetedDoc > : * ", "CloseIndex", "CloseTaxonomyIndex", "OpenTaxonomyReader" };
    // 2. execute the algorithm  (required in every "logic" test)
    Benchmark benchmark = execBenchmark(algLines);
    PerfRunData runData = benchmark.getRunData();
    assertNull("taxo writer was not properly closed", runData.getTaxonomyWriter());
    TaxonomyReader taxoReader = runData.getTaxonomyReader();
    assertNotNull("taxo reader was not opened", taxoReader);
    assertTrue("nothing was added to the taxnomy (expecting root and at least one addtional category)", taxoReader.getSize() > 1);
    taxoReader.close();
}
Also used : TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader)

Aggregations

TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)33 IndexSearcher (org.apache.lucene.search.IndexSearcher)26 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)25 Directory (org.apache.lucene.store.Directory)20 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)13 DirectoryReader (org.apache.lucene.index.DirectoryReader)13 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 FacetResult (org.apache.lucene.facet.FacetResult)12 Facets (org.apache.lucene.facet.Facets)11 FacetsCollector (org.apache.lucene.facet.FacetsCollector)11 Test (org.junit.Test)11 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)9 DrillSidewaysResult (org.apache.lucene.facet.DrillSideways.DrillSidewaysResult)9 Document (org.apache.lucene.document.Document)8 FastTaxonomyFacetCounts (org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 ArrayList (java.util.ArrayList)7 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)7 FacetLabel (org.apache.lucene.facet.taxonomy.FacetLabel)7 IndexReader (org.apache.lucene.index.IndexReader)7