Search in sources :

Example 21 with DirectoryTaxonomyReader

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

the class TestTaxonomyCombined method testReaderBasic.

/**  Basic tests for TaxonomyReader's category <=> ordinal transformations
    (getSize(), getCategory() and getOrdinal()).
    We test that after writing the index, it can be read and all the
    categories and ordinals are there just as we expected them to be.
   */
@Test
public void testReaderBasic() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
    fillTaxonomy(tw);
    tw.close();
    TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
    // test TaxonomyReader.getSize():
    assertEquals(expectedCategories.length, tr.getSize());
    // test round trips of ordinal => category => ordinal
    for (int i = 0; i < tr.getSize(); i++) {
        assertEquals(i, tr.getOrdinal(tr.getPath(i)));
    }
    // test TaxonomyReader.getCategory():
    for (int i = 1; i < tr.getSize(); i++) {
        FacetLabel expectedCategory = new FacetLabel(expectedCategories[i]);
        FacetLabel category = tr.getPath(i);
        if (!expectedCategory.equals(category)) {
            fail("For ordinal " + i + " expected category " + showcat(expectedCategory) + ", but got " + showcat(category));
        }
    }
    //  (also test invalid ordinals:)
    assertNull(tr.getPath(-1));
    assertNull(tr.getPath(tr.getSize()));
    assertNull(tr.getPath(TaxonomyReader.INVALID_ORDINAL));
    // test TaxonomyReader.getOrdinal():
    for (int i = 1; i < expectedCategories.length; i++) {
        int expectedOrdinal = i;
        int ordinal = tr.getOrdinal(new FacetLabel(expectedCategories[i]));
        if (expectedOrdinal != ordinal) {
            fail("For category " + showcat(expectedCategories[i]) + " expected ordinal " + expectedOrdinal + ", but got " + ordinal);
        }
    }
    // (also test invalid categories:)
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(new FacetLabel("non-existant")));
    assertEquals(TaxonomyReader.INVALID_ORDINAL, tr.getOrdinal(new FacetLabel("Author", "Jules Verne")));
    tr.close();
    indexDir.close();
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) SlowRAMDirectory(org.apache.lucene.facet.SlowRAMDirectory) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) Test(org.junit.Test)

Example 22 with DirectoryTaxonomyReader

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

the class TestTaxonomyCombined method testWriterParent1.

/**
   * Tests for TaxonomyWriter's getParent() method. We check it by comparing
   * its results to those we could have gotten by looking at the category
   * string paths using a TaxonomyReader (where the parentage is obvious).
   * Note that after testReaderBasic(), we already know we can trust the
   * ordinal &lt;=&gt; category conversions from TaxonomyReader.
   *
   * The difference between testWriterParent1 and testWriterParent2 is that
   * the former closes the taxonomy writer before reopening it, while the
   * latter does not.
   * 
   * This test code is virtually identical to that of testReaderParent().
   */
@Test
public void testWriterParent1() throws Exception {
    Directory indexDir = newDirectory();
    TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
    fillTaxonomy(tw);
    tw.close();
    tw = new DirectoryTaxonomyWriter(indexDir);
    TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
    checkWriterParent(tr, tw);
    tw.close();
    tr.close();
    indexDir.close();
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) SlowRAMDirectory(org.apache.lucene.facet.SlowRAMDirectory) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) Test(org.junit.Test)

Example 23 with DirectoryTaxonomyReader

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

the class TestTaxonomyFacetAssociations method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    dir = newDirectory();
    taxoDir = newDirectory();
    // preparations - index, taxonomy, content
    TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    // Cannot mix ints & floats in the same indexed field:
    config = new FacetsConfig();
    config.setIndexFieldName("int", "$facets.int");
    config.setMultiValued("int", true);
    config.setIndexFieldName("float", "$facets.float");
    config.setMultiValued("float", true);
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    // index documents, 50% have only 'b' and all have 'a'
    for (int i = 0; i < 110; i++) {
        Document doc = new Document();
        // aggregators to go into an infinite loop
        if (i % 11 != 0) {
            doc.add(new IntAssociationFacetField(2, "int", "a"));
            doc.add(new FloatAssociationFacetField(0.5f, "float", "a"));
            if (i % 2 == 0) {
                // 50
                doc.add(new IntAssociationFacetField(3, "int", "b"));
                doc.add(new FloatAssociationFacetField(0.2f, "float", "b"));
            }
        }
        writer.addDocument(config.build(taxoWriter, doc));
    }
    taxoWriter.close();
    reader = writer.getReader();
    writer.close();
    taxoReader = new DirectoryTaxonomyReader(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) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) BeforeClass(org.junit.BeforeClass)

Example 24 with DirectoryTaxonomyReader

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

the class TestTaxonomyFacetCounts method testSeparateIndexedFields.

public void testSeparateIndexedFields() 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.setIndexFieldName("b", "$b");
    for (int i = atLeast(30); i > 0; --i) {
        Document doc = new Document();
        doc.add(new StringField("f", "v", Field.Store.NO));
        doc.add(new FacetField("a", "1"));
        doc.add(new FacetField("b", "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 facets1 = getTaxonomyFacetCounts(taxoReader, config, sfc);
    Facets facets2 = getTaxonomyFacetCounts(taxoReader, config, sfc, "$b");
    assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue());
    assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.intValue());
    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) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) StringField(org.apache.lucene.document.StringField) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 25 with DirectoryTaxonomyReader

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

the class TestTaxonomyFacetCounts method testMultiValuedHierarchy.

public void testMultiValuedHierarchy() throws Exception {
    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
    FacetsConfig config = new FacetsConfig();
    config.setHierarchical("a", true);
    config.setMultiValued("a", true);
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(newTextField("field", "text", Field.Store.NO));
    doc.add(new FacetField("a", "path", "x"));
    doc.add(new FacetField("a", "path", "y"));
    writer.addDocument(config.build(taxoWriter, doc));
    // NRT open
    IndexSearcher searcher = newSearcher(writer.getReader());
    // NRT open
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, searcher, taxoReader, config);
    expectThrows(IllegalArgumentException.class, () -> {
        facets.getSpecificValue("a");
    });
    FacetResult result = facets.getTopChildren(10, "a");
    assertEquals(1, result.labelValues.length);
    assertEquals(1, result.labelValues[0].value.intValue());
    writer.close();
    IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, dir, taxoDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) IndexSearcher(org.apache.lucene.search.IndexSearcher) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetField(org.apache.lucene.facet.FacetField) FacetResult(org.apache.lucene.facet.FacetResult) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Aggregations

DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)73 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)52 Directory (org.apache.lucene.store.Directory)50 IndexSearcher (org.apache.lucene.search.IndexSearcher)44 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)37 Facets (org.apache.lucene.facet.Facets)35 Document (org.apache.lucene.document.Document)31 FacetsCollector (org.apache.lucene.facet.FacetsCollector)30 FacetResult (org.apache.lucene.facet.FacetResult)29 DirectoryReader (org.apache.lucene.index.DirectoryReader)28 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)25 FacetsConfig (org.apache.lucene.facet.FacetsConfig)24 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)24 FacetField (org.apache.lucene.facet.FacetField)22 Test (org.junit.Test)22 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)19 SlowRAMDirectory (org.apache.lucene.facet.SlowRAMDirectory)14 IndexWriter (org.apache.lucene.index.IndexWriter)11 ArrayList (java.util.ArrayList)10 DrillSidewaysResult (org.apache.lucene.facet.DrillSideways.DrillSidewaysResult)9