use of org.apache.lucene.facet.FacetField in project lucene-solr by apache.
the class TestCachedOrdinalsReader method testWithThreads.
@Test
public void testWithThreads() throws Exception {
// LUCENE-5303: OrdinalsCache used the ThreadLocal BinaryDV instead of reader.getCoreCacheKey().
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
IndexWriterConfig conf = newIndexWriterConfig(new MockAnalyzer(random()));
IndexWriter writer = new IndexWriter(indexDir, conf);
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
FacetsConfig config = new FacetsConfig();
Document doc = new Document();
doc.add(new FacetField("A", "1"));
writer.addDocument(config.build(taxoWriter, doc));
doc = new Document();
doc.add(new FacetField("A", "2"));
writer.addDocument(config.build(taxoWriter, doc));
final DirectoryReader reader = DirectoryReader.open(writer);
final CachedOrdinalsReader ordsReader = new CachedOrdinalsReader(new DocValuesOrdinalsReader(FacetsConfig.DEFAULT_INDEX_FIELD_NAME));
Thread[] threads = new Thread[3];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread("CachedOrdsThread-" + i) {
@Override
public void run() {
for (LeafReaderContext context : reader.leaves()) {
try {
ordsReader.getReader(context);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
};
}
long ramBytesUsed = 0;
for (Thread t : threads) {
t.start();
t.join();
if (ramBytesUsed == 0) {
ramBytesUsed = ordsReader.ramBytesUsed();
} else {
assertEquals(ramBytesUsed, ordsReader.ramBytesUsed());
}
}
writer.close();
IOUtils.close(taxoWriter, reader, indexDir, taxoDir);
}
use of org.apache.lucene.facet.FacetField in project lucene-solr by apache.
the class TestFacetLabel method testEmptyNullComponents.
@Test
public void testEmptyNullComponents() throws Exception {
// LUCENE-4724: CategoryPath should not allow empty or null components
String[][] components_tests = new String[][] { // empty in the beginning
new String[] { "", "test" }, // empty in the end
new String[] { "test", "" }, // empty in the middle
new String[] { "test", "", "foo" }, // null at the beginning
new String[] { null, "test" }, // null in the end
new String[] { "test", null }, // null in the middle
new String[] { "test", null, "foo" } };
// empty or null components should not be allowed.
for (String[] components : components_tests) {
expectThrows(IllegalArgumentException.class, () -> {
new FacetLabel(components);
});
expectThrows(IllegalArgumentException.class, () -> {
new FacetField("dim", components);
});
expectThrows(IllegalArgumentException.class, () -> {
new AssociationFacetField(new BytesRef(), "dim", components);
});
expectThrows(IllegalArgumentException.class, () -> {
new IntAssociationFacetField(17, "dim", components);
});
expectThrows(IllegalArgumentException.class, () -> {
new FloatAssociationFacetField(17.0f, "dim", components);
});
}
expectThrows(IllegalArgumentException.class, () -> {
new FacetField(null, new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new FacetField("", new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new IntAssociationFacetField(17, null, new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new IntAssociationFacetField(17, "", new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new FloatAssociationFacetField(17.0f, null, new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new FloatAssociationFacetField(17.0f, "", new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new AssociationFacetField(new BytesRef(), null, new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new AssociationFacetField(new BytesRef(), "", new String[] { "abc" });
});
expectThrows(IllegalArgumentException.class, () -> {
new SortedSetDocValuesFacetField(null, "abc");
});
expectThrows(IllegalArgumentException.class, () -> {
new SortedSetDocValuesFacetField("", "abc");
});
expectThrows(IllegalArgumentException.class, () -> {
new SortedSetDocValuesFacetField("dim", null);
});
expectThrows(IllegalArgumentException.class, () -> {
new SortedSetDocValuesFacetField("dim", "");
});
}
use of org.apache.lucene.facet.FacetField in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method indexTwoDocs.
private void indexTwoDocs(TaxonomyWriter taxoWriter, IndexWriter indexWriter, FacetsConfig config, boolean withContent) throws Exception {
for (int i = 0; i < 2; i++) {
Document doc = new Document();
if (withContent) {
doc.add(new StringField("f", "a", Field.Store.NO));
}
if (config != null) {
doc.add(new FacetField("A", Integer.toString(i)));
indexWriter.addDocument(config.build(taxoWriter, doc));
} else {
indexWriter.addDocument(doc);
}
}
indexWriter.commit();
}
use of org.apache.lucene.facet.FacetField in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method testWrongIndexFieldName.
public void testWrongIndexFieldName() throws Exception {
Directory dir = newDirectory();
Directory taxoDir = newDirectory();
// Writes facet ords to a separate directory from the
// main index:
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
FacetsConfig config = new FacetsConfig();
config.setIndexFieldName("a", "$facets2");
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new FacetField("a", "foo1"));
writer.addDocument(config.build(taxoWriter, doc));
// NRT open
IndexSearcher searcher = newSearcher(writer.getReader());
// NRT open
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
FacetsCollector c = new FacetsCollector();
searcher.search(new MatchAllDocsQuery(), c);
// Uses default $facets field:
Facets facets;
if (random().nextBoolean()) {
facets = new FastTaxonomyFacetCounts(taxoReader, config, c);
} else {
OrdinalsReader ordsReader = new DocValuesOrdinalsReader();
if (random().nextBoolean()) {
ordsReader = new CachedOrdinalsReader(ordsReader);
}
facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c);
}
// Ask for top 10 labels for any dims that have counts:
List<FacetResult> results = facets.getAllDims(10);
assertTrue(results.isEmpty());
expectThrows(IllegalArgumentException.class, () -> {
facets.getSpecificValue("a");
});
expectThrows(IllegalArgumentException.class, () -> {
facets.getTopChildren(10, "a");
});
writer.close();
IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);
}
use of org.apache.lucene.facet.FacetField in project lucene-solr by apache.
the class TestTaxonomyFacetCounts method testDetectHierarchicalField.
// Make sure we catch when app didn't declare field as
// hierarchical but it was:
public void testDetectHierarchicalField() throws Exception {
Directory dir = newDirectory();
Directory taxoDir = newDirectory();
TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
FacetsConfig config = new FacetsConfig();
Document doc = new Document();
doc.add(newTextField("field", "text", Field.Store.NO));
doc.add(new FacetField("a", "path", "other"));
expectThrows(IllegalArgumentException.class, () -> {
config.build(taxoWriter, doc);
});
writer.close();
IOUtils.close(taxoWriter, dir, taxoDir);
}
Aggregations