Search in sources :

Example 6 with BitDocIdSet

use of org.apache.lucene.util.BitDocIdSet in project lucene-solr by apache.

the class WithinPrefixTreeQuery method getDocIdSet.

@Override
protected DocIdSet getDocIdSet(LeafReaderContext context) throws IOException {
    return new VisitorTemplate(context) {

        private FixedBitSet inside;

        private FixedBitSet outside;

        @Override
        protected void start() {
            inside = new FixedBitSet(maxDoc);
            outside = new FixedBitSet(maxDoc);
        }

        @Override
        protected DocIdSet finish() {
            inside.andNot(outside);
            return new BitDocIdSet(inside);
        }

        @Override
        protected CellIterator findSubCellsToVisit(Cell cell) {
            //use buffered query shape instead of orig.  Works with null too.
            return cell.getNextLevelCells(bufferedQueryShape);
        }

        @Override
        protected boolean visitPrefix(Cell cell) throws IOException {
            //cell.relate is based on the bufferedQueryShape; we need to examine what
            // the relation is against the queryShape
            SpatialRelation visitRelation = cell.getShape().relate(queryShape);
            if (cell.getLevel() == detailLevel) {
                collectDocs(visitRelation.intersects() ? inside : outside);
                return false;
            } else if (visitRelation == SpatialRelation.WITHIN) {
                collectDocs(inside);
                return false;
            } else if (visitRelation == SpatialRelation.DISJOINT) {
                collectDocs(outside);
                return false;
            }
            return true;
        }

        @Override
        protected void visitLeaf(Cell cell) throws IOException {
            if (allCellsIntersectQuery(cell))
                collectDocs(inside);
            else
                collectDocs(outside);
        }

        /** Returns true if the provided cell, and all its sub-cells down to
       * detailLevel all intersect the queryShape.
       */
        private boolean allCellsIntersectQuery(Cell cell) {
            SpatialRelation relate = cell.getShape().relate(queryShape);
            if (cell.getLevel() == detailLevel)
                return relate.intersects();
            if (relate == SpatialRelation.WITHIN)
                return true;
            if (relate == SpatialRelation.DISJOINT)
                return false;
            // Note: Generating all these cells just to determine intersection is not ideal.
            // The real solution is LUCENE-4869.
            CellIterator subCells = cell.getNextLevelCells(null);
            while (subCells.hasNext()) {
                Cell subCell = subCells.next();
                if (//recursion
                !allCellsIntersectQuery(subCell))
                    return false;
            }
            return true;
        }

        @Override
        protected void visitScanned(Cell cell) throws IOException {
            //collects as we want, even if not a leaf
            visitLeaf(cell);
        //        if (cell.isLeaf()) {
        //          visitLeaf(cell);
        //        } else {
        //          visitPrefix(cell);
        //        }
        }
    }.getDocIdSet();
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) FixedBitSet(org.apache.lucene.util.FixedBitSet) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) CellIterator(org.apache.lucene.spatial.prefix.tree.CellIterator) IOException(java.io.IOException) Cell(org.apache.lucene.spatial.prefix.tree.Cell) SpatialRelation(org.locationtech.spatial4j.shape.SpatialRelation)

Example 7 with BitDocIdSet

use of org.apache.lucene.util.BitDocIdSet in project lucene-solr by apache.

the class QueryBitSetProducer method getBitSet.

@Override
public BitSet getBitSet(LeafReaderContext context) throws IOException {
    final LeafReader reader = context.reader();
    final IndexReader.CacheHelper cacheHelper = reader.getCoreCacheHelper();
    DocIdSet docIdSet = null;
    if (cacheHelper != null) {
        docIdSet = cache.get(cacheHelper.getKey());
    }
    if (docIdSet == null) {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight weight = searcher.createNormalizedWeight(query, false);
        final Scorer s = weight.scorer(context);
        if (s == null) {
            docIdSet = DocIdSet.EMPTY;
        } else {
            docIdSet = new BitDocIdSet(BitSet.of(s.iterator(), context.reader().maxDoc()));
        }
        if (cacheHelper != null) {
            cache.put(cacheHelper.getKey(), docIdSet);
        }
    }
    return docIdSet == DocIdSet.EMPTY ? null : ((BitDocIdSet) docIdSet).bits();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) LeafReader(org.apache.lucene.index.LeafReader) IndexReader(org.apache.lucene.index.IndexReader) DocIdSet(org.apache.lucene.search.DocIdSet) BitDocIdSet(org.apache.lucene.util.BitDocIdSet) Scorer(org.apache.lucene.search.Scorer) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) Weight(org.apache.lucene.search.Weight)

Example 8 with BitDocIdSet

use of org.apache.lucene.util.BitDocIdSet 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 9 with BitDocIdSet

use of org.apache.lucene.util.BitDocIdSet 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 10 with BitDocIdSet

use of org.apache.lucene.util.BitDocIdSet in project lucene-solr by apache.

the class TestSort method randSet.

public DocIdSet randSet(int sz) {
    FixedBitSet obs = new FixedBitSet(sz);
    int n = r.nextInt(sz);
    for (int i = 0; i < n; i++) {
        obs.set(r.nextInt(sz));
    }
    return new BitDocIdSet(obs);
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Aggregations

BitDocIdSet (org.apache.lucene.util.BitDocIdSet)11 FixedBitSet (org.apache.lucene.util.FixedBitSet)10 DocIdSet (org.apache.lucene.search.DocIdSet)4 IOException (java.io.IOException)3 LeafReader (org.apache.lucene.index.LeafReader)3 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Bits (org.apache.lucene.util.Bits)2 LinkedList (java.util.LinkedList)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Scorer (org.apache.lucene.search.Scorer)1 Weight (org.apache.lucene.search.Weight)1 Cell (org.apache.lucene.spatial.prefix.tree.Cell)1 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)1 SpatialRelation (org.locationtech.spatial4j.shape.SpatialRelation)1