Search in sources :

Example 21 with ConstantScoreScorer

use of org.apache.lucene.search.ConstantScoreScorer in project crate by crate.

the class ShardSplittingQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) {
    return new ConstantScoreWeight(this, boost) {

        @Override
        public String toString() {
            return "weight(delete docs query)";
        }

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            LeafReader leafReader = context.reader();
            FixedBitSet bitSet = new FixedBitSet(leafReader.maxDoc());
            Terms terms = leafReader.terms(RoutingFieldMapper.NAME);
            Predicate<BytesRef> includeInShard = ref -> {
                int targetShardId = OperationRouting.generateShardId(indexMetadata, Uid.decodeId(ref.bytes, ref.offset, ref.length), null);
                return shardId == targetShardId;
            };
            if (terms == null) {
                // by ID and parent and nested all have the same id.
                assert indexMetadata.isRoutingPartitionedIndex() == false;
                findSplitDocs(IdFieldMapper.NAME, includeInShard, leafReader, bitSet::set);
            } else {
                if (indexMetadata.isRoutingPartitionedIndex()) {
                    // this is the heaviest invariant. Here we have to visit all docs stored fields do extract _id and _routing
                    // this this index is routing partitioned.
                    Visitor visitor = new Visitor(leafReader);
                    TwoPhaseIterator twoPhaseIterator = new RoutingPartitionedDocIdSetIterator(visitor);
                    return new ConstantScoreScorer(this, score(), scoreMode, twoPhaseIterator);
                } else {
                    // in the _routing case we first go and find all docs that have a routing value and mark the ones we have to delete
                    findSplitDocs(RoutingFieldMapper.NAME, ref -> {
                        int targetShardId = OperationRouting.generateShardId(indexMetadata, null, ref.utf8ToString());
                        return shardId == targetShardId;
                    }, leafReader, bitSet::set);
                    // with a routing value from the next iteration an delete / select based on the ID.
                    if (terms.getDocCount() != leafReader.maxDoc()) {
                        // this is a special case where some of the docs have no routing values this sucks but it's possible today
                        FixedBitSet hasRoutingValue = new FixedBitSet(leafReader.maxDoc());
                        findSplitDocs(RoutingFieldMapper.NAME, ref -> false, leafReader, hasRoutingValue::set);
                        IntConsumer bitSetConsumer = bitSet::set;
                        findSplitDocs(IdFieldMapper.NAME, includeInShard, leafReader, docId -> {
                            if (hasRoutingValue.get(docId) == false) {
                                bitSetConsumer.accept(docId);
                            }
                        });
                    }
                }
            }
            return new ConstantScoreScorer(this, score(), scoreMode, new BitSetIterator(bitSet, bitSet.length()));
        }

        @Override
        public boolean isCacheable(LeafReaderContext ctx) {
            // anyway.
            return false;
        }
    };
}
Also used : Query(org.apache.lucene.search.Query) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) BitSetIterator(org.apache.lucene.util.BitSetIterator) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Weight(org.apache.lucene.search.Weight) IntConsumer(java.util.function.IntConsumer) StoredFieldVisitor(org.apache.lucene.index.StoredFieldVisitor) FixedBitSet(org.apache.lucene.util.FixedBitSet) RoutingFieldMapper(org.elasticsearch.index.mapper.RoutingFieldMapper) TermsEnum(org.apache.lucene.index.TermsEnum) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) Scorer(org.apache.lucene.search.Scorer) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) Uid(org.elasticsearch.index.mapper.Uid) BytesRef(org.apache.lucene.util.BytesRef) IdFieldMapper(org.elasticsearch.index.mapper.IdFieldMapper) Predicate(java.util.function.Predicate) IOException(java.io.IOException) ScoreMode(org.apache.lucene.search.ScoreMode) LeafReader(org.apache.lucene.index.LeafReader) FieldInfo(org.apache.lucene.index.FieldInfo) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) IndexSearcher(org.apache.lucene.search.IndexSearcher) BitSetIterator(org.apache.lucene.util.BitSetIterator) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) LeafReader(org.apache.lucene.index.LeafReader) StoredFieldVisitor(org.apache.lucene.index.StoredFieldVisitor) Terms(org.apache.lucene.index.Terms) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight) IntConsumer(java.util.function.IntConsumer) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) FixedBitSet(org.apache.lucene.util.FixedBitSet) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)21 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)21 ConstantScoreWeight (org.apache.lucene.search.ConstantScoreWeight)16 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)11 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)11 Scorer (org.apache.lucene.search.Scorer)8 Weight (org.apache.lucene.search.Weight)8 IOException (java.io.IOException)5 LeafReader (org.apache.lucene.index.LeafReader)5 Set (java.util.Set)4 FieldInfo (org.apache.lucene.index.FieldInfo)4 PointValues (org.apache.lucene.index.PointValues)4 DocIdSetBuilder (org.apache.lucene.util.DocIdSetBuilder)4 IntersectVisitor (org.apache.lucene.index.PointValues.IntersectVisitor)3 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)3 DocIdSet (org.apache.lucene.search.DocIdSet)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Rectangle (org.apache.lucene.geo.Rectangle)2 IndexReader (org.apache.lucene.index.IndexReader)2