Search in sources :

Example 31 with FacetLabel

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

the class CompactLabelToOrdinal method init.

private void init() {
    labelRepository = new CharBlockArray();
    CategoryPathUtils.serialize(new FacetLabel(), labelRepository);
    int c = this.capacity;
    for (int i = 0; i < this.hashArrays.length; i++) {
        this.hashArrays[i] = new HashArray(c);
        c /= 2;
    }
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel)

Example 32 with FacetLabel

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

the class TestAddTaxonomy method validate.

private void validate(Directory dest, Directory src, OrdinalMap ordMap) throws Exception {
    DirectoryTaxonomyReader destTR = new DirectoryTaxonomyReader(dest);
    try {
        final int destSize = destTR.getSize();
        DirectoryTaxonomyReader srcTR = new DirectoryTaxonomyReader(src);
        try {
            int[] map = ordMap.getMap();
            // validate taxo sizes
            int srcSize = srcTR.getSize();
            assertTrue("destination taxonomy expected to be larger than source; dest=" + destSize + " src=" + srcSize, destSize >= srcSize);
            // ordinals are as expected.
            for (int j = 1; j < srcSize; j++) {
                FacetLabel cp = srcTR.getPath(j);
                int destOrdinal = destTR.getOrdinal(cp);
                assertTrue(cp + " not found in destination", destOrdinal > 0);
                assertEquals(destOrdinal, map[j]);
            }
        } finally {
            srcTR.close();
        }
    } finally {
        destTR.close();
    }
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel)

Example 33 with FacetLabel

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

the class TestAddTaxonomy method testSimple.

public void testSimple() throws Exception {
    Directory dest = newDirectory();
    DirectoryTaxonomyWriter tw1 = new DirectoryTaxonomyWriter(dest);
    tw1.addCategory(new FacetLabel("Author", "Mark Twain"));
    tw1.addCategory(new FacetLabel("Animals", "Dog"));
    tw1.addCategory(new FacetLabel("Author", "Rob Pike"));
    Directory src = newDirectory();
    DirectoryTaxonomyWriter tw2 = new DirectoryTaxonomyWriter(src);
    tw2.addCategory(new FacetLabel("Author", "Rob Pike"));
    tw2.addCategory(new FacetLabel("Aardvarks", "Bob"));
    tw2.close();
    OrdinalMap map = randomOrdinalMap();
    tw1.addTaxonomy(src, map);
    tw1.close();
    validate(dest, src, map);
    IOUtils.close(dest, src);
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) DiskOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap) MemoryOrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap) OrdinalMap(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap) Directory(org.apache.lucene.store.Directory)

Example 34 with FacetLabel

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

the class TestDirectoryTaxonomyReader method testAlreadyClosed.

@Test
public void testAlreadyClosed() throws Exception {
    Directory dir = newDirectory();
    DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
    ltw.addCategory(new FacetLabel("a"));
    ltw.close();
    DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
    ltr.close();
    expectThrows(AlreadyClosedException.class, () -> {
        ltr.getSize();
    });
    dir.close();
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 35 with FacetLabel

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

the class TestConcurrentFacetedIndexing method testConcurrency.

public void testConcurrency() throws Exception {
    final AtomicInteger numDocs = new AtomicInteger(atLeast(10000));
    final Directory indexDir = newDirectory();
    final Directory taxoDir = newDirectory();
    final ConcurrentHashMap<String, String> values = new ConcurrentHashMap<>();
    final IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(null));
    final DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE, newTaxoWriterCache(numDocs.get()));
    final Thread[] indexThreads = new Thread[atLeast(4)];
    final FacetsConfig config = new FacetsConfig();
    for (int i = 0; i < 10; i++) {
        config.setHierarchical("l1." + i, true);
        config.setMultiValued("l1." + i, true);
    }
    for (int i = 0; i < indexThreads.length; i++) {
        indexThreads[i] = new Thread() {

            @Override
            public void run() {
                Random random = random();
                while (numDocs.decrementAndGet() > 0) {
                    try {
                        Document doc = new Document();
                        // 1-3
                        int numCats = random.nextInt(3) + 1;
                        while (numCats-- > 0) {
                            FacetField ff = newCategory();
                            doc.add(ff);
                            FacetLabel label = new FacetLabel(ff.dim, ff.path);
                            // add all prefixes to values
                            int level = label.length;
                            while (level > 0) {
                                String s = FacetsConfig.pathToString(label.components, level);
                                values.put(s, s);
                                --level;
                            }
                        }
                        iw.addDocument(config.build(tw, doc));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
    }
    for (Thread t : indexThreads) t.start();
    for (Thread t : indexThreads) t.join();
    DirectoryTaxonomyReader tr = new DirectoryTaxonomyReader(tw);
    // +1 for root category
    if (values.size() + 1 != tr.getSize()) {
        for (String value : values.keySet()) {
            FacetLabel label = new FacetLabel(FacetsConfig.stringToPath(value));
            if (tr.getOrdinal(label) == -1) {
                System.out.println("FAIL: path=" + label + " not recognized");
            }
        }
        fail("mismatch number of categories");
    }
    int[] parents = tr.getParallelTaxonomyArrays().parents();
    for (String cat : values.keySet()) {
        FacetLabel cp = new FacetLabel(FacetsConfig.stringToPath(cat));
        assertTrue("category not found " + cp, tr.getOrdinal(cp) > 0);
        int level = cp.length;
        // for root, parent is always virtual ROOT (ord=0)
        int parentOrd = 0;
        FacetLabel path = null;
        for (int i = 0; i < level; i++) {
            path = cp.subpath(i + 1);
            int ord = tr.getOrdinal(path);
            assertEquals("invalid parent for cp=" + path, parentOrd, parents[ord]);
            // next level should have this parent
            parentOrd = ord;
        }
    }
    iw.close();
    IOUtils.close(tw, tr, taxoDir, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) FacetField(org.apache.lucene.facet.FacetField) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexWriter(org.apache.lucene.index.IndexWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Directory(org.apache.lucene.store.Directory)

Aggregations

FacetLabel (org.apache.lucene.facet.taxonomy.FacetLabel)43 Directory (org.apache.lucene.store.Directory)32 Test (org.junit.Test)25 RAMDirectory (org.apache.lucene.store.RAMDirectory)13 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)7 MemoryOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.MemoryOrdinalMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IOException (java.io.IOException)5 Random (java.util.Random)5 DiskOrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.DiskOrdinalMap)5 OrdinalMap (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter.OrdinalMap)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Document (org.apache.lucene.document.Document)3