package org.apache.lucene.search.join;
import java.io.IOException;
import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.BitSet;
public class ParentChildrenBlockJoinQuery extends Query {
private final BitSetProducer parentFilter;
private final Query childQuery;
private final int parentDocId;
public ParentChildrenBlockJoinQuery(BitSetProducer parentFilter, Query childQuery, int parentDocId) {
this.parentFilter = parentFilter;
this.childQuery = childQuery;
this.parentDocId = parentDocId;
}
@Override
public boolean equals(Object obj) {
if (sameClassAs(obj) == false) {
return false;
}
ParentChildrenBlockJoinQuery other = (ParentChildrenBlockJoinQuery) obj;
return parentFilter.equals(other.parentFilter)
&& childQuery.equals(other.childQuery)
&& parentDocId == other.parentDocId;
}
@Override
public int hashCode() {
int hash = classHash();
hash = 31 * hash + parentFilter.hashCode();
hash = 31 * hash + childQuery.hashCode();
hash = 31 * hash + parentDocId;
return hash;
}
@Override
public String toString(String field) {
return "ParentChildrenBlockJoinQuery (" + childQuery + ")";
}
@Override
public void visit(QueryVisitor visitor) {
visitor.visitLeaf(this);
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
final Query childRewrite = childQuery.rewrite(reader);
if (childRewrite != childQuery) {
return new ParentChildrenBlockJoinQuery(parentFilter, childRewrite, parentDocId);
} else {
return super.rewrite(reader);
}
}
@Override
public Weight createWeight(IndexSearcher searcher, org.apache.lucene.search.ScoreMode scoreMode, float boost) throws IOException {
final Weight childWeight = childQuery.createWeight(searcher, scoreMode, boost);
final int readerIndex = ReaderUtil.subIndex(parentDocId, searcher.getIndexReader().leaves());
return new Weight(this) {
@Override
public void (Set<Term> terms) {
childWeight.extractTerms(terms);
}
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
return Explanation.noMatch("Not implemented, use ToParentBlockJoinQuery explain why a document matched");
}
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
if (context.ord != readerIndex) {
return null;
}
final int localParentDocId = parentDocId - context.docBase;
if (localParentDocId == 0) {
return null;
}
final BitSet parents = parentFilter.getBitSet(context);
final int firstChildDocId = parents.prevSetBit(localParentDocId - 1) + 1;
if (firstChildDocId == localParentDocId) {
return null;
}
final Scorer childrenScorer = childWeight.scorer(context);
if (childrenScorer == null) {
return null;
}
DocIdSetIterator childrenIterator = childrenScorer.iterator();
final DocIdSetIterator it = 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 {
target = Math.max(firstChildDocId, target);
if (target >= localParentDocId) {
return doc = NO_MORE_DOCS;
} else {
int advanced = childrenIterator.advance(target);
if (advanced >= localParentDocId) {
return doc = NO_MORE_DOCS;
} else {
return doc = advanced;
}
}
}
@Override
public long cost() {
return Math.min(childrenIterator.cost(), localParentDocId - firstChildDocId);
}
};
return new Scorer(this) {
@Override
public int docID() {
return it.docID();
}
@Override
public float score() throws IOException {
return childrenScorer.score();
}
@Override
public float getMaxScore(int upTo) throws IOException {
return Float.POSITIVE_INFINITY;
}
@Override
public DocIdSetIterator iterator() {
return it;
}
};
}
@Override
public boolean isCacheable(LeafReaderContext ctx) {
return false;
}
};
}
}