Search in sources :

Example 41 with FacetsConfig

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

Example 42 with FacetsConfig

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

the class TestTaxonomyFacetCounts2 method indexDocsWithFacetsNoTerms.

private static void indexDocsWithFacetsNoTerms(IndexWriter indexWriter, TaxonomyWriter taxoWriter, Map<String, Integer> expectedCounts) throws IOException {
    Random random = random();
    int numDocs = atLeast(random, 2);
    FacetsConfig config = getConfig();
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        addFacets(doc, config, false);
        indexWriter.addDocument(config.build(taxoWriter, doc));
    }
    // flush a segment
    indexWriter.commit();
}
Also used : Random(java.util.Random) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Document(org.apache.lucene.document.Document)

Example 43 with FacetsConfig

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

the class TestTaxonomyFacetAssociations method testNoHierarchy.

public void testNoHierarchy() throws Exception {
    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();
    TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    FacetsConfig config = new FacetsConfig();
    config.setHierarchical("a", true);
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new IntAssociationFacetField(14, "a", "x"));
    expectThrows(IllegalArgumentException.class, () -> {
        writer.addDocument(config.build(taxoWriter, doc));
    });
    writer.close();
    IOUtils.close(taxoWriter, dir, taxoDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 44 with FacetsConfig

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

the class TestTaxonomyFacetAssociations method testMixedTypesInSameIndexField.

public void testMixedTypesInSameIndexField() throws Exception {
    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();
    TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    FacetsConfig config = new FacetsConfig();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new IntAssociationFacetField(14, "a", "x"));
    doc.add(new FloatAssociationFacetField(55.0f, "b", "y"));
    expectThrows(IllegalArgumentException.class, () -> {
        writer.addDocument(config.build(taxoWriter, doc));
    });
    writer.close();
    IOUtils.close(taxoWriter, dir, taxoDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 45 with FacetsConfig

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

the class TestTaxonomyFacetCounts method testChildCount.

public void testChildCount() throws Exception {
    // LUCENE-4885: FacetResult.numValidDescendants was not set properly by FacetsAccumulator
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    FacetsConfig config = new FacetsConfig();
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new FacetField("a", Integer.toString(i)));
        iw.addDocument(config.build(taxoWriter, doc));
    }
    DirectoryReader r = DirectoryReader.open(iw);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config);
    assertEquals(10, facets.getTopChildren(2, "a").childCount);
    iw.close();
    IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Aggregations

FacetsConfig (org.apache.lucene.facet.FacetsConfig)53 Document (org.apache.lucene.document.Document)44 Directory (org.apache.lucene.store.Directory)44 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)38 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)35 FacetField (org.apache.lucene.facet.FacetField)29 Facets (org.apache.lucene.facet.Facets)29 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)24 IndexSearcher (org.apache.lucene.search.IndexSearcher)23 FacetsCollector (org.apache.lucene.facet.FacetsCollector)22 FacetResult (org.apache.lucene.facet.FacetResult)18 IndexWriter (org.apache.lucene.index.IndexWriter)18 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)17 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)16 DirectoryReader (org.apache.lucene.index.DirectoryReader)12 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)8 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)8 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)8 LabelAndValue (org.apache.lucene.facet.LabelAndValue)7 Term (org.apache.lucene.index.Term)6