package org.apache.lucene.search.spans;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
public abstract class SpanPositionCheckQuery extends SpanQuery implements Cloneable {
protected SpanQuery match;
public SpanPositionCheckQuery(SpanQuery match) {
this.match = Objects.requireNonNull(match);
}
public SpanQuery getMatch() { return match; }
@Override
public String getField() { return match.getField(); }
protected abstract AcceptStatus acceptPosition(Spans spans) throws IOException;
@Override
public SpanWeight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
SpanWeight matchWeight = match.createWeight(searcher, scoreMode, boost);
return new SpanPositionCheckWeight(matchWeight, searcher, scoreMode.needsScores() ? getTermStates(matchWeight) : null, boost);
}
public class SpanPositionCheckWeight extends SpanWeight {
final SpanWeight matchWeight;
public SpanPositionCheckWeight(SpanWeight matchWeight, IndexSearcher searcher, Map<Term, TermStates> terms, float boost) throws IOException {
super(SpanPositionCheckQuery.this, searcher, terms, boost);
this.matchWeight = matchWeight;
}
@Override
public void (Set<Term> terms) {
matchWeight.extractTerms(terms);
}
@Override
public boolean isCacheable(LeafReaderContext ctx) {
return matchWeight.isCacheable(ctx);
}
@Override
public void (Map<Term, TermStates> contexts) {
matchWeight.extractTermStates(contexts);
}
@Override
public Spans getSpans(final LeafReaderContext context, Postings requiredPostings) throws IOException {
Spans matchSpans = matchWeight.getSpans(context, requiredPostings);
return (matchSpans == null) ? null : new FilterSpans(matchSpans) {
@Override
protected AcceptStatus accept(Spans candidate) throws IOException {
return acceptPosition(candidate);
}
};
}
}
@Override
public Query rewrite(IndexReader reader) throws IOException {
SpanQuery rewritten = (SpanQuery) match.rewrite(reader);
if (rewritten != match) {
try {
SpanPositionCheckQuery clone = (SpanPositionCheckQuery) this.clone();
clone.match = rewritten;
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
return super.rewrite(reader);
}
@Override
public void visit(QueryVisitor visitor) {
if (visitor.acceptField(getField())) {
match.visit(visitor.getSubVisitor(BooleanClause.Occur.MUST, this));
}
}
@Override
public boolean equals(Object other) {
return sameClassAs(other) &&
match.equals(((SpanPositionCheckQuery) other).match);
}
@Override
public int hashCode() {
return classHash() ^ match.hashCode();
}
}