Java Code Examples for org.apache.lucene.search.TwoPhaseIterator#approximation()

The following examples show how to use org.apache.lucene.search.TwoPhaseIterator#approximation() . 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: MinScoreScorer.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator inTwoPhase = this.in.twoPhaseIterator();
    final DocIdSetIterator approximation = inTwoPhase == null ? in.iterator() : inTwoPhase.approximation();
    return new TwoPhaseIterator(approximation) {

        @Override
        public boolean matches() throws IOException {
            // we need to check the two-phase iterator first
            // otherwise calling score() is illegal
            if (inTwoPhase != null && inTwoPhase.matches() == false) {
                return false;
            }
            return in.score() >= minScore;
        }

        @Override
        public float matchCost() {
            return 1000f // random constant for the score computation
                    + (inTwoPhase == null ? 0 : inTwoPhase.matchCost());
        }
    };
}
 
Example 2
Source File: DrillSidewaysScorer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
DocsAndCost(Scorer scorer, Collector sidewaysCollector) {
  final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
  if (twoPhase == null) {
    this.approximation = scorer.iterator();
    this.twoPhase = null;
  } else {
    this.approximation = twoPhase.approximation();
    this.twoPhase = twoPhase;
  }
  this.sidewaysCollector = sidewaysCollector;
}
 
Example 3
Source File: MtasSpanNotSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (spans1 == null || spans2 == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {

    TwoPhaseIterator twoPhaseIterator1 = spans1.spans.asTwoPhaseIterator();
    if (twoPhaseIterator1 != null) {
      return new TwoPhaseIterator(twoPhaseIterator1.approximation()) {
        @Override
        public boolean matches() throws IOException {
          return twoPhaseIterator1.matches() && twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return twoPhaseIterator1.matchCost();
        }
      };
    } else {
      return new TwoPhaseIterator(spans1.spans) {
        @Override
        public boolean matches() throws IOException {
          return twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return spans1.spans.positionsCost();
        }
      };
    }
  }
}
 
Example 4
Source File: MtasMaximumExpandSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    TwoPhaseIterator originalTwoPhaseIterator = subSpans.asTwoPhaseIterator();
    if (originalTwoPhaseIterator != null) {
      return new TwoPhaseIterator(originalTwoPhaseIterator.approximation()) {
        @Override
        public boolean matches() throws IOException {
          return originalTwoPhaseIterator.matches()
              && twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return originalTwoPhaseIterator.matchCost();
        }
      };
    } else {
      return new TwoPhaseIterator(subSpans) {

        @Override
        public boolean matches() throws IOException {
          return twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return subSpans.positionsCost();
        }
      };
    }
  }
}
 
Example 5
Source File: MtasExpandSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    TwoPhaseIterator originalTwoPhaseIterator = subSpans.asTwoPhaseIterator();
    if (originalTwoPhaseIterator != null) {
      return new TwoPhaseIterator(originalTwoPhaseIterator.approximation()) {
        @Override
        public boolean matches() throws IOException {
          return originalTwoPhaseIterator.matches()
              && twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return originalTwoPhaseIterator.matchCost();
        }
      };
    } else {
      return new TwoPhaseIterator(subSpans) {
        @Override
        public boolean matches() throws IOException {
          return twoPhaseCurrentDocMatches();
        }

        @Override
        public float matchCost() {
          return subSpans.positionsCost();
        }
      };
    }
  }
}
 
Example 6
Source File: ToParentBlockJoinQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
ParentTwoPhase(ParentApproximation parentApproximation, TwoPhaseIterator childTwoPhase) {
  super(parentApproximation);
  this.parentApproximation = parentApproximation;
  this.childApproximation = childTwoPhase.approximation();
  this.childTwoPhase = childTwoPhase;
}
 
Example 7
Source File: AssertingSpans.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
AssertingTwoPhaseView(TwoPhaseIterator iterator) {
  super(new AssertingDISI(iterator.approximation()));
  this.in = iterator;
}
 
Example 8
Source File: SpanNotQuery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Spans getSpans(final LeafReaderContext context, Postings requiredPostings) throws IOException {
  Spans includeSpans = includeWeight.getSpans(context, requiredPostings);
  if (includeSpans == null) {
    return null;
  }

  Spans excludeSpans = excludeWeight.getSpans(context, requiredPostings);
  if (excludeSpans == null) {
    return includeSpans;
  }

  TwoPhaseIterator excludeTwoPhase = excludeSpans.asTwoPhaseIterator();
  DocIdSetIterator excludeApproximation = excludeTwoPhase == null ? null : excludeTwoPhase.approximation();

  return new FilterSpans(includeSpans) {
    // last document we have checked matches() against for the exclusion, and failed
    // when using approximations, so we don't call it again, and pass thru all inclusions.
    int lastApproxDoc = -1;
    boolean lastApproxResult = false;

    @Override
    protected AcceptStatus accept(Spans candidate) throws IOException {
      // TODO: this logic is ugly and sneaky, can we clean it up?
      int doc = candidate.docID();
      if (doc > excludeSpans.docID()) {
        // catch up 'exclude' to the current doc
        if (excludeTwoPhase != null) {
          if (excludeApproximation.advance(doc) == doc) {
            lastApproxDoc = doc;
            lastApproxResult = excludeTwoPhase.matches();
          }
        } else {
          excludeSpans.advance(doc);
        }
      } else if (excludeTwoPhase != null && doc == excludeSpans.docID() && doc != lastApproxDoc) {
        // excludeSpans already sitting on our candidate doc, but matches not called yet.
        lastApproxDoc = doc;
        lastApproxResult = excludeTwoPhase.matches();
      }

      if (doc != excludeSpans.docID() || (doc == lastApproxDoc && lastApproxResult == false)) {
        return AcceptStatus.YES;
      }

      if (excludeSpans.startPosition() == -1) { // init exclude start position if needed
        excludeSpans.nextStartPosition();
      }

      while (excludeSpans.endPosition() <= candidate.startPosition() - pre) {
        // exclude end position is before a possible exclusion
        if (excludeSpans.nextStartPosition() == NO_MORE_POSITIONS) {
          return AcceptStatus.YES; // no more exclude at current doc.
        }
      }

      // exclude end position far enough in current doc, check start position:
      if (excludeSpans.startPosition() - post >= candidate.endPosition()) {
        return AcceptStatus.YES;
      } else {
        return AcceptStatus.NO;
      }
    }
  };
}