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

The following examples show how to use org.apache.lucene.search.TwoPhaseIterator#matches() . 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: MatchedQueriesFetchSubPhase.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMatchedQueries(HitContext hitContext, ImmutableMap<String, Query> namedQueries, List<String> matchedQueries) throws IOException {
    for (Map.Entry<String, Query> entry : namedQueries.entrySet()) {
        String name = entry.getKey();
        Query filter = entry.getValue();

        final Weight weight = hitContext.topLevelSearcher().createNormalizedWeight(filter, false);
        final Scorer scorer = weight.scorer(hitContext.readerContext());
        if (scorer == null) {
            continue;
        }
        final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
        if (twoPhase == null) {
            if (scorer.iterator().advance(hitContext.docId()) == hitContext.docId()) {
                matchedQueries.add(name);
            }
        } else {
            if (twoPhase.approximation().advance(hitContext.docId()) == hitContext.docId() && twoPhase.matches()) {
                matchedQueries.add(name);
            }
        }
    }
}
 
Example 2
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 3
Source File: ToParentBlockJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Matches matches(LeafReaderContext context, int doc) throws IOException {
  // The default implementation would delegate to the joinQuery's Weight, which
  // matches on children.  We need to match on the parent instead
  Scorer scorer = scorer(context);
  if (scorer == null) {
    return null;
  }
  final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
  if (twoPhase == null) {
    if (scorer.iterator().advance(doc) != doc) {
      return null;
    }
  }
  else {
    if (twoPhase.approximation().advance(doc) != doc || twoPhase.matches() == false) {
      return null;
    }
  }
  return MatchesUtils.MATCH_WITH_NO_TERMS;
}
 
Example 4
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 5
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 6
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 7
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;
      }
    }
  };
}