private Spans buildConjunctionSpans() throws IOException { if (requiredSpans.size() == 1) { return requiredSpans.get(0); } else { if (inOrder) { return new NearSpansOrdered(requiredSpans, slop); } else { return new NearSpansUnordered(requiredSpans, slop); } } }
private boolean isMatching() { return (this.getSlop() <= allowedSlop); }
@Override public boolean nextPosition() throws IOException { if (firstTime) { if (!this.initialiseSpansPosition()) { matchStart = matchEnd = PositionsIterator.NO_MORE_POS; return false; } firstTime = false; } else { if (!this.advanceMinToNextPosition()) { matchStart = matchEnd = PositionsIterator.NO_MORE_POS; return false; } } boolean isMatching; do { // while it is not matching, advance the min to the next position, and try again if (!(isMatching = isMatching())) { if (!this.advanceMinToNextPosition()) { matchStart = matchEnd = PositionsIterator.NO_MORE_POS; return false; } } } while (!isMatching); matchStart = queue.top().start(); matchEnd = max.end(); return isMatching; }
/** * Initialise the spans by advancing them to their first position, compute the initial total length, and find the max. */ private boolean initialiseSpansPosition() throws IOException { // reset totalLength to 0 totalLength = 0; // reset max max = null; // Initialise the spans, compute the initial total length, and find the max for (Spans spans : subSpans) { if (!spans.nextPosition()) { return false; } // increment total length totalLength += spans.end() - spans.start(); // update max reference this.updateMax(spans); } // rebuild the queue this.rebuildQueue(); return true; }
/** * Advance the min to its next position, update the total length and the max. */ private boolean advanceMinToNextPosition() throws IOException { // retrieve the min Spans min = queue.top(); // cache length before advancing to the next position int oldLength = min.end() - min.start(); // we advance the min to its next position if (!min.nextPosition()) { return false; } // update total length this.updateTotalLength(oldLength, min.end() - min.start()); // update max reference this.updateMax(min); // update queue queue.updateTop(); return true; }
@Override public Scorer scorer(final AtomicReaderContext context, final Bits acceptDocs) throws IOException { final List<Spans> spans = new ArrayList<Spans>(); for (final Weight w : weights) { final Scorer scorer = w.scorer(context, acceptDocs); if (scorer == null) { return null; } if (!(scorer instanceof SpanScorer)) { throw new IllegalArgumentException("SpanScorer expected"); } spans.add(((SpanScorer) scorer).getSpans()); } int slop = NearSpanQuery.this.getSlop(); NearSpans nearSpans = inOrder ? new NearSpansOrdered(spans, slop) : new NearSpansUnordered(spans, slop); return new SpanScorer(this, nearSpans); }