Java Code Examples for org.apache.lucene.search.TwoPhaseIterator

The following examples show how to use org.apache.lucene.search.TwoPhaseIterator. These examples are extracted from open source projects. 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 Project: Elasticsearch   Source File: MatchedQueriesFetchSubPhase.java    License: 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 Project: Elasticsearch   Source File: MinScoreScorer.java    License: 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 Project: lucene-solr   Source File: SerializedDVStrategy.java    License: 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 Project: lucene-solr   Source File: CompositeVerifyQuery.java    License: 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 Project: lucene-solr   Source File: ValueSourceScorer.java    License: 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 6
Source Project: lucene-solr   Source File: ToParentBlockJoinQuery.java    License: 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 7
Source Project: lucene-solr   Source File: GlobalOrdinalsQuery.java    License: 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 8
Source Project: lucene-solr   Source File: GlobalOrdinalsQuery.java    License: 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 Project: lucene-solr   Source File: GlobalOrdinalsWithScoreQuery.java    License: 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 10
Source Project: lucene-solr   Source File: GlobalOrdinalsWithScoreQuery.java    License: 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 11
Source Project: lucene-solr   Source File: FilterSpans.java    License: 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 "[email protected](inner=" + inner + ", in=" + in + ")";
      }
    };
  } else {
    // wrapped instance has no approximation, but 
    // we can still defer matching until absolutely needed.
    return new TwoPhaseIterator(in) {
 
Example 12
Source Project: lucene-solr   Source File: PointVectorStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
  Weight w = inner.createWeight(searcher, scoreMode, 1f);
  return new ConstantScoreWeight(this, boost) {
    @Override
    public Scorer scorer(LeafReaderContext context) throws IOException {
      Scorer in = w.scorer(context);
      if (in == null)
        return null;
      DoubleValues v = distanceSource.getValues(context, DoubleValuesSource.fromScorer(in));
      DocIdSetIterator approximation = in.iterator();
      TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
        @Override
        public boolean matches() throws IOException {
          return v.advanceExact(approximation.docID()) && v.doubleValue() <= limit;
        }

        @Override
        public float matchCost() {
          return 100;   // distance calculation can be heavy!
        }
      };
      return new ConstantScoreScorer(this, score(), scoreMode, twoPhase);
    }

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

  };
}
 
Example 13
Source Project: lucene-solr   Source File: ShapeValuesPredicate.java    License: Apache License 2.0 5 votes vote down vote up
public TwoPhaseIterator iterator(LeafReaderContext ctx, DocIdSetIterator approximation) throws IOException {
  final ShapeValues shapeValues = shapeValuesource.getValues(ctx);
  return new TwoPhaseIterator(approximation) {
    @Override
    public boolean matches() throws IOException {
      return shapeValues.advanceExact(approximation.docID()) && op.evaluate(shapeValues.value(), queryShape);
    }

    @Override
    public float matchCost() {
      return 100; // is this necessary?
    }
  };
}
 
Example 14
Source Project: lucene-solr   Source File: FunctionMatchQuery.java    License: 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 15
Source Project: lucene-solr   Source File: IntervalScorer.java    License: 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 16
Source Project: lucene-solr   Source File: DrillSidewaysScorer.java    License: 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 17
Source Project: lucene-solr   Source File: ToParentBlockJoinQuery.java    License: 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 18
Source Project: lucene-solr   Source File: AssertingSpans.java    License: 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 19
Source Project: mtas   Source File: MtasSpanSequenceSpans.java    License: 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 20
Source Project: mtas   Source File: MtasSpanFullyAlignedWithSpans.java    License: 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 21
Source Project: mtas   Source File: MtasSpanStartSpans.java    License: 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 22
Source Project: mtas   Source File: MtasSpanNotSpans.java    License: 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 23
Source Project: mtas   Source File: MtasSpanPositionSpans.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example 24
Source Project: mtas   Source File: MtasSpanEndSpans.java    License: 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 25
Source Project: mtas   Source File: MtasSpanMatchNoneSpans.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example 26
Source Project: mtas   Source File: MtasSpanIntersectingSpans.java    License: 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 27
Source Project: mtas   Source File: MtasSpanRecurrenceSpans.java    License: 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 28
Source Project: mtas   Source File: MtasSpanMatchAllSpans.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
  if (!query.twoPhaseIteratorAllowed()) {
    return null;
  } else {
    // TODO
    return null;
  }
}
 
Example 29
Source Project: mtas   Source File: MtasSpanPrecededBySpans.java    License: 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 30
Source Project: mtas   Source File: MtasMaximumExpandSpans.java    License: 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();
        }
      };
    }
  }
}