Search in sources :

Example 6 with DocIdSet

use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.

the class IntervalFacets method getCountString.

private void getCountString() throws IOException {
    Filter filter = docs.getTopFilter();
    List<LeafReaderContext> leaves = searcher.getTopReaderContext().leaves();
    for (int subIndex = 0; subIndex < leaves.size(); subIndex++) {
        LeafReaderContext leaf = leaves.get(subIndex);
        // solr docsets already exclude any deleted docs
        DocIdSet dis = filter.getDocIdSet(leaf, null);
        if (dis == null) {
            continue;
        }
        DocIdSetIterator disi = dis.iterator();
        if (disi != null) {
            if (schemaField.multiValued()) {
                SortedSetDocValues sub = leaf.reader().getSortedSetDocValues(schemaField.getName());
                if (sub == null) {
                    continue;
                }
                final SortedDocValues singleton = DocValues.unwrapSingleton(sub);
                if (singleton != null) {
                    // some codecs may optimize SORTED_SET storage for single-valued fields
                    accumIntervalsSingle(singleton, disi, dis.bits());
                } else {
                    accumIntervalsMulti(sub, disi, dis.bits());
                }
            } else {
                SortedDocValues sub = leaf.reader().getSortedDocValues(schemaField.getName());
                if (sub == null) {
                    continue;
                }
                accumIntervalsSingle(sub, disi, dis.bits());
            }
        }
    }
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) Filter(org.apache.solr.search.Filter) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSet(org.apache.lucene.search.DocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 7 with DocIdSet

use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.

the class TestDocIdSetBuilder method testSparse.

public void testSparse() throws IOException {
    final int maxDoc = 1000000 + random().nextInt(1000000);
    DocIdSetBuilder builder = new DocIdSetBuilder(maxDoc);
    final int numIterators = 1 + random().nextInt(10);
    final FixedBitSet ref = new FixedBitSet(maxDoc);
    for (int i = 0; i < numIterators; ++i) {
        final int baseInc = 200000 + random().nextInt(10000);
        RoaringDocIdSet.Builder b = new RoaringDocIdSet.Builder(maxDoc);
        for (int doc = random().nextInt(100); doc < maxDoc; doc += baseInc + random().nextInt(10000)) {
            b.add(doc);
            ref.set(doc);
        }
        builder.add(b.build().iterator());
    }
    DocIdSet result = builder.build();
    assertTrue(result instanceof IntArrayDocIdSet);
    assertEquals(new BitDocIdSet(ref), result);
}
Also used : DocIdSet(org.apache.lucene.search.DocIdSet)

Example 8 with DocIdSet

use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.

the class TestDocIdSetBuilder method testRandom.

public void testRandom() throws IOException {
    final int maxDoc = TestUtil.nextInt(random(), 1, 10000000);
    for (int i = 1; i < maxDoc / 2; i <<= 1) {
        final int numDocs = TestUtil.nextInt(random(), 1, i);
        final FixedBitSet docs = new FixedBitSet(maxDoc);
        int c = 0;
        while (c < numDocs) {
            final int d = random().nextInt(maxDoc);
            if (docs.get(d) == false) {
                docs.set(d);
                c += 1;
            }
        }
        final int[] array = new int[numDocs + random().nextInt(100)];
        DocIdSetIterator it = new BitSetIterator(docs, 0L);
        int j = 0;
        for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {
            array[j++] = doc;
        }
        assertEquals(numDocs, j);
        // add some duplicates
        while (j < array.length) {
            array[j++] = array[random().nextInt(numDocs)];
        }
        // shuffle
        for (j = array.length - 1; j >= 1; --j) {
            final int k = random().nextInt(j);
            int tmp = array[j];
            array[j] = array[k];
            array[k] = tmp;
        }
        // add docs out of order
        DocIdSetBuilder builder = new DocIdSetBuilder(maxDoc);
        for (j = 0; j < array.length; ) {
            final int l = TestUtil.nextInt(random(), 1, array.length - j);
            DocIdSetBuilder.BulkAdder adder = null;
            for (int k = 0, budget = 0; k < l; ++k) {
                if (budget == 0 || rarely()) {
                    budget = TestUtil.nextInt(random(), 1, l - k + 5);
                    adder = builder.grow(budget);
                }
                adder.add(array[j++]);
                budget--;
            }
        }
        final DocIdSet expected = new BitDocIdSet(docs);
        final DocIdSet actual = builder.build();
        assertEquals(expected, actual);
    }
}
Also used : DocIdSet(org.apache.lucene.search.DocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 9 with DocIdSet

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

use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.

the class BaseBitSetTestCase method testOr.

private void testOr(float load) throws IOException {
    final int numBits = 1 + random().nextInt(100000);
    // empty
    BitSet set1 = new JavaUtilBitSet(randomSet(numBits, 0), numBits);
    T set2 = copyOf(set1, numBits);
    final int iterations = atLeast(10);
    for (int iter = 0; iter < iterations; ++iter) {
        DocIdSet otherSet = randomCopy(new JavaUtilBitSet(randomSet(numBits, load), numBits), numBits);
        DocIdSetIterator otherIterator = otherSet.iterator();
        if (otherIterator != null) {
            set1.or(otherIterator);
            set2.or(otherSet.iterator());
            assertEquals(set1, set2, numBits);
        }
    }
}
Also used : DocIdSet(org.apache.lucene.search.DocIdSet) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Aggregations

DocIdSet (org.apache.lucene.search.DocIdSet)27 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)16 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)14 Filter (org.apache.solr.search.Filter)6 Bits (org.apache.lucene.util.Bits)5 FixedBitSet (org.apache.lucene.util.FixedBitSet)5 LeafReader (org.apache.lucene.index.LeafReader)4 SortedDocValues (org.apache.lucene.index.SortedDocValues)4 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)4 BitDocIdSet (org.apache.lucene.util.BitDocIdSet)4 IOException (java.io.IOException)3 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Scorer (org.apache.lucene.search.Scorer)3 Weight (org.apache.lucene.search.Weight)3 IndexReader (org.apache.lucene.index.IndexReader)2 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)2 MultiDocValues (org.apache.lucene.index.MultiDocValues)2 MultiSortedSetDocValues (org.apache.lucene.index.MultiDocValues.MultiSortedSetDocValues)2 OrdinalMap (org.apache.lucene.index.MultiDocValues.OrdinalMap)2