Search in sources :

Example 11 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project elasticsearch by elastic.

the class ProfileScorer method twoPhaseIterator.

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }
    final DocIdSetIterator inApproximation = in.approximation();
    final DocIdSetIterator approximation = new DocIdSetIterator() {

        @Override
        public int advance(int target) throws IOException {
            profile.startTime(QueryTimingType.ADVANCE);
            try {
                return inApproximation.advance(target);
            } finally {
                profile.stopAndRecordTime();
            }
        }

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

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

        @Override
        public long cost() {
            return inApproximation.cost();
        }
    };
    return new TwoPhaseIterator(approximation) {

        @Override
        public boolean matches() throws IOException {
            profile.startTime(QueryTimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public float matchCost() {
            return in.matchCost();
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 12 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class ConjunctionSpans method asTwoPhaseIterator.

/**
   * Return a {@link TwoPhaseIterator} view of this ConjunctionSpans.
   */
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
    float totalMatchCost = 0;
    // Compute the matchCost as the total matchCost/positionsCostant of the sub spans.
    for (Spans spans : subSpans) {
        TwoPhaseIterator tpi = spans.asTwoPhaseIterator();
        if (tpi != null) {
            totalMatchCost += tpi.matchCost();
        } else {
            totalMatchCost += spans.positionsCost();
        }
    }
    final float matchCost = totalMatchCost;
    return new TwoPhaseIterator(conjunction) {

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

        @Override
        public float matchCost() {
            return matchCost;
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator)

Example 13 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class CompositeVerifyQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    //scores aren't unsupported
    final Weight indexQueryWeight = indexQuery.createWeight(searcher, false, boost);
    final Map valueSourceContext = ValueSource.newContext(searcher);
    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 FunctionValues predFuncValues = predicateValueSource.getValues(valueSourceContext, context);
            final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(indexQueryScorer.iterator()) {

                @Override
                public boolean matches() throws IOException {
                    return predFuncValues.boolVal(indexQueryScorer.docID());
                }

                @Override
                public float matchCost() {
                    // TODO: use cost of predFuncValues.boolVal()
                    return 100;
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhaseIterator);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) Scorer(org.apache.lucene.search.Scorer) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Map(java.util.Map) Weight(org.apache.lucene.search.Weight) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 14 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class SortedSetDocValuesRangeQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            SortedSetDocValues values = getValues(context.reader(), field);
            if (values == null) {
                return null;
            }
            final long minOrd;
            if (lowerValue == null) {
                minOrd = 0;
            } else {
                final long ord = values.lookupTerm(lowerValue);
                if (ord < 0) {
                    minOrd = -1 - ord;
                } else if (lowerInclusive) {
                    minOrd = ord;
                } else {
                    minOrd = ord + 1;
                }
            }
            final long maxOrd;
            if (upperValue == null) {
                maxOrd = values.getValueCount() - 1;
            } else {
                final long ord = values.lookupTerm(upperValue);
                if (ord < 0) {
                    maxOrd = -2 - ord;
                } else if (upperInclusive) {
                    maxOrd = ord;
                } else {
                    maxOrd = ord - 1;
                }
            }
            if (minOrd > maxOrd) {
                return null;
            }
            final SortedDocValues singleton = DocValues.unwrapSingleton(values);
            final TwoPhaseIterator iterator;
            if (singleton != null) {
                iterator = new TwoPhaseIterator(singleton) {

                    @Override
                    public boolean matches() throws IOException {
                        final long ord = singleton.ordValue();
                        return ord >= minOrd && ord <= maxOrd;
                    }

                    @Override
                    public float matchCost() {
                        // 2 comparisons
                        return 2;
                    }
                };
            } else {
                iterator = new TwoPhaseIterator(values) {

                    @Override
                    public boolean matches() throws IOException {
                        for (long ord = values.nextOrd(); ord != SortedSetDocValues.NO_MORE_ORDS; ord = values.nextOrd()) {
                            if (ord < minOrd) {
                                continue;
                            }
                            // Values are sorted, so the first ord that is >= minOrd is our best candidate
                            return ord <= maxOrd;
                        }
                        // all ords were < minOrd
                        return false;
                    }

                    @Override
                    public float matchCost() {
                        // 2 comparisons
                        return 2;
                    }
                };
            }
            return new ConstantScoreScorer(this, score(), iterator);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) IOException(java.io.IOException) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 15 with TwoPhaseIterator

use of org.apache.lucene.search.TwoPhaseIterator in project lucene-solr by apache.

the class LatLonDocValuesDistanceQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {

        private final GeoEncodingUtils.DistancePredicate distancePredicate = GeoEncodingUtils.createDistancePredicate(latitude, longitude, radiusMeters);

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final SortedNumericDocValues values = context.reader().getSortedNumericDocValues(field);
            if (values == null) {
                return null;
            }
            final TwoPhaseIterator iterator = new TwoPhaseIterator(values) {

                @Override
                public boolean matches() throws IOException {
                    for (int i = 0, count = values.docValueCount(); i < count; ++i) {
                        final long value = values.nextValue();
                        final int lat = (int) (value >>> 32);
                        final int lon = (int) (value & 0xFFFFFFFF);
                        if (distancePredicate.test(lat, lon)) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    // TODO: what should it be?
                    return 100f;
                }
            };
            return new ConstantScoreScorer(this, boost, iterator);
        }
    };
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Aggregations

TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)20 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)13 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)11 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)9 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)9 Scorer (org.apache.lucene.search.Scorer)7 Weight (org.apache.lucene.search.Weight)7 IOException (java.io.IOException)6 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 Set (java.util.Set)3 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)3 Query (org.apache.lucene.search.Query)3 Bits (org.apache.lucene.util.Bits)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Document (org.apache.lucene.document.Document)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 Term (org.apache.lucene.index.Term)2 FunctionValues (org.apache.lucene.queries.function.FunctionValues)2