use of org.apache.lucene.search.ScoreMode 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;
}
};
}
use of org.apache.lucene.search.ScoreMode in project OpenGrok by OpenGrok.
the class SuggesterSearcher method getComplexQueryData.
private ComplexQueryData getComplexQueryData(final Query query, final LeafReaderContext leafReaderContext) {
ComplexQueryData data = new ComplexQueryData();
if (query == null || query instanceof SuggesterQuery) {
data.documentIds = new BitIntsHolder(0);
return data;
}
BitIntsHolder documentIds = new BitIntsHolder();
try {
search(query, new Collector() {
@Override
public LeafCollector getLeafCollector(final LeafReaderContext context) {
return new LeafCollector() {
final int docBase = context.docBase;
@Override
public void setScorer(final Scorable scorer) {
if (leafReaderContext == context) {
if (scorer instanceof PhraseScorer) {
data.scorer = (PhraseScorer) scorer;
} else {
try {
// in #setScorer but no better way was found
for (Scorer.ChildScorable childScorer : scorer.getChildren()) {
if (childScorer.child instanceof PhraseScorer) {
data.scorer = (PhraseScorer) childScorer.child;
}
}
} catch (Exception e) {
// ignore
}
}
}
}
@Override
public void collect(int doc) {
if (leafReaderContext == context) {
documentIds.set(docBase + doc);
}
}
};
}
@Override
public ScoreMode scoreMode() {
return ScoreMode.COMPLETE_NO_SCORES;
}
});
} catch (IOException e) {
if (Thread.currentThread().isInterrupted()) {
interrupted = true;
return null;
} else {
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
}
data.documentIds = documentIds;
return data;
}
Aggregations