Search in sources :

Example 41 with FacetsCollector

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

the class TestTaxonomyFacetCounts2 method testNoParents.

@Test
public void testNoParents() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);
    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);
    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);
    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_C, CP_C);
    assertEquals(allExpectedCounts.get(CP_C), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_C + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(NUM_CHILDREN_CP_D, CP_D);
    assertEquals(allExpectedCounts.get(CP_C), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_D + "/" + labelValue.label), labelValue.value);
    }
    IOUtils.close(indexReader, taxoReader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector) Test(org.junit.Test)

Example 42 with FacetsCollector

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

the class TestTaxonomyFacetAssociations method testIntAndFloatAssocation.

/** Make sure we can test both int and float assocs in one
   *  index, as long as we send each to a different field. */
public void testIntAndFloatAssocation() throws Exception {
    FacetsCollector fc = new FacetsCollector();
    IndexSearcher searcher = newSearcher(reader);
    searcher.search(new MatchAllDocsQuery(), fc);
    Facets facets = new TaxonomyFacetSumFloatAssociations("$facets.float", taxoReader, config, fc);
    assertEquals("Wrong count for category 'a'!", 50f, facets.getSpecificValue("float", "a").floatValue(), 0.00001);
    assertEquals("Wrong count for category 'b'!", 10f, facets.getSpecificValue("float", "b").floatValue(), 0.00001);
    facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
    assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
    assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 43 with FacetsCollector

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

the class TestTaxonomyFacetAssociations method testIntSumAssociation.

public void testIntSumAssociation() throws Exception {
    FacetsCollector fc = new FacetsCollector();
    IndexSearcher searcher = newSearcher(reader);
    searcher.search(new MatchAllDocsQuery(), fc);
    Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
    assertEquals("dim=int path=[] value=-1 childCount=2\n  a (200)\n  b (150)\n", facets.getTopChildren(10, "int").toString());
    assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
    assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 44 with FacetsCollector

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

Example 45 with FacetsCollector

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

the class TestTaxonomyFacetSumValueSource method testRollupValues.

public void testRollupValues() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    FacetsConfig config = new FacetsConfig();
    config.setHierarchical("a", true);
    for (int i = 0; i < 4; i++) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField("price", (i + 1)));
        doc.add(new FacetField("a", Integer.toString(i % 2), "1"));
        iw.addDocument(config.build(taxoWriter, doc));
    }
    DirectoryReader r = DirectoryReader.open(iw);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    FacetsCollector sfc = new FacetsCollector();
    newSearcher(r).search(new MatchAllDocsQuery(), sfc);
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, DoubleValuesSource.fromLongField("price"));
    assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());
    iw.close();
    IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Aggregations

FacetsCollector (org.apache.lucene.facet.FacetsCollector)67 Facets (org.apache.lucene.facet.Facets)60 FacetResult (org.apache.lucene.facet.FacetResult)42 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)41 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)30 Directory (org.apache.lucene.store.Directory)27 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)25 Document (org.apache.lucene.document.Document)24 DirectoryReader (org.apache.lucene.index.DirectoryReader)24 FacetsConfig (org.apache.lucene.facet.FacetsConfig)22 LabelAndValue (org.apache.lucene.facet.LabelAndValue)22 ArrayList (java.util.ArrayList)18 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)17 FacetField (org.apache.lucene.facet.FacetField)14 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)14 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)14 IOException (java.io.IOException)13 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)13 SortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState)13