org.apache.lucene.search.TwoPhaseIterator Java Examples

The following examples show how to use org.apache.lucene.search.TwoPhaseIterator. 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: FilterSpans.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public final TwoPhaseIterator asTwoPhaseIterator() {
  TwoPhaseIterator inner = in.asTwoPhaseIterator();
  if (inner != null) {
    // wrapped instance has an approximation
    return new TwoPhaseIterator(inner.approximation()) {
      @Override
      public boolean matches() throws IOException {
        return inner.matches() && twoPhaseCurrentDocMatches();
      }

      @Override
      public float matchCost() {
        return inner.matchCost(); // underestimate
      }

      @Override
      public String toString() {
        return "FilterSpans@asTwoPhaseIterator(inner=" + inner + ", in=" + in + ")";
      }
    };
  } else {
    // wrapped instance has no approximation, but 
    // we can still defer matching until absolutely needed.
    return new TwoPhaseIterator(in) {
 
Example #3
Source File: SerializedDVStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
      TwoPhaseIterator it = predicateValueSource.iterator(context, approximation);
      return new ConstantScoreScorer(this, score(), scoreMode, it);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return predicateValueSource.isCacheable(ctx);
    }

  };
}
 
Example #4
Source File: CompositeVerifyQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  final Weight indexQueryWeight = indexQuery.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, boost);//scores aren't unsupported

  return new ConstantScoreWeight(this, boost) {

    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {

      final Scorer indexQueryScorer = indexQueryWeight.scorer(context);
      if (indexQueryScorer == null) {
        return null;
      }

      final TwoPhaseIterator predFuncValues = predicateValueSource.iterator(context, indexQueryScorer.iterator());
      return new ConstantScoreScorer(this, score(), scoreMode, predFuncValues);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return predicateValueSource.isCacheable(ctx);
    }

  };
}
 
Example #5
Source File: GlobalOrdinalsQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
  return new TwoPhaseIterator(approximation) {

    @Override
    public boolean matches() throws IOException {
      if (values.advanceExact(approximation.docID())) {
        final long segmentOrd = values.ordValue();
        final long globalOrd = segmentOrdToGlobalOrdLookup.get(segmentOrd);
        if (foundOrds.get(globalOrd)) {
          return true;
        }
      }
      return false;
    }

    @Override
    public float matchCost() {
      return 100; // TODO: use cost of values.getOrd() and foundOrds.get()
    }
  };
}
 
Example #6
Source File: ValueSourceScorer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected ValueSourceScorer(Weight weight, LeafReaderContext readerContext, FunctionValues values) {
  super(weight);
  this.values = values;
  final DocIdSetIterator approximation = DocIdSetIterator.all(readerContext.reader().maxDoc()); // no approximation!
  this.twoPhaseIterator = new TwoPhaseIterator(approximation) {
    @Override
    public boolean matches() throws IOException {
      return ValueSourceScorer.this.matches(approximation.docID());
    }

    @Override
    public float matchCost() {
      return ValueSourceScorer.this.matchCost();
    }
  };
  this.disi = TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator);
}
 
Example #7
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 #8
Source File: GlobalOrdinalsQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
  return new TwoPhaseIterator(approximation) {

    @Override
    public boolean matches() throws IOException {
      if (values.advanceExact(approximation.docID()) && foundOrds.get(values.ordValue())) {
        return true;
      }
      return false;
    }

    @Override
    public float matchCost() {
      return 100; // TODO: use cost of values.getOrd() and foundOrds.get()
    }
  };
}
 
Example #9
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 #10
Source File: GlobalOrdinalsWithScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
  return new TwoPhaseIterator(approximation) {

    @Override
    public boolean matches() throws IOException {
      if (values.advanceExact(approximation.docID())) {
        final long segmentOrd = values.ordValue();
        final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(segmentOrd);
        if (collector.match(globalOrd)) {
          score = collector.score(globalOrd);
          return true;
        }
      }
      return false;
    }

    @Override
    public float matchCost() {
      return 100; // TODO: use cost of values.getOrd() and collector.score()
    }
  };
}
 
Example #11
Source File: GlobalOrdinalsWithScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
  return new TwoPhaseIterator(approximation) {

    @Override
    public boolean matches() throws IOException {
      if (values.advanceExact(approximation.docID())) {
        final int segmentOrd = values.ordValue();
        if (collector.match(segmentOrd)) {
          score = collector.score(segmentOrd);
          return true;
        }
      }
      return false;
    }

    @Override
    public float matchCost() {
      return 100; // TODO: use cost.getOrd() of values and collector.score()
    }
  };
}
 
Example #12
Source File: MtasSpanMatchNoneSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example #13
Source File: MtasSpanRecurrenceSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (spans == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example #14
Source File: MtasSpanIntersectingSpans.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 {
    // TODO
    return null;
  }
}
 
Example #15
Source File: AssertingSpans.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  final TwoPhaseIterator iterator = in.asTwoPhaseIterator();
  if (iterator == null) {
    return null;
  }
  return new AssertingTwoPhaseView(iterator);
}
 
Example #16
Source File: MtasSpanFullyAlignedWithSpans.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 {
    // TODO
    return null;
  }
}
 
Example #17
Source File: MtasSpanEndSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (spans == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    return spans.asTwoPhaseIterator();
  }
}
 
Example #18
Source File: MtasSpanPositionSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example #19
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 #20
Source File: MtasSpanStartSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (spans == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    return spans.asTwoPhaseIterator();
  }
}
 
Example #21
Source File: MtasSpanMatchAllSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example #22
Source File: MtasSpanPrecededBySpans.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 {
    // TODO
    return null;
  }
}
 
Example #23
Source File: ToParentBlockJoinQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public DocIdSetIterator iterator() {
  if (parentTwoPhase == null) {
    // the approximation is exact
    return parentApproximation;
  } else {
    return TwoPhaseIterator.asDocIdSetIterator(parentTwoPhase);
  }
}
 
Example #24
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 #25
Source File: MtasSpanSequenceSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (queueSpans == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example #26
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 #27
Source File: IntervalScorer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator twoPhaseIterator() {
  return new TwoPhaseIterator(intervals) {
    @Override
    public boolean matches() throws IOException {
      return intervals.nextInterval() != IntervalIterator.NO_MORE_INTERVALS;
    }

    @Override
    public float matchCost() {
      return intervals.matchCost();
    }
  };
}
 
Example #28
Source File: MtasSpanUniquePositionSpans.java    From mtas with Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (spans == null || !query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    return spans.asTwoPhaseIterator();
  }
}
 
Example #29
Source File: FunctionMatchQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  DoubleValuesSource vs = source.rewrite(searcher);
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      DoubleValues values = vs.getValues(context, null);
      DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
      TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
          return values.advanceExact(approximation.docID()) && filter.test(values.doubleValue());
        }

        @Override
        public float matchCost() {
          return 100; // TODO maybe DoubleValuesSource should have a matchCost?
        }
      };
      return new ConstantScoreScorer(this, score(), scoreMode, twoPhase);
    }

    @Override
    public boolean isCacheable(LeafReaderContext ctx) {
      return source.isCacheable(ctx);
    }

  };
}
 
Example #30
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();
        }
      };
    }
  }
}