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;
}
}
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();
}
}
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);
}
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();
}
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);
}
Aggregations