use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader in project lucene-solr by apache.
the class TestTaxonomyCombined method testSeparateReaderAndWriter.
/** Test that if separate reader and writer objects are opened, new
categories written into the writer are available to a reader only
after a commit().
Note that this test obviously doesn't cover all the different
concurrency scenarios, all different methods, and so on. We may
want to write more tests of this sort.
This test simulates what would happen when there are two separate
processes, one doing indexing, and the other searching, and each opens
its own object (with obviously no connection between the objects) using
the same disk files. Note, though, that this test does not test what
happens when the two processes do their actual work at exactly the same
time.
It also doesn't test multi-threading.
*/
@Test
public void testSeparateReaderAndWriter() throws Exception {
Directory indexDir = newDirectory();
TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
tw.commit();
TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
// the empty taxonomy has size 1 (the root)
assertEquals(1, tr.getSize());
tw.addCategory(new FacetLabel("Author"));
// still root only...
assertEquals(1, tr.getSize());
// this is not enough, because tw.commit() hasn't been done yet
assertNull(TaxonomyReader.openIfChanged(tr));
// still root only...
assertEquals(1, tr.getSize());
tw.commit();
// still root only...
assertEquals(1, tr.getSize());
TaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(tr);
assertNotNull(newTaxoReader);
tr.close();
tr = newTaxoReader;
int author = 1;
try {
assertEquals(TaxonomyReader.ROOT_ORDINAL, tr.getParallelTaxonomyArrays().parents()[author]);
// ok
} catch (ArrayIndexOutOfBoundsException e) {
fail("After category addition, commit() and refresh(), getParent for " + author + " should NOT throw exception");
}
// finally, see there are two categories
assertEquals(2, tr.getSize());
// now, add another category, and verify that after commit and refresh
// the parent of this category is correct (this requires the reader
// to correctly update its prefetched parent vector), and that the
// old information also wasn't ruined:
tw.addCategory(new FacetLabel("Author", "Richard Dawkins"));
int dawkins = 2;
tw.commit();
newTaxoReader = TaxonomyReader.openIfChanged(tr);
assertNotNull(newTaxoReader);
tr.close();
tr = newTaxoReader;
int[] parents = tr.getParallelTaxonomyArrays().parents();
assertEquals(author, parents[dawkins]);
assertEquals(TaxonomyReader.ROOT_ORDINAL, parents[author]);
assertEquals(TaxonomyReader.INVALID_ORDINAL, parents[TaxonomyReader.ROOT_ORDINAL]);
assertEquals(3, tr.getSize());
tw.close();
tr.close();
indexDir.close();
}
use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method testGetFacetResultsTwice.
public void testGetFacetResultsTwice() throws Exception {
// LUCENE-4893: counts were multiplied as many times as getFacetResults was called.
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
FacetsConfig config = new FacetsConfig();
Document doc = new Document();
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);
Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config);
List<FacetResult> res1 = facets.getAllDims(10);
List<FacetResult> res2 = facets.getAllDims(10);
assertEquals("calling getFacetResults twice should return the .equals()=true result", res1, res2);
iw.close();
IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method testCountRoot.
public void testCountRoot() throws Exception {
// LUCENE-4882: FacetsAccumulator threw NPE if a FacetRequest was defined on CP.EMPTY
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 = atLeast(30); i > 0; --i) {
Document doc = new Document();
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);
Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config);
for (FacetResult result : facets.getAllDims(10)) {
assertEquals(r.numDocs(), result.value.intValue());
}
iw.close();
IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method testManyFacetsInOneDocument.
// LUCENE-4583: make sure if we require > 32 KB for one
// document, we don't hit exc when using Facet42DocValuesFormat
public void testManyFacetsInOneDocument() throws Exception {
assumeTrue("default Codec doesn't support huge BinaryDocValues", TestUtil.fieldSupportsHugeBinaryDocValues(FacetsConfig.DEFAULT_INDEX_FIELD_NAME));
Directory dir = newDirectory();
Directory taxoDir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
FacetsConfig config = new FacetsConfig();
config.setMultiValued("dim", true);
int numLabels = TestUtil.nextInt(random(), 40000, 100000);
Document doc = new Document();
doc.add(newTextField("field", "text", Field.Store.NO));
for (int i = 0; i < numLabels; i++) {
doc.add(new FacetField("dim", "" + i));
}
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);
FacetResult result = facets.getTopChildren(Integer.MAX_VALUE, "dim");
assertEquals(numLabels, result.labelValues.length);
Set<String> allLabels = new HashSet<>();
for (LabelAndValue labelValue : result.labelValues) {
allLabels.add(labelValue.label);
assertEquals(1, labelValue.value.intValue());
}
assertEquals(numLabels, allLabels.size());
writer.close();
IOUtils.close(searcher.getIndexReader(), taxoWriter, taxoReader, dir, taxoDir);
}
use of org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader in project lucene-solr by apache.
the class TestDrillDownQuery method beforeClassDrillDownQueryTest.
@BeforeClass
public static void beforeClassDrillDownQueryTest() throws Exception {
dir = newDirectory();
Random r = random();
RandomIndexWriter writer = new RandomIndexWriter(r, dir, newIndexWriterConfig(new MockAnalyzer(r, MockTokenizer.KEYWORD, false)));
taxoDir = newDirectory();
TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
config = new FacetsConfig();
// Randomize the per-dim config:
config.setHierarchical("a", random().nextBoolean());
config.setMultiValued("a", random().nextBoolean());
if (random().nextBoolean()) {
config.setIndexFieldName("a", "$a");
}
config.setRequireDimCount("a", true);
config.setHierarchical("b", random().nextBoolean());
config.setMultiValued("b", random().nextBoolean());
if (random().nextBoolean()) {
config.setIndexFieldName("b", "$b");
}
config.setRequireDimCount("b", true);
for (int i = 0; i < 100; i++) {
Document doc = new Document();
if (i % 2 == 0) {
// 50
doc.add(new TextField("content", "foo", Field.Store.NO));
}
if (i % 3 == 0) {
// 33
doc.add(new TextField("content", "bar", Field.Store.NO));
}
if (i % 4 == 0) {
// 25
if (r.nextBoolean()) {
doc.add(new FacetField("a", "1"));
} else {
doc.add(new FacetField("a", "2"));
}
}
if (i % 5 == 0) {
// 20
doc.add(new FacetField("b", "1"));
}
writer.addDocument(config.build(taxoWriter, doc));
}
taxoWriter.close();
reader = writer.getReader();
writer.close();
taxo = new DirectoryTaxonomyReader(taxoDir);
}
Aggregations