Search in sources :

Example 6 with DocIdSetIterator

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

the class GeoDistanceSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final boolean indexCreatedBeforeV2_0 = context.indexVersionCreated().before(Version.V_2_0_0);
    // validation was not available prior to 2.x, so to support bwc percolation queries we only ignore_malformed
    // on 2.x created indexes
    GeoPoint[] localPoints = points.toArray(new GeoPoint[points.size()]);
    if (!indexCreatedBeforeV2_0 && !GeoValidationMethod.isIgnoreMalformed(validation)) {
        for (GeoPoint point : localPoints) {
            if (GeoUtils.isValidLatitude(point.lat()) == false) {
                throw new ElasticsearchParseException("illegal latitude value [{}] for [GeoDistanceSort] for field [{}].", point.lat(), fieldName);
            }
            if (GeoUtils.isValidLongitude(point.lon()) == false) {
                throw new ElasticsearchParseException("illegal longitude value [{}] for [GeoDistanceSort] for field [{}].", point.lon(), fieldName);
            }
        }
    }
    if (GeoValidationMethod.isCoerce(validation)) {
        for (GeoPoint point : localPoints) {
            GeoUtils.normalizePoint(point, true, true);
        }
    }
    boolean reverse = (order == SortOrder.DESC);
    final MultiValueMode finalSortMode;
    if (sortMode == null) {
        finalSortMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    } else {
        finalSortMode = MultiValueMode.fromString(sortMode.toString());
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        throw new IllegalArgumentException("failed to find mapper for [" + fieldName + "] for geo distance based sort");
    }
    final IndexGeoPointFieldData geoIndexFieldData = context.getForField(fieldType);
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    if (// only works with 5.x geo_point
    geoIndexFieldData.getClass() == LatLonPointDVIndexFieldData.class && nested == null && // LatLonDocValuesField internally picks the closest point
    finalSortMode == MultiValueMode.MIN && unit == DistanceUnit.METERS && reverse == false && localPoints.length == 1) {
        return new SortFieldAndFormat(LatLonDocValuesField.newDistanceSort(fieldName, localPoints[0].lat(), localPoints[0].lon()), DocValueFormat.RAW);
    }
    IndexFieldData.XFieldComparatorSource geoDistanceComparatorSource = new IndexFieldData.XFieldComparatorSource() {

        @Override
        public SortField.Type reducedType() {
            return SortField.Type.DOUBLE;
        }

        @Override
        public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
            return new FieldComparator.DoubleComparator(numHits, null, null) {

                @Override
                protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
                    final MultiGeoPointValues geoPointValues = geoIndexFieldData.load(context).getGeoPointValues();
                    final SortedNumericDoubleValues distanceValues = GeoUtils.distanceValues(geoDistance, unit, geoPointValues, localPoints);
                    final NumericDoubleValues selectedValues;
                    if (nested == null) {
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY);
                    } else {
                        final BitSet rootDocs = nested.rootDocs(context);
                        final DocIdSetIterator innerDocs = nested.innerDocs(context);
                        selectedValues = finalSortMode.select(distanceValues, Double.POSITIVE_INFINITY, rootDocs, innerDocs, context.reader().maxDoc());
                    }
                    return selectedValues.getRawDoubleValues();
                }
            };
        }
    };
    return new SortFieldAndFormat(new SortField(fieldName, geoDistanceComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : IndexGeoPointFieldData(org.elasticsearch.index.fielddata.IndexGeoPointFieldData) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BitSet(org.apache.lucene.util.BitSet) SortField(org.apache.lucene.search.SortField) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiValueMode(org.elasticsearch.search.MultiValueMode) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) LatLonPointDVIndexFieldData(org.elasticsearch.index.fielddata.plain.AbstractLatLonPointDVIndexFieldData.LatLonPointDVIndexFieldData) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) MultiGeoPointValues(org.elasticsearch.index.fielddata.MultiGeoPointValues) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 7 with DocIdSetIterator

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

the class IndexFieldTerm method setReader.

// when the reader changes, we have to get the posting list for this term
// and reader
private void setReader(LeafReader reader) {
    try {
        postings = getPostings(convertToLuceneFlags(flags), reader);
        if (postings == null) {
            // no term or field for this segment, fake out the postings...
            final DocIdSetIterator empty = DocIdSetIterator.empty();
            postings = new PostingsEnum() {

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

                @Override
                public int nextDoc() throws IOException {
                    return empty.nextDoc();
                }

                @Override
                public int advance(int target) throws IOException {
                    return empty.advance(target);
                }

                @Override
                public long cost() {
                    return empty.cost();
                }

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

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

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

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

                @Override
                public BytesRef getPayload() throws IOException {
                    return null;
                }
            };
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Unable to get postings for field " + fieldName + " and term " + term, e);
    }
}
Also used : IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) PostingsEnum(org.apache.lucene.index.PostingsEnum) FilterPostingsEnum(org.apache.lucene.index.FilterLeafReader.FilterPostingsEnum) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) BytesRef(org.apache.lucene.util.BytesRef)

Example 8 with DocIdSetIterator

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

the class BestBucketsDeferringCollector method prepareSelectedBuckets.

/**
     * Replay the wrapped collector, but only on a selection of buckets.
     */
@Override
public void prepareSelectedBuckets(long... selectedBuckets) throws IOException {
    if (!finished) {
        throw new IllegalStateException("Cannot replay yet, collection is not finished: postCollect() has not been called");
    }
    if (this.selectedBuckets != null) {
        throw new IllegalStateException("Already been replayed");
    }
    final LongHash hash = new LongHash(selectedBuckets.length, BigArrays.NON_RECYCLING_INSTANCE);
    for (long bucket : selectedBuckets) {
        hash.add(bucket);
    }
    this.selectedBuckets = hash;
    boolean needsScores = collector.needsScores();
    Weight weight = null;
    if (needsScores) {
        weight = searchContext.searcher().createNormalizedWeight(searchContext.query(), true);
    }
    for (Entry entry : entries) {
        final LeafBucketCollector leafCollector = collector.getLeafCollector(entry.context);
        DocIdSetIterator docIt = null;
        if (needsScores && entry.docDeltas.size() > 0) {
            Scorer scorer = weight.scorer(entry.context);
            // We don't need to check if the scorer is null
            // since we are sure that there are documents to replay (entry.docDeltas it not empty).
            docIt = scorer.iterator();
            leafCollector.setScorer(scorer);
        }
        final PackedLongValues.Iterator docDeltaIterator = entry.docDeltas.iterator();
        final PackedLongValues.Iterator buckets = entry.buckets.iterator();
        int doc = 0;
        for (long i = 0, end = entry.docDeltas.size(); i < end; ++i) {
            doc += docDeltaIterator.next();
            final long bucket = buckets.next();
            final long rebasedBucket = hash.find(bucket);
            if (rebasedBucket != -1) {
                if (needsScores) {
                    if (docIt.docID() < doc) {
                        docIt.advance(doc);
                    }
                    // aggregations should only be replayed on matching documents
                    assert docIt.docID() == doc;
                }
                leafCollector.collect(doc, rebasedBucket);
            }
        }
    }
    collector.postCollection();
}
Also used : PackedLongValues(org.apache.lucene.util.packed.PackedLongValues) LongHash(org.elasticsearch.common.util.LongHash) LeafBucketCollector(org.elasticsearch.search.aggregations.LeafBucketCollector) Scorer(org.apache.lucene.search.Scorer) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) Weight(org.apache.lucene.search.Weight)

Example 9 with DocIdSetIterator

use of org.apache.lucene.search.DocIdSetIterator 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 10 with DocIdSetIterator

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

the class BufferedUpdatesStream method applyQueryDeletes.

// Delete by query
private static long applyQueryDeletes(Iterable<QueryAndLimit> queriesIter, SegmentState segState) throws IOException {
    long delCount = 0;
    final LeafReaderContext readerContext = segState.reader.getContext();
    for (QueryAndLimit ent : queriesIter) {
        Query query = ent.query;
        int limit = ent.limit;
        final IndexSearcher searcher = new IndexSearcher(readerContext.reader());
        searcher.setQueryCache(null);
        final Weight weight = searcher.createNormalizedWeight(query, false);
        final Scorer scorer = weight.scorer(readerContext);
        if (scorer != null) {
            final DocIdSetIterator it = scorer.iterator();
            final Bits liveDocs = readerContext.reader().getLiveDocs();
            while (true) {
                int doc = it.nextDoc();
                if (doc >= limit) {
                    break;
                }
                if (liveDocs != null && liveDocs.get(doc) == false) {
                    continue;
                }
                if (!segState.any) {
                    segState.rld.initWritableLiveDocs();
                    segState.any = true;
                }
                if (segState.rld.delete(doc)) {
                    delCount++;
                }
            }
        }
    }
    return delCount;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) Scorer(org.apache.lucene.search.Scorer) Bits(org.apache.lucene.util.Bits) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) Weight(org.apache.lucene.search.Weight)

Aggregations

DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)68 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)28 Scorer (org.apache.lucene.search.Scorer)15 DocIdSet (org.apache.lucene.search.DocIdSet)14 Weight (org.apache.lucene.search.Weight)12 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)10 BitSet (org.apache.lucene.util.BitSet)10 Bits (org.apache.lucene.util.Bits)10 BytesRef (org.apache.lucene.util.BytesRef)10 IOException (java.io.IOException)9 MatchingDocs (org.apache.lucene.facet.FacetsCollector.MatchingDocs)8 IndexSearcher (org.apache.lucene.search.IndexSearcher)8 SortedDocValues (org.apache.lucene.index.SortedDocValues)7 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)7 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)7 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)6 Query (org.apache.lucene.search.Query)6 Document (org.apache.lucene.document.Document)5 IndexReader (org.apache.lucene.index.IndexReader)5 LeafReader (org.apache.lucene.index.LeafReader)5