Search in sources :

Example 1 with ConstantScoreWeight

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

the class MinDocQuery method createWeight.

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

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            final int maxDoc = context.reader().maxDoc();
            if (context.docBase + maxDoc <= minDoc) {
                return null;
            }
            final int segmentMinDoc = Math.max(0, minDoc - context.docBase);
            final DocIdSetIterator disi = new DocIdSetIterator() {

                int doc = -1;

                @Override
                public int docID() {
                    return doc;
                }

                @Override
                public int nextDoc() throws IOException {
                    return advance(doc + 1);
                }

                @Override
                public int advance(int target) throws IOException {
                    assert target > doc;
                    if (doc == -1) {
                        // skip directly to minDoc
                        doc = Math.max(target, segmentMinDoc);
                    } else {
                        doc = target;
                    }
                    if (doc >= maxDoc) {
                        doc = NO_MORE_DOCS;
                    }
                    return doc;
                }

                @Override
                public long cost() {
                    return maxDoc - segmentMinDoc;
                }
            };
            return new ConstantScoreScorer(this, score(), disi);
        }
    };
}
Also used : ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 2 with ConstantScoreWeight

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

the class RangeFieldQuery method createWeight.

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

        final RangeFieldComparator target = new RangeFieldComparator();

        private DocIdSet buildMatchingDocIdSet(LeafReader reader, PointValues values) throws IOException {
            DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values, field);
            values.intersect(new IntersectVisitor() {

                DocIdSetBuilder.BulkAdder adder;

                @Override
                public void grow(int count) {
                    adder = result.grow(count);
                }

                @Override
                public void visit(int docID) throws IOException {
                    adder.add(docID);
                }

                @Override
                public void visit(int docID, byte[] leaf) throws IOException {
                    if (target.matches(leaf)) {
                        adder.add(docID);
                    }
                }

                @Override
                public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
                    return compareRange(minPackedValue, maxPackedValue);
                }
            });
            return result.build();
        }

        private Relation compareRange(byte[] minPackedValue, byte[] maxPackedValue) {
            byte[] node = getInternalRange(minPackedValue, maxPackedValue);
            // compute range relation for BKD traversal
            if (target.intersects(node) == false) {
                return Relation.CELL_OUTSIDE_QUERY;
            } else if (target.within(node)) {
                // target within cell; continue traversing:
                return Relation.CELL_CROSSES_QUERY;
            } else if (target.contains(node)) {
                // target contains cell; add iff queryType is not a CONTAINS or CROSSES query:
                return (queryType == QueryType.CONTAINS || queryType == QueryType.CROSSES) ? Relation.CELL_OUTSIDE_QUERY : Relation.CELL_INSIDE_QUERY;
            }
            // target intersects cell; continue traversing:
            return Relation.CELL_CROSSES_QUERY;
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            LeafReader reader = context.reader();
            PointValues values = reader.getPointValues(field);
            if (values == null) {
                // no docs in this segment indexed any ranges
                return null;
            }
            FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
            if (fieldInfo == null) {
                // no docs in this segment indexed this field
                return null;
            }
            checkFieldInfo(fieldInfo);
            boolean allDocsMatch = false;
            if (values.getDocCount() == reader.maxDoc() && compareRange(values.getMinPackedValue(), values.getMaxPackedValue()) == Relation.CELL_INSIDE_QUERY) {
                allDocsMatch = true;
            }
            DocIdSetIterator iterator = allDocsMatch == true ? DocIdSetIterator.all(reader.maxDoc()) : buildMatchingDocIdSet(reader, values).iterator();
            return new ConstantScoreScorer(this, score(), iterator);
        }

        /** get an encoded byte representation of the internal node; this is
       *  the lower half of the min array and the upper half of the max array */
        private byte[] getInternalRange(byte[] min, byte[] max) {
            byte[] range = new byte[min.length];
            final int dimSize = numDims * bytesPerDim;
            System.arraycopy(min, 0, range, 0, dimSize);
            System.arraycopy(max, dimSize, range, dimSize, dimSize);
            return range;
        }
    };
}
Also used : IntersectVisitor(org.apache.lucene.index.PointValues.IntersectVisitor) LeafReader(org.apache.lucene.index.LeafReader) IOException(java.io.IOException) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) PointValues(org.apache.lucene.index.PointValues) Relation(org.apache.lucene.index.PointValues.Relation) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetBuilder(org.apache.lucene.util.DocIdSetBuilder) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) FieldInfo(org.apache.lucene.index.FieldInfo)

Example 3 with ConstantScoreWeight

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

the class SortedNumericDocValuesRangeQuery 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 {
            SortedNumericDocValues values = getValues(context.reader(), field);
            if (values == null) {
                return null;
            }
            final NumericDocValues singleton = DocValues.unwrapSingleton(values);
            final TwoPhaseIterator iterator;
            if (singleton != null) {
                iterator = new TwoPhaseIterator(singleton) {

                    @Override
                    public boolean matches() throws IOException {
                        final long value = singleton.longValue();
                        return value >= lowerValue && value <= upperValue;
                    }

                    @Override
                    public float matchCost() {
                        // 2 comparisons
                        return 2;
                    }
                };
            } else {
                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();
                            if (value < lowerValue) {
                                continue;
                            }
                            // Values are sorted, so the first value that is >= lowerValue is our best candidate
                            return value <= upperValue;
                        }
                        // all values were < lowerValue
                        return false;
                    }

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

Example 4 with ConstantScoreWeight

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

the class FunctionMatchQuery 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 {
            DoubleValues values = source.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() {
                    // TODO maybe DoubleValuesSource should have a matchCost?
                    return 100;
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhase);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 5 with ConstantScoreWeight

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

the class LatLonDocValuesBoxQuery 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 {
            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);
                        if (lat < minLatitude || lat > maxLatitude) {
                            // not within latitude range
                            continue;
                        }
                        final int lon = (int) (value & 0xFFFFFFFF);
                        if (crossesDateline) {
                            if (lon > maxLongitude && lon < minLongitude) {
                                // not within longitude range
                                continue;
                            }
                        } else {
                            if (lon < minLongitude || lon > maxLongitude) {
                                // not within longitude range
                                continue;
                            }
                        }
                        return true;
                    }
                    return false;
                }

                @Override
                public float matchCost() {
                    // 5 comparisons
                    return 5;
                }
            };
            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

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)15 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)15 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)15 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)8 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)7 IOException (java.io.IOException)4 LeafReader (org.apache.lucene.index.LeafReader)4 PointValues (org.apache.lucene.index.PointValues)4 DocIdSetBuilder (org.apache.lucene.util.DocIdSetBuilder)4 FieldInfo (org.apache.lucene.index.FieldInfo)3 IntersectVisitor (org.apache.lucene.index.PointValues.IntersectVisitor)3 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)3 Map (java.util.Map)2 Rectangle (org.apache.lucene.geo.Rectangle)2 Relation (org.apache.lucene.index.PointValues.Relation)2 FunctionValues (org.apache.lucene.queries.function.FunctionValues)2 DocIdSet (org.apache.lucene.search.DocIdSet)2 Weight (org.apache.lucene.search.Weight)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1