Search in sources :

Example 11 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class BitDocSet method getTopFilter.

@Override
public Filter getTopFilter() {
    return new Filter() {

        final FixedBitSet bs = bits;

        @Override
        public DocIdSet getDocIdSet(final LeafReaderContext context, final Bits acceptDocs) {
            LeafReader reader = context.reader();
            // all Solr DocSets that are used as filters only include live docs
            final Bits acceptDocs2 = acceptDocs == null ? null : (reader.getLiveDocs() == acceptDocs ? null : acceptDocs);
            if (context.isTopLevel) {
                return BitsFilteredDocIdSet.wrap(new BitDocIdSet(bs), acceptDocs);
            }
            final int base = context.docBase;
            // one past the max doc in this segment.
            final int max = base + reader.maxDoc();
            return BitsFilteredDocIdSet.wrap(new DocIdSet() {

                @Override
                public DocIdSetIterator iterator() {
                    return new DocIdSetIterator() {

                        int pos = base - 1;

                        int adjustedDoc = -1;

                        @Override
                        public int docID() {
                            return adjustedDoc;
                        }

                        @Override
                        public int nextDoc() {
                            int next = pos + 1;
                            if (next >= max) {
                                return adjustedDoc = NO_MORE_DOCS;
                            } else {
                                pos = bs.nextSetBit(next);
                                return adjustedDoc = pos < max ? pos - base : NO_MORE_DOCS;
                            }
                        }

                        @Override
                        public int advance(int target) {
                            if (target == NO_MORE_DOCS)
                                return adjustedDoc = NO_MORE_DOCS;
                            int adjusted = target + base;
                            if (adjusted >= max) {
                                return adjustedDoc = NO_MORE_DOCS;
                            } else {
                                pos = bs.nextSetBit(adjusted);
                                return adjustedDoc = pos < max ? pos - base : NO_MORE_DOCS;
                            }
                        }

                        @Override
                        public long cost() {
                            // we don't want to actually compute cardinality, but
                            // if it's already been computed, we use it (pro-rated for the segment)
                            int maxDoc = max - base;
                            if (size != -1) {
                                return (long) (size * ((FixedBitSet.bits2words(maxDoc) << 6) / (float) bs.length()));
                            } else {
                                return maxDoc;
                            }
                        }
                    };
                }

                @Override
                public long ramBytesUsed() {
                    return bs.ramBytesUsed();
                }

                @Override
                public Bits bits() {
                    return new Bits() {

                        @Override
                        public boolean get(int index) {
                            return bs.get(index + base);
                        }

                        @Override
                        public int length() {
                            return max - base;
                        }
                    };
                }
            }, acceptDocs2);
        }

        @Override
        public String toString(String field) {
            return "BitSetDocTopFilter";
        }

        @Override
        public boolean equals(Object other) {
            return sameClassAs(other) && Objects.equals(bs, getClass().cast(other).bs);
        }

        @Override
        public int hashCode() {
            return classHash() * 31 + bs.hashCode();
        }
    };
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) LeafReader(org.apache.lucene.index.LeafReader) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 12 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class DocSetBase method getTopFilter.

@Override
public Filter getTopFilter() {
    return new Filter() {

        final FixedBitSet bs = getBits();

        @Override
        public DocIdSet getDocIdSet(final LeafReaderContext context, Bits acceptDocs) {
            LeafReader reader = context.reader();
            // all Solr DocSets that are used as filters only include live docs
            final Bits acceptDocs2 = acceptDocs == null ? null : (reader.getLiveDocs() == acceptDocs ? null : acceptDocs);
            if (context.isTopLevel) {
                return BitsFilteredDocIdSet.wrap(new BitDocIdSet(bs), acceptDocs);
            }
            final int base = context.docBase;
            final int maxDoc = reader.maxDoc();
            // one past the max doc in this segment.
            final int max = base + maxDoc;
            return BitsFilteredDocIdSet.wrap(new DocIdSet() {

                @Override
                public DocIdSetIterator iterator() {
                    return new DocIdSetIterator() {

                        int pos = base - 1;

                        int adjustedDoc = -1;

                        @Override
                        public int docID() {
                            return adjustedDoc;
                        }

                        @Override
                        public int nextDoc() {
                            // TODO: this is buggy if getBits() returns a bitset that does not have a capacity of maxDoc
                            pos = bs.nextSetBit(pos + 1);
                            return adjustedDoc = pos < max ? pos - base : NO_MORE_DOCS;
                        }

                        @Override
                        public int advance(int target) {
                            if (target == NO_MORE_DOCS)
                                return adjustedDoc = NO_MORE_DOCS;
                            pos = bs.nextSetBit(target + base);
                            return adjustedDoc = pos < max ? pos - base : NO_MORE_DOCS;
                        }

                        @Override
                        public long cost() {
                            return bs.length();
                        }
                    };
                }

                @Override
                public long ramBytesUsed() {
                    return bs.ramBytesUsed();
                }

                @Override
                public Bits bits() {
                    // sparse filters should not use random access
                    return null;
                }
            }, acceptDocs2);
        }

        @Override
        public String toString(String field) {
            return "DocSetTopFilter";
        }

        @Override
        public boolean equals(Object other) {
            return sameClassAs(other) && Objects.equals(bs, getClass().cast(other).bs);
        }

        @Override
        public int hashCode() {
            return classHash() ^ bs.hashCode();
        }
    };
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) LeafReader(org.apache.lucene.index.LeafReader) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 13 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class DocSetUtil method createDocSet.

public static DocSet createDocSet(SolrIndexSearcher searcher, Term term) throws IOException {
    // raw reader to avoid extra wrapping overhead
    DirectoryReader reader = searcher.getRawReader();
    int maxDoc = searcher.getIndexReader().maxDoc();
    int smallSetSize = smallSetSize(maxDoc);
    String field = term.field();
    BytesRef termVal = term.bytes();
    int maxCount = 0;
    int firstReader = -1;
    List<LeafReaderContext> leaves = reader.leaves();
    // use array for slightly higher scanning cost, but fewer memory allocations
    PostingsEnum[] postList = new PostingsEnum[leaves.size()];
    for (LeafReaderContext ctx : leaves) {
        assert leaves.get(ctx.ord) == ctx;
        LeafReader r = ctx.reader();
        Fields f = r.fields();
        Terms t = f.terms(field);
        // field is missing
        if (t == null)
            continue;
        TermsEnum te = t.iterator();
        if (te.seekExact(termVal)) {
            maxCount += te.docFreq();
            postList[ctx.ord] = te.postings(null, PostingsEnum.NONE);
            if (firstReader < 0)
                firstReader = ctx.ord;
        }
    }
    DocSet answer = null;
    if (maxCount == 0) {
        answer = DocSet.EMPTY;
    } else if (maxCount <= smallSetSize) {
        answer = createSmallSet(leaves, postList, maxCount, firstReader);
    } else {
        answer = createBigSet(leaves, postList, maxDoc, firstReader);
    }
    return DocSetUtil.getDocSet(answer, searcher);
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Terms(org.apache.lucene.index.Terms) TermsEnum(org.apache.lucene.index.TermsEnum) Fields(org.apache.lucene.index.Fields) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

Example 14 with LeafReader

use of org.apache.lucene.index.LeafReader 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 15 with LeafReader

use of org.apache.lucene.index.LeafReader in project lucene-solr by apache.

the class LatLonPointDistanceComparator method getLeafComparator.

@Override
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
    LeafReader reader = context.reader();
    FieldInfo info = reader.getFieldInfos().fieldInfo(field);
    if (info != null) {
        LatLonDocValuesField.checkCompatible(info);
    }
    currentDocs = DocValues.getSortedNumeric(reader, field);
    valuesDocID = -1;
    return this;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) FieldInfo(org.apache.lucene.index.FieldInfo)

Aggregations

LeafReader (org.apache.lucene.index.LeafReader)187 BytesRef (org.apache.lucene.util.BytesRef)69 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)68 Document (org.apache.lucene.document.Document)65 Directory (org.apache.lucene.store.Directory)62 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)55 DirectoryReader (org.apache.lucene.index.DirectoryReader)49 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)42 Test (org.junit.Test)41 IndexWriter (org.apache.lucene.index.IndexWriter)35 Terms (org.apache.lucene.index.Terms)34 NumericDocValues (org.apache.lucene.index.NumericDocValues)33 TermsEnum (org.apache.lucene.index.TermsEnum)32 IndexReader (org.apache.lucene.index.IndexReader)26 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)24 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)24 Term (org.apache.lucene.index.Term)24 SortedDocValues (org.apache.lucene.index.SortedDocValues)22 Bits (org.apache.lucene.util.Bits)21 IOException (java.io.IOException)20