Example usage for org.apache.lucene.search TwoPhaseIterator TwoPhaseIterator

List of usage examples for org.apache.lucene.search TwoPhaseIterator TwoPhaseIterator

Introduction

In this page you can find the example usage for org.apache.lucene.search TwoPhaseIterator TwoPhaseIterator.

Prototype

protected TwoPhaseIterator(DocIdSetIterator approximation) 

Source Link

Document

Takes the approximation to be returned by #approximation .

Usage

From source file:org.apache.solr.search.Filter.java

License:Apache License

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

        @Override//from   www . j a v a2s.  com
        public void extractTerms(Set<Term> terms) {
        }

        @Override
        public Explanation explain(LeafReaderContext context, int doc) throws IOException {
            final Scorer scorer = scorer(context);
            final boolean match = (scorer != null && scorer.iterator().advance(doc) == doc);
            if (match) {
                assert scorer.score() == 0f;
                return Explanation.match(0f, "Match on id " + doc);
            } else {
                return Explanation.match(0f, "No match on id " + doc);
            }
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final DocIdSet set = getDocIdSet(context, null);
            if (set == null) {
                return null;
            }
            if (applyLazily && set.bits() != null) {
                final Bits bits = set.bits();
                final DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
                final TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
                    @Override
                    public boolean matches() throws IOException {
                        return bits.get(approximation.docID());
                    }

                    @Override
                    public float matchCost() {
                        return 10; // TODO use cost of bits.get()
                    }
                };
                return new ConstantScoreScorer(this, 0f, twoPhase);
            }
            final DocIdSetIterator iterator = set.iterator();
            if (iterator == null) {
                return null;
            }
            return new ConstantScoreScorer(this, 0f, iterator);
        }

    };
}

From source file:org.codelibs.elasticsearch.common.lucene.search.function.MinScoreScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator inTwoPhase = this.in.twoPhaseIterator();
    final DocIdSetIterator approximation = inTwoPhase == null ? in.iterator() : inTwoPhase.approximation();
    return new TwoPhaseIterator(approximation) {

        @Override//from   ww  w. j av  a  2  s .  c om
        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());
        }
    };
}

From source file:org.codelibs.elasticsearch.search.profile.query.ProfileScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }/* ww w .  j  a v a  2s . c o  m*/
    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();
        }
    };
}

From source file:org.elasticsearch.common.lucene.search.function.MinScoreScorerTests.java

License:Apache License

private static Scorer scorer(int maxDoc, final int[] docs, final float[] scores, final boolean twoPhase) {
    final DocIdSetIterator iterator = twoPhase ? DocIdSetIterator.all(maxDoc) : iterator(docs);
    return new Scorer(null) {
        public DocIdSetIterator iterator() {
            if (twoPhase) {
                return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
            } else {
                return iterator;
            }//w ww.  ja v  a 2s . c  o  m
        }

        public TwoPhaseIterator twoPhaseIterator() {
            if (twoPhase) {
                return new TwoPhaseIterator(iterator) {

                    @Override
                    public boolean matches() throws IOException {
                        return Arrays.binarySearch(docs, iterator.docID()) >= 0;
                    }

                    @Override
                    public float matchCost() {
                        return 10;
                    }
                };
            } else {
                return null;
            }
        }

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

        @Override
        public float score() throws IOException {
            final int idx = Arrays.binarySearch(docs, docID());
            return scores[idx];
        }

        @Override
        public int freq() throws IOException {
            return 1;
        }
    };
}

From source file:org.elasticsearch.index.query.PercolatorQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores);
    return new Weight(this) {
        @Override//from  w  ww .ja v  a  2  s . com
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                int result = scorer.iterator().advance(docId);
                if (result == docId) {
                    return Explanation.match(scorer.score(), "PercolatorQuery");
                }
            }
            return Explanation.noMatch("PercolatorQuery");
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return innerWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            innerWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = innerWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }

            final QueryRegistry.Leaf percolatorQueries = queryRegistry.getQueries(leafReaderContext);
            return new Scorer(this) {

                @Override
                public DocIdSetIterator iterator() {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                }

                @Override
                public TwoPhaseIterator twoPhaseIterator() {
                    return new TwoPhaseIterator(approximation.iterator()) {
                        @Override
                        public boolean matches() throws IOException {
                            return matchDocId(approximation.docID());
                        }

                        @Override
                        public float matchCost() {
                            return MATCH_COST;
                        }
                    };
                }

                @Override
                public float score() throws IOException {
                    return approximation.score();
                }

                @Override
                public int freq() throws IOException {
                    return approximation.freq();
                }

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

                boolean matchDocId(int docId) throws IOException {
                    Query query = percolatorQueries.getQuery(docId);
                    if (query != null) {
                        return Lucene.exists(percolatorIndexSearcher, query);
                    } else {
                        return false;
                    }
                }
            };
        }
    };
}

From source file:org.elasticsearch.index.search.geo.GeoDistanceRangeQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight boundingBoxWeight;
    if (boundingBoxFilter != null) {
        boundingBoxWeight = searcher.createNormalizedWeight(boundingBoxFilter, false);
    } else {//from  w  w  w. j  a  v  a2s  .  c o m
        boundingBoxWeight = null;
    }
    return new ConstantScoreWeight(this) {
        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final DocIdSetIterator approximation;
            if (boundingBoxWeight != null) {
                Scorer s = boundingBoxWeight.scorer(context);
                if (s == null) {
                    // if the approximation does not match anything, we're done
                    return null;
                }
                approximation = s.iterator();
            } else {
                approximation = DocIdSetIterator.all(context.reader().maxDoc());
            }
            final MultiGeoPointValues values = indexFieldData.load(context).getGeoPointValues();
            final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                @Override
                public boolean matches() throws IOException {
                    final int doc = approximation.docID();
                    values.setDocument(doc);
                    final int length = values.count();
                    for (int i = 0; i < length; i++) {
                        GeoPoint point = values.valueAt(i);
                        if (distanceBoundingCheck.isWithin(point.lat(), point.lon())) {
                            double d = fixedSourceDistance.calculate(point.lat(), point.lon());
                            if (d >= inclusiveLowerPoint && d <= inclusiveUpperPoint) {
                                return true;
                            }
                        }
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    if (distanceBoundingCheck == GeoDistance.ALWAYS_INSTANCE) {
                        return 0.0f;
                    } else {
                        // TODO: is this right (up to 4 comparisons from GeoDistance.SimpleDistanceBoundingCheck)?
                        return 4.0f;
                    }
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhaseIterator);
        }
    };
}

From source file:org.elasticsearch.index.search.geo.LegacyGeoDistanceRangeQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight boundingBoxWeight;
    if (boundingBoxFilter != null) {
        boundingBoxWeight = searcher.createNormalizedWeight(boundingBoxFilter, false);
    } else {//from  w  w  w .j  a  v a2 s . co m
        boundingBoxWeight = null;
    }
    return new ConstantScoreWeight(this) {
        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final DocIdSetIterator approximation;
            if (boundingBoxWeight != null) {
                Scorer s = boundingBoxWeight.scorer(context);
                if (s == null) {
                    // if the approximation does not match anything, we're done
                    return null;
                }
                approximation = s.iterator();
            } else {
                approximation = DocIdSetIterator.all(context.reader().maxDoc());
            }
            final MultiGeoPointValues values = indexFieldData.load(context).getGeoPointValues();
            final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approximation) {
                @Override
                public boolean matches() throws IOException {
                    final int doc = approximation.docID();
                    values.setDocument(doc);
                    final int length = values.count();
                    for (int i = 0; i < length; i++) {
                        GeoPoint point = values.valueAt(i);
                        if (bbox == null || GeoUtils.rectangleContainsPoint(bbox, point.lat(), point.lon())) {
                            double d = geoDistance.calculate(lat, lon, point.lat(), point.lon(),
                                    DistanceUnit.DEFAULT);
                            if (d >= inclusiveLowerPoint && d <= inclusiveUpperPoint) {
                                return true;
                            }
                        }
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    if (bbox != null) {
                        // always within bounds so we're going to compute distance for every point
                        return values.count();
                    } else {
                        // TODO: come up with better estimate of boundary points
                        return 4.0f;
                    }
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhaseIterator);
        }
    };
}

From source file:org.elasticsearch.percolator.PercolatorQuery.java

License:Apache License

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores);
    return new Weight(this) {
        @Override//w w  w  . java2s . c om
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                int result = scorer.iterator().advance(docId);
                if (result == docId) {
                    return Explanation.match(scorer.score(), "PercolatorQuery");
                }
            }
            return Explanation.noMatch("PercolatorQuery");
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return innerWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            innerWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = innerWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }

            final LeafReader leafReader = leafReaderContext.reader();
            return new Scorer(this) {

                @Override
                public DocIdSetIterator iterator() {
                    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
                }

                @Override
                public TwoPhaseIterator twoPhaseIterator() {
                    return new TwoPhaseIterator(approximation.iterator()) {
                        @Override
                        public boolean matches() throws IOException {
                            return matchDocId(approximation.docID(), leafReader);
                        }

                        @Override
                        public float matchCost() {
                            return MATCH_COST;
                        }
                    };
                }

                @Override
                public float score() throws IOException {
                    return approximation.score();
                }

                @Override
                public int freq() throws IOException {
                    return approximation.freq();
                }

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

                boolean matchDocId(int docId, LeafReader leafReader) throws IOException {
                    SingleFieldsVisitor singleFieldsVisitor = new SingleFieldsVisitor(UidFieldMapper.NAME);
                    leafReader.document(docId, singleFieldsVisitor);
                    BytesRef percolatorQueryId = new BytesRef(singleFieldsVisitor.uid().id());
                    return matchQuery(percolatorQueryId);
                }
            };
        }
    };
}

From source file:org.elasticsearch.search.profile.ProfileScorer.java

License:Apache License

@Override
public TwoPhaseIterator twoPhaseIterator() {
    final TwoPhaseIterator in = scorer.twoPhaseIterator();
    if (in == null) {
        return null;
    }//  ww  w  .  ja v a  2  s  .  c o  m
    final DocIdSetIterator inApproximation = in.approximation();
    final DocIdSetIterator approximation = new DocIdSetIterator() {

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

        @Override
        public int nextDoc() throws IOException {
            profile.startTime(ProfileBreakdown.TimingType.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(ProfileBreakdown.TimingType.MATCH);
            try {
                return in.matches();
            } finally {
                profile.stopAndRecordTime();
            }
        }

        @Override
        public float matchCost() {
            return in.matchCost();
        }
    };
}

From source file:org.hibernate.search.spatial.impl.DistanceQuery.java

License:LGPL

/**
 * Returns a {@link TwoPhaseIterator} that will first check the {@link #approximationQuery} (if any),
 * and will only match documents whose coordinates are within distance(radius) of the center of the search.
 *
 * @param approximation an approximation of matching documents.
 * @param context the {@link LeafReaderContext} for which to return the {LeafReaderContext}.
 *
 * @return a {@link TwoPhaseIterator} with the matching document ids
 *///from   w  w w .j  a v a 2s  .  c  o m
private TwoPhaseIterator createDocIdSetIterator(DocIdSetIterator approximation, LeafReaderContext context)
        throws IOException {
    return new TwoPhaseIterator(approximation) {

        private Bits docsWithLatitude;
        private Bits docsWithLongitude;
        private NumericDocValues latitudeValues;
        private NumericDocValues longitudeValues;

        private void lazyInit() throws IOException {
            if (docsWithLatitude != null) {
                return;
            }
            LeafReader atomicReader = context.reader();
            this.docsWithLatitude = DocValues.getDocsWithField(atomicReader, getLatitudeField());
            this.docsWithLongitude = DocValues.getDocsWithField(atomicReader, getLongitudeField());
            this.latitudeValues = DocValues.getNumeric(atomicReader, getLatitudeField());
            this.longitudeValues = DocValues.getNumeric(atomicReader, getLongitudeField());
        }

        @Override
        public boolean matches() throws IOException {
            lazyInit();
            int docID = approximation().docID();
            if (docsWithLatitude.get(docID) && docsWithLongitude.get(docID)) {
                double lat = coordinate(latitudeValues, docID);
                double lon = coordinate(longitudeValues, docID);
                if (center.getDistanceTo(lat, lon) <= radius) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public float matchCost() {
            /*
             * I honestly have no idea how many "simple operations" we're performing here.
             * I suppose sines and cosines are very low-level, probably assembly instructions
             * on most architectures.
             * Some Lucene implementations seem to use 100 as a default, so let's do the same.
             */
            return 100;
        }
    };
}