public void testNearSpansNextThenSkipPast() throws Exception { SpanNearQuery q = makeQuery(); Spans span = q.getSpans(searcher.getIndexReader()); assertEquals(true, span.next()); assertEquals(s(0, 0, 3), s(span)); assertEquals(false, span.skipTo(2)); }
@Override public Collection<byte[]> getPayload() throws IOException { ArrayList<byte[]> result = null; if (spans.isPayloadAvailable()) { result = new ArrayList<byte[]>(spans.getPayload()); } return result; // TODO: any way to avoid the new construction? }
@Override public int advance(int target) throws IOException { if (!more) { return doc = NO_MORE_DOCS; } if (spans.doc() < target) { // setFreqCurrentDoc() leaves spans.doc() ahead more = spans.skipTo(target); } if (!setFreqCurrentDoc()) { doc = NO_MORE_DOCS; } return doc; }
protected boolean setFreqCurrentDoc() throws IOException { if (!more) { return false; } doc = spans.doc(); freq = 0.0f; do { int matchLength = spans.end() - spans.start(); freq += similarity.sloppyFreq(matchLength); more = spans.next(); } while (more && (doc == spans.doc())); return true; }
/** * Check whether two Spans in the same document are ordered. * * @param spans1 * @param spans2 * @return true iff spans1 starts before spans2 or the spans start at the same position, and * spans1 ends before spans2. */ static final boolean docSpansOrdered(Spans spans1, Spans spans2) { assert spans1.doc() == spans2.doc() : "doc1 " + spans1.doc() + " != doc2 " + spans2.doc(); int start1 = spans1.start(); int start2 = spans2.start(); /* Do not call docSpansOrdered(int,int,int,int) to avoid invoking .end() : */ return (start1 == start2) ? (spans1.end() < spans2.end()) : (start1 < start2); }
protected boolean doNext() throws IOException { for (; ; ) { switch (acceptPosition(this)) { case YES: return true; case NO: if (!spans.next()) return false; break; case NO_AND_ADVANCE: if (!spans.skipTo(spans.doc() + 1)) return false; break; } } }
/** * Check whether two Spans in the same document are ordered with possible overlap. * * @return true iff spans1 starts before spans2 or the spans start at the same position, and * spans1 ends before spans2. */ static boolean positionsOrdered(Spans spans1, Spans spans2) { assert spans1.docID() == spans2.docID() : "doc1 " + spans1.docID() + " != doc2 " + spans2.docID(); int start1 = spans1.startPosition(); int start2 = spans2.startPosition(); return (start1 == start2) ? (spans1.endPosition() < spans2.endPosition()) : (start1 < start2); }
/** * Collect all Spans extracted from a Scorer using a SpanCollector * * @param scorer the scorer to extract Spans from * @param collector the SpanCollector * @param errorOnNoSpans if true, throw an error if no Spans can be extracted from the Scorer or * any of its children * @throws IOException on error */ public static void collect(Scorer scorer, SpanCollector collector, boolean errorOnNoSpans) throws IOException { List<Spans> allSpans = getSpans(scorer, errorOnNoSpans); int doc = scorer.docID(); for (Spans spans : allSpans) { int spanDoc = spans.docID(); // if the Scorer advances lazily, then not all of its subspans may be on // the correct document if (spanDoc == doc || (spanDoc < doc && spans.advance(doc) == doc)) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { spans.collect(collector); } } } }
private void adjustLength() { if (spanLength != -1) { totalSpanLength -= spanLength; // subtract old, possibly from a previous doc } assert in.startPosition() != NO_MORE_POSITIONS; spanLength = endPosition() - startPosition(); assert spanLength >= 0; totalSpanLength += spanLength; // add new }
@Override public int nextStartPosition() throws IOException { int res = in.nextStartPosition(); if (res != NO_MORE_POSITIONS) { adjustLength(); } adjustMax(); // also after last end position in current doc. return res; }
public boolean skipTo(int target) throws IOException { if (length != -1) // subtract old length totalLength -= length; boolean more = spans.skipTo(target); // skip if (more) { length = end() - start(); // compute new length totalLength += length; // add new length to total if (max == null || doc() > max.doc() || // maintain max (doc() == max.doc() && end() > max.end())) max = this; } return more; }
public boolean skipTo(int target) throws IOException { more = spans.skipTo(target); if (!more) return false; freq = 0.0f; doc = spans.doc(); while (more && spans.doc() == target) { freq += getSimilarity().sloppyFreq(spans.end() - spans.start()); more = spans.next(); } return more || freq != 0.0f; }
public boolean next() throws IOException { if (firstTime) { more = spans.next(); firstTime = false; } if (!more) return false; freq = 0.0f; doc = spans.doc(); while (more && doc == spans.doc()) { int matchLength = spans.end() - spans.start(); freq += getSimilarity().sloppyFreq(matchLength); more = spans.next(); } return more || freq != 0.0f; }
@Override public void collect(SpanCollector collector) throws IOException { in.collect(collector); }
public void testNearSpansSkipTo1() throws Exception { SpanNearQuery q = makeQuery(); Spans span = q.getSpans(searcher.getIndexReader()); assertEquals(true, span.skipTo(1)); assertEquals(s(1, 0, 4), s(span)); }
@Override public String toString() { return "NearSpansUnordered.SpansCell(" + in.toString() + ")"; }
@Override public long cost() { return in.cost(); }
@Override public int advance(int target) throws IOException { return in.advance(target); }
@Override public int nextDoc() throws IOException { return in.nextDoc(); }
@Override public int docID() { return in.docID(); }
public void testNearSpansSkipPast() throws Exception { SpanNearQuery q = makeQuery(); Spans span = q.getSpans(searcher.getIndexReader()); assertEquals(false, span.skipTo(2)); }
public int start() { return spans.start(); }
public String s(Spans span) { return s(span.doc(), span.start(), span.end()); }
public String toString() { return spans.toString() + "#" + index; }
@Override public int endPosition() { return in.endPosition(); }
@Override public int startPosition() { return in.startPosition(); }
public int end() { return spans.end(); }
@Override public TwoPhaseIterator asTwoPhaseIterator() { return in.asTwoPhaseIterator(); }
@Override public int width() { return in.width(); }
@Override public float positionsCost() { return in.positionsCost(); }