Search in sources :

Example 1 with FacetLabel

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

the class DirectoryTaxonomyReader method getPath.

@Override
public FacetLabel getPath(int ordinal) throws IOException {
    ensureOpen();
    // the cache.
    if (ordinal < 0 || ordinal >= indexReader.maxDoc()) {
        return null;
    }
    // TODO: can we use an int-based hash impl, such as IntToObjectMap,
    // wrapped as LRU?
    Integer catIDInteger = Integer.valueOf(ordinal);
    synchronized (categoryCache) {
        FacetLabel res = categoryCache.get(catIDInteger);
        if (res != null) {
            return res;
        }
    }
    Document doc = indexReader.document(ordinal);
    FacetLabel ret = new FacetLabel(FacetsConfig.stringToPath(doc.get(Consts.FULL)));
    synchronized (categoryCache) {
        categoryCache.put(catIDInteger, ret);
    }
    return ret;
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) Document(org.apache.lucene.document.Document)

Example 2 with FacetLabel

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

the class DirectoryTaxonomyReader method toString.

/** Returns ordinal -&gt; label mapping, up to the provided
   *  max ordinal or number of ordinals, whichever is
   *  smaller. */
public String toString(int max) {
    ensureOpen();
    StringBuilder sb = new StringBuilder();
    int upperl = Math.min(max, indexReader.maxDoc());
    for (int i = 0; i < upperl; i++) {
        try {
            FacetLabel category = this.getPath(i);
            if (category == null) {
                sb.append(i + ": NULL!! \n");
                continue;
            }
            if (category.length == 0) {
                sb.append(i + ": EMPTY STRING!! \n");
                continue;
            }
            sb.append(i + ": " + category.toString() + "\n");
        } catch (IOException e) {
            if (logger.isLoggable(Level.FINEST)) {
                logger.log(Level.FINEST, e.getMessage(), e);
            }
        }
    }
    return sb.toString();
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) IOException(java.io.IOException)

Example 3 with FacetLabel

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

the class DirectoryTaxonomyWriter method addTaxonomy.

/**
   * Takes the categories from the given taxonomy directory, and adds the
   * missing ones to this taxonomy. Additionally, it fills the given
   * {@link OrdinalMap} with a mapping from the original ordinal to the new
   * ordinal.
   */
public void addTaxonomy(Directory taxoDir, OrdinalMap map) throws IOException {
    ensureOpen();
    DirectoryReader r = DirectoryReader.open(taxoDir);
    try {
        final int size = r.numDocs();
        final OrdinalMap ordinalMap = map;
        ordinalMap.setSize(size);
        int base = 0;
        PostingsEnum docs = null;
        for (final LeafReaderContext ctx : r.leaves()) {
            final LeafReader ar = ctx.reader();
            final Terms terms = ar.terms(Consts.FULL);
            // TODO: share per-segment TermsEnum here!
            TermsEnum te = terms.iterator();
            while (te.next() != null) {
                FacetLabel cp = new FacetLabel(FacetsConfig.stringToPath(te.term().utf8ToString()));
                final int ordinal = addCategory(cp);
                docs = te.postings(docs, PostingsEnum.NONE);
                ordinalMap.addMapping(docs.nextDoc() + base, ordinal);
            }
            // no deletions, so we're ok
            base += ar.maxDoc();
        }
        ordinalMap.addDone();
    } finally {
        r.close();
    }
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) Terms(org.apache.lucene.index.Terms) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) PostingsEnum(org.apache.lucene.index.PostingsEnum) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 4 with FacetLabel

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

the class FacetsConfig method processSSDVFacetFields.

private void processSSDVFacetFields(Map<String, List<SortedSetDocValuesFacetField>> byField, Document doc) throws IOException {
    //System.out.println("process SSDV: " + byField);
    for (Map.Entry<String, List<SortedSetDocValuesFacetField>> ent : byField.entrySet()) {
        String indexFieldName = ent.getKey();
        for (SortedSetDocValuesFacetField facetField : ent.getValue()) {
            FacetLabel cp = new FacetLabel(facetField.dim, facetField.label);
            String fullPath = pathToString(cp.components, cp.length);
            //System.out.println("add " + fullPath);
            // For facet counts:
            doc.add(new SortedSetDocValuesField(indexFieldName, new BytesRef(fullPath)));
            // For drill-down:
            doc.add(new StringField(indexFieldName, fullPath, Field.Store.NO));
            doc.add(new StringField(indexFieldName, facetField.dim, Field.Store.NO));
        }
    }
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) StringField(org.apache.lucene.document.StringField) ArrayList(java.util.ArrayList) List(java.util.List) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) SortedSetDocValuesFacetField(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with FacetLabel

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

the class TestDirectoryTaxonomyWriter method testReplaceTaxoWithLargeTaxonomy.

@Test
public void testReplaceTaxoWithLargeTaxonomy() throws Exception {
    Directory srcTaxoDir = newDirectory(), targetTaxoDir = newDirectory();
    // build source, large, taxonomy
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(srcTaxoDir);
    int ord = taxoWriter.addCategory(new FacetLabel("A", "1", "1", "1", "1", "1", "1"));
    taxoWriter.close();
    taxoWriter = new DirectoryTaxonomyWriter(targetTaxoDir);
    int ordinal = taxoWriter.addCategory(new FacetLabel("B", "1"));
    // call getParent to initialize taxoArrays
    assertEquals(1, taxoWriter.getParent(ordinal));
    taxoWriter.commit();
    taxoWriter.replaceTaxonomy(srcTaxoDir);
    assertEquals(ord - 1, taxoWriter.getParent(ord));
    taxoWriter.close();
    srcTaxoDir.close();
    targetTaxoDir.close();
}
Also used : FacetLabel(org.apache.lucene.facet.taxonomy.FacetLabel) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

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