Search in sources :

Example 1 with FacetField

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);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetField(org.apache.lucene.facet.FacetField) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.Test)

Example 2 with FacetField

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", "");
    });
}
Also used : FacetField(org.apache.lucene.facet.FacetField) SortedSetDocValuesFacetField(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField) SortedSetDocValuesFacetField(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 3 with FacetField

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();
}
Also used : StringField(org.apache.lucene.document.StringField) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document)

Example 4 with FacetField

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);
}
Also used : 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) 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) FacetResult(org.apache.lucene.facet.FacetResult) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 5 with FacetField

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);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) FacetsConfig(org.apache.lucene.facet.FacetsConfig) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

FacetField (org.apache.lucene.facet.FacetField)42 Document (org.apache.lucene.document.Document)36 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)31 FacetsConfig (org.apache.lucene.facet.FacetsConfig)29 Directory (org.apache.lucene.store.Directory)28 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)26 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)22 Facets (org.apache.lucene.facet.Facets)19 IndexWriter (org.apache.lucene.index.IndexWriter)16 FacetsCollector (org.apache.lucene.facet.FacetsCollector)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)13 FacetResult (org.apache.lucene.facet.FacetResult)11 DirectoryReader (org.apache.lucene.index.DirectoryReader)11 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)10 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)8 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)8 HashMap (java.util.HashMap)4 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)4 StringField (org.apache.lucene.document.StringField)4