Java Code Examples for org.apache.lucene.search.DocIdSetIterator#docID()

The following examples show how to use org.apache.lucene.search.DocIdSetIterator#docID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBlockJoinSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static void assertNoMoreDoc(DocIdSetIterator sdv, int maxDoc) throws IOException{
  Random r = random();
  if(r.nextBoolean()){
    assertEquals(NO_MORE_DOCS, sdv.nextDoc());
  } else {
    if (r.nextBoolean()) {
      assertEquals(NO_MORE_DOCS,  sdv.advance(sdv.docID()+random().nextInt(maxDoc-sdv.docID())));
    } else {
      final int noMatchDoc = sdv.docID()+random().nextInt(maxDoc-sdv.docID()-1)+1;
      assertFalse(advanceExact(sdv,noMatchDoc));
      assertEquals(noMatchDoc, sdv.docID());
      if (r.nextBoolean()){
        assertEquals(NO_MORE_DOCS, sdv.nextDoc());
      }
    }
  }
}
 
Example 2
Source File: TestBlockJoinSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static int nextDoc(DocIdSetIterator sdv, int docId) throws IOException{
  Random r = random();
  if(r.nextBoolean()){
    return sdv.nextDoc();
  } else {
    if (r.nextBoolean()) {
      return sdv.advance(sdv.docID()+random().nextInt(docId-sdv.docID()-1)+1);
    } else {
      if (r.nextBoolean()){
        final int noMatchDoc = sdv.docID()+random().nextInt(docId-sdv.docID()-1)+1;
        assertFalse(advanceExact(sdv,noMatchDoc));
        assertEquals(noMatchDoc, sdv.docID());
      }
      assertTrue(advanceExact(sdv,docId));
      return sdv.docID();
    }
  }
}
 
Example 3
Source File: ProfileScorer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public DocIdSetIterator iterator() {
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {
        
        @Override
        public int advance(int target) throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.ADVANCE);
            try {
                return in.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.NEXT_DOC);
            try {
                return in.nextDoc();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

        @Override
        public long cost() {
            return in.cost();
        }
    };
}
 
Example 4
Source File: TestMultiDocValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void testRandomAdvance(DocIdSetIterator iter1, DocIdSetIterator iter2) throws IOException {
  assertEquals(-1, iter1.docID());
  assertEquals(-1, iter2.docID());

  while (iter1.docID() != NO_MORE_DOCS) {
    if (random().nextBoolean()) {
      assertEquals(iter1.nextDoc(), iter2.nextDoc());
    } else {
      int target = iter1.docID() + TestUtil.nextInt(random(), 1, 100);
      assertEquals(iter1.advance(target), iter2.advance(target));
    }
  }
}
 
Example 5
Source File: ScriptFeature.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
    LeafScoreFunction leafScoreFunction = function.getLeafScoreFunction(context);
    DocIdSetIterator iterator = DocIdSetIterator.all(context.reader().maxDoc());
    return new Scorer(this) {
        @Override
        public int docID() {
            return iterator.docID();
        }

        @Override
        public float score() throws IOException {
            return (float) leafScoreFunction.score(iterator.docID(), 0F);
        }

        @Override
        public DocIdSetIterator iterator() {
            return iterator;
        }

        /**
         * Return the maximum score that documents between the last {@code target}
         * that this iterator was {@link #advanceShallow(int) shallow-advanced} to
         * included and {@code upTo} included.
         */
        @Override
        public float getMaxScore(int upTo) throws IOException {
            //TODO??
            return Float.POSITIVE_INFINITY;
        }
    };
}
 
Example 6
Source File: ProfileScorer.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public DocIdSetIterator iterator() {
    if (isConstantScoreQuery) {
        return scorer.iterator();
    }
    final DocIdSetIterator in = scorer.iterator();
    return new DocIdSetIterator() {

        @Override
        public int advance(int target) throws IOException {
            advanceTimer.start();
            try {
                return in.advance(target);
            } finally {
                advanceTimer.stop();
            }
        }

        @Override
        public int nextDoc() throws IOException {
            nextDocTimer.start();
            try {
                return in.nextDoc();
            } finally {
                nextDocTimer.stop();
            }
        }

        @Override
        public int docID() {
            return in.docID();
        }

        @Override
        public long cost() {
            return in.cost();
        }
    };
}
 
Example 7
Source File: GeoDistanceRangeQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight boundingBoxWeight;
    if (boundingBoxFilter != null) {
        boundingBoxWeight = searcher.createNormalizedWeight(boundingBoxFilter, false);
    } else {
        boundingBoxWeight = null;
    }
    return new ConstantScoreWeight(this) {
        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final DocIdSetIterator approximation;
            if (boundingBoxWeight != null) {
                Scorer s = boundingBoxWeight.scorer(context);
                if (s == null) {
                    // if the approximation does not match anything, we're done
                    return null;
                }
                approximation = s.iterator();
            } else {
                approximation = DocIdSetIterator.all(context.reader().maxDoc());
            }
            final MultiGeoPointValues values = indexFieldData.load(context).getGeoPointValues();
            final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                @Override
                public boolean matches() throws IOException {
                    final int doc = approximation.docID();
                    values.setDocument(doc);
                    final int length = values.count();
                    for (int i = 0; i < length; i++) {
                        GeoPoint point = values.valueAt(i);
                        if (distanceBoundingCheck.isWithin(point.lat(), point.lon())) {
                            double d = fixedSourceDistance.calculate(point.lat(), point.lon());
                            if (d >= inclusiveLowerPoint && d <= inclusiveUpperPoint) {
                                return true;
                            }
                        }
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    if (distanceBoundingCheck == GeoDistance.ALWAYS_INSTANCE) {
                        return 0.0f;
                    } else {
                        // TODO: is this right (up to 4 comparisons from GeoDistance.SimpleDistanceBoundingCheck)?
                        return 4.0f;
                    }
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhaseIterator);
        }
    };
}
 
Example 8
Source File: DoubleRangeFacetCounts.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void count(DoubleValuesSource valueSource, List<MatchingDocs> matchingDocs) throws IOException {

    DoubleRange[] ranges = (DoubleRange[]) this.ranges;

    LongRange[] longRanges = new LongRange[ranges.length];
    for(int i=0;i<ranges.length;i++) {
      DoubleRange range = ranges[i];
      longRanges[i] =  new LongRange(range.label,
                                     NumericUtils.doubleToSortableLong(range.min), true,
                                     NumericUtils.doubleToSortableLong(range.max), true);
    }

    LongRangeCounter counter = new LongRangeCounter(longRanges);

    int missingCount = 0;
    for (MatchingDocs hits : matchingDocs) {
      DoubleValues fv = valueSource.getValues(hits.context, null);
      
      totCount += hits.totalHits;
      final DocIdSetIterator fastMatchDocs;
      if (fastMatchQuery != null) {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(hits.context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight fastMatchWeight = searcher.createWeight(searcher.rewrite(fastMatchQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
        Scorer s = fastMatchWeight.scorer(hits.context);
        if (s == null) {
          continue;
        }
        fastMatchDocs = s.iterator();
      } else {
        fastMatchDocs = null;
      }

      DocIdSetIterator docs = hits.bits.iterator();

      for (int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
        if (fastMatchDocs != null) {
          int fastMatchDoc = fastMatchDocs.docID();
          if (fastMatchDoc < doc) {
            fastMatchDoc = fastMatchDocs.advance(doc);
          }

          if (doc != fastMatchDoc) {
            doc = docs.advance(fastMatchDoc);
            continue;
          }
        }
        // Skip missing docs:
        if (fv.advanceExact(doc)) {
          counter.add(NumericUtils.doubleToSortableLong(fv.doubleValue()));
        } else {
          missingCount++;
        }

        doc = docs.nextDoc();
      }
    }

    missingCount += counter.fillCounts(counts);
    totCount -= missingCount;
  }
 
Example 9
Source File: LongRangeFacetCounts.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void count(LongValuesSource valueSource, List<MatchingDocs> matchingDocs) throws IOException {

    LongRange[] ranges = (LongRange[]) this.ranges;

    LongRangeCounter counter = new LongRangeCounter(ranges);

    int missingCount = 0;
    for (MatchingDocs hits : matchingDocs) {
      LongValues fv = valueSource.getValues(hits.context, null);
      
      totCount += hits.totalHits;
      final DocIdSetIterator fastMatchDocs;
      if (fastMatchQuery != null) {
        final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(hits.context);
        final IndexSearcher searcher = new IndexSearcher(topLevelContext);
        searcher.setQueryCache(null);
        final Weight fastMatchWeight = searcher.createWeight(searcher.rewrite(fastMatchQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
        Scorer s = fastMatchWeight.scorer(hits.context);
        if (s == null) {
          continue;
        }
        fastMatchDocs = s.iterator();
      } else {
        fastMatchDocs = null;
      }

      DocIdSetIterator docs = hits.bits.iterator();      
      for (int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
        if (fastMatchDocs != null) {
          int fastMatchDoc = fastMatchDocs.docID();
          if (fastMatchDoc < doc) {
            fastMatchDoc = fastMatchDocs.advance(doc);
          }

          if (doc != fastMatchDoc) {
            doc = docs.advance(fastMatchDoc);
            continue;
          }
        }
        // Skip missing docs:
        if (fv.advanceExact(doc)) {
          counter.add(fv.longValue());
        } else {
          missingCount++;
        }

        doc = docs.nextDoc();
      }
    }
    
    int x = counter.fillCounts(counts);

    missingCount += x;

    //System.out.println("totCount " + totCount + " x " + x + " missingCount " + missingCount);
    totCount -= missingCount;
  }
 
Example 10
Source File: BitSet.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Assert that the current doc is -1. */
protected final void checkUnpositioned(DocIdSetIterator iter) {
  if (iter.docID() != -1) {
    throw new IllegalStateException("This operation only works with an unpositioned iterator, got current position = " + iter.docID());
  }
}
 
Example 11
Source File: DocumentVisibilityFilter.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(DocIdSetIterator o1, DocIdSetIterator o2) {
  int docID1 = o1.docID();
  int docID2 = o2.docID();
  return docID1 - docID2;
}