Search in sources :

Example 1 with SearcherAndTaxonomy

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

the class TestSearcherTaxonomyManager method testDirectory.

public void testDirectory() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    final IndexWriter w = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    final DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
    // first empty commit
    w.commit();
    tw.commit();
    final SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(indexDir, taxoDir, null);
    final FacetsConfig config = new FacetsConfig();
    config.setMultiValued("field", true);
    final AtomicBoolean stop = new AtomicBoolean();
    // How many unique facets to index before stopping:
    final int ordLimit = TEST_NIGHTLY ? 100000 : 6000;
    Thread indexer = new IndexerThread(w, config, tw, mgr, ordLimit, stop);
    indexer.start();
    try {
        while (!stop.get()) {
            SearcherAndTaxonomy pair = mgr.acquire();
            try {
                //System.out.println("search maxOrd=" + pair.taxonomyReader.getSize());
                FacetsCollector sfc = new FacetsCollector();
                pair.searcher.search(new MatchAllDocsQuery(), sfc);
                Facets facets = getTaxonomyFacetCounts(pair.taxonomyReader, config, sfc);
                FacetResult result = facets.getTopChildren(10, "field");
                if (pair.searcher.getIndexReader().numDocs() > 0) {
                    //System.out.println(pair.taxonomyReader.getSize());
                    assertTrue(result.childCount > 0);
                    assertTrue(result.labelValues.length > 0);
                }
            //if (VERBOSE) {
            //System.out.println("TEST: facets=" + FacetTestUtils.toString(results.get(0)));
            //}
            } finally {
                mgr.release(pair);
            }
        }
    } finally {
        indexer.join();
    }
    if (VERBOSE) {
        System.out.println("TEST: now stop");
    }
    w.close();
    IOUtils.close(mgr, tw, taxoDir, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) FacetResult(org.apache.lucene.facet.FacetResult) SearcherAndTaxonomy(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager.SearcherAndTaxonomy) Directory(org.apache.lucene.store.Directory)

Example 2 with SearcherAndTaxonomy

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

the class TestSearcherTaxonomyManager method testReplaceTaxonomyDirectory.

public void testReplaceTaxonomyDirectory() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    IndexWriter w = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
    w.commit();
    tw.commit();
    Directory taxoDir2 = newDirectory();
    DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(taxoDir2);
    tw2.addCategory(new FacetLabel("a", "b"));
    tw2.close();
    SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(indexDir, taxoDir, null);
    SearcherAndTaxonomy pair = mgr.acquire();
    try {
        assertEquals(1, pair.taxonomyReader.getSize());
    } finally {
        mgr.release(pair);
    }
    w.addDocument(new Document());
    tw.replaceTaxonomy(taxoDir2);
    taxoDir2.close();
    w.commit();
    tw.commit();
    mgr.maybeRefresh();
    pair = mgr.acquire();
    try {
        assertEquals(3, pair.taxonomyReader.getSize());
    } finally {
        mgr.release(pair);
    }
    w.close();
    IOUtils.close(mgr, tw, taxoDir, indexDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) Document(org.apache.lucene.document.Document) SearcherAndTaxonomy(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager.SearcherAndTaxonomy) Directory(org.apache.lucene.store.Directory)

Example 3 with SearcherAndTaxonomy

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

the class TestSearcherTaxonomyManager method testNRT.

public void testNRT() throws Exception {
    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    // Don't allow tiny maxBufferedDocs; it can make this
    // test too slow:
    iwc.setMaxBufferedDocs(Math.max(500, iwc.getMaxBufferedDocs()));
    // MockRandom/AlcololicMergePolicy are too slow:
    TieredMergePolicy tmp = new TieredMergePolicy();
    tmp.setFloorSegmentMB(.001);
    iwc.setMergePolicy(tmp);
    final IndexWriter w = new IndexWriter(dir, iwc);
    final DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
    final FacetsConfig config = new FacetsConfig();
    config.setMultiValued("field", true);
    final AtomicBoolean stop = new AtomicBoolean();
    // How many unique facets to index before stopping:
    final int ordLimit = TEST_NIGHTLY ? 100000 : 6000;
    Thread indexer = new IndexerThread(w, config, tw, null, ordLimit, stop);
    final SearcherTaxonomyManager mgr = new SearcherTaxonomyManager(w, true, null, tw);
    Thread reopener = new Thread() {

        @Override
        public void run() {
            while (!stop.get()) {
                try {
                    // Sleep for up to 20 msec:
                    Thread.sleep(random().nextInt(20));
                    if (VERBOSE) {
                        System.out.println("TEST: reopen");
                    }
                    mgr.maybeRefresh();
                    if (VERBOSE) {
                        System.out.println("TEST: reopen done");
                    }
                } catch (Exception ioe) {
                    throw new RuntimeException(ioe);
                }
            }
        }
    };
    reopener.setName("reopener");
    reopener.start();
    indexer.setName("indexer");
    indexer.start();
    try {
        while (!stop.get()) {
            SearcherAndTaxonomy pair = mgr.acquire();
            try {
                //System.out.println("search maxOrd=" + pair.taxonomyReader.getSize());
                FacetsCollector sfc = new FacetsCollector();
                pair.searcher.search(new MatchAllDocsQuery(), sfc);
                Facets facets = getTaxonomyFacetCounts(pair.taxonomyReader, config, sfc);
                FacetResult result = facets.getTopChildren(10, "field");
                if (pair.searcher.getIndexReader().numDocs() > 0) {
                    //System.out.println(pair.taxonomyReader.getSize());
                    assertTrue(result.childCount > 0);
                    assertTrue(result.labelValues.length > 0);
                }
            //if (VERBOSE) {
            //System.out.println("TEST: facets=" + FacetTestUtils.toString(results.get(0)));
            //}
            } finally {
                mgr.release(pair);
            }
        }
    } finally {
        indexer.join();
        reopener.join();
    }
    if (VERBOSE) {
        System.out.println("TEST: now stop");
    }
    w.close();
    IOUtils.close(mgr, tw, taxoDir, dir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IOException(java.io.IOException) FacetsCollector(org.apache.lucene.facet.FacetsCollector) TieredMergePolicy(org.apache.lucene.index.TieredMergePolicy) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) FacetResult(org.apache.lucene.facet.FacetResult) SearcherAndTaxonomy(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager.SearcherAndTaxonomy) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 SearcherAndTaxonomy (org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager.SearcherAndTaxonomy)3 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 Directory (org.apache.lucene.store.Directory)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FacetResult (org.apache.lucene.facet.FacetResult)2 Facets (org.apache.lucene.facet.Facets)2 FacetsCollector (org.apache.lucene.facet.FacetsCollector)2 FacetsConfig (org.apache.lucene.facet.FacetsConfig)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 IOException (java.io.IOException)1 Document (org.apache.lucene.document.Document)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 TieredMergePolicy (org.apache.lucene.index.TieredMergePolicy)1