You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2015/05/22 04:15:36 UTC

svn commit: r1680978 [1/2] - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/core/ lucene/core/src/java/org/apache/lucene/search/payloads/ lucene/core/src/java/org/apache/lucene/search/spans/ lucene/core/src/test/org/apache/lucene/search/ lucene/c...

Author: rmuir
Date: Fri May 22 02:15:34 2015
New Revision: 1680978

URL: http://svn.apache.org/r1680978
Log:
LUCENE-6371, LUCENE-6466: back out from 5.2, see https://issues.apache.org/jira/browse/LUCENE-6494

Added:
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansPayloadOrdered.java
      - copied unchanged from r1680207, lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansPayloadOrdered.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
      - copied unchanged from r1680207, lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearPayloadCheckQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
      - copied unchanged from r1680207, lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPayloadCheckQuery.java
Removed:
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanCollector.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/SpanNearPayloadCheckQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/SpanPayloadCheckQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/BufferedSpanCollector.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanCollector.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanCollectorFactory.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanSimilarity.java
    lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/spans/AssertingSpanWeight.java
Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/lucene/core/   (props changed)
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/ContainSpans.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FilterSpans.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainingQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanScorer.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanTermQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanWithinQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/Spans.java
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/TermSpans.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/TestPositionIncrement.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadBasics.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadNearQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadSpans.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/spans/JustCompileSearchSpans.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/spans/MultiSpansWrapper.java
    lucene/dev/branches/branch_5x/lucene/core/src/test/org/apache/lucene/search/spans/TestFieldMaskingSpanQuery.java
    lucene/dev/branches/branch_5x/lucene/highlighter/   (props changed)
    lucene/dev/branches/branch_5x/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
    lucene/dev/branches/branch_5x/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
    lucene/dev/branches/branch_5x/lucene/test-framework/   (props changed)
    lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/spans/AssertingSpanQuery.java
    lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/spans/AssertingSpans.java
    lucene/dev/branches/branch_5x/solr/   (props changed)
    lucene/dev/branches/branch_5x/solr/core/   (props changed)
    lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/highlight/HighlighterTest.java

Modified: lucene/dev/branches/branch_5x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/CHANGES.txt?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/lucene/CHANGES.txt Fri May 22 02:15:34 2015
@@ -173,19 +173,9 @@ API Changes
 * LUCENE-6445: Two new methods in Highlighter's TokenSources; the existing
   methods are now marked deprecated. (David Smiley)
 
-* LUCENE-6371, LUCENE-6490: Payload collection from Spans is moved to a more generic
-  SpanCollector framework.  Spans no longer implements .hasPayload() and
-  .getPayload() methods, and instead exposes a collect() method that allows
-  the collection of arbitrary postings information. SpanPayloadCheckQuery and
-  SpanPayloadNearCheckQuery have moved from the .spans package to the .payloads
-  package. (Alan Woodward, David Smiley, Paul Elschot, Robert Muir)
-
 * LUCENE-6484: Removed EliasFanoDocIdSet, which was unused.
   (Paul Elschot via Adrien Grand)
 
-* LUCENE-6466: Moved SpanQuery.getSpans() and .extractTerms() to SpanWeight
-  (Alan Woodward)
-
 Other
 
 * LUCENE-6413: Test runner should report the number of suites completed/ 

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java Fri May 22 02:15:34 2015
@@ -17,6 +17,11 @@ package org.apache.lucene.search.payload
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Objects;
+
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
@@ -24,24 +29,17 @@ import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.similarities.DefaultSimilarity;
 import org.apache.lucene.search.similarities.Similarity;
 import org.apache.lucene.search.similarities.Similarity.SimScorer;
-import org.apache.lucene.search.spans.SpanCollectorFactory;
+import org.apache.lucene.search.spans.NearSpansOrdered;
+import org.apache.lucene.search.spans.NearSpansUnordered;
 import org.apache.lucene.search.spans.SpanNearQuery;
 import org.apache.lucene.search.spans.SpanQuery;
 import org.apache.lucene.search.spans.SpanScorer;
-import org.apache.lucene.search.spans.SpanSimilarity;
 import org.apache.lucene.search.spans.SpanWeight;
 import org.apache.lucene.search.spans.Spans;
 import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.BytesRef;
 import org.apache.lucene.util.ToStringUtils;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Objects;
-
 /**
  * This class is very similar to
  * {@link org.apache.lucene.search.spans.SpanNearQuery} except that it factors
@@ -57,7 +55,6 @@ import java.util.Objects;
  * @see org.apache.lucene.search.similarities.Similarity.SimScorer#computePayloadFactor(int, int, int, BytesRef)
  */
 public class PayloadNearQuery extends SpanNearQuery {
-
   protected String fieldName;
   protected PayloadFunction function;
 
@@ -73,13 +70,8 @@ public class PayloadNearQuery extends Sp
   }
 
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    List<SpanWeight> subWeights = new ArrayList<>();
-    for (SpanQuery q : clauses) {
-      subWeights.add(q.createWeight(searcher, false, PayloadSpanCollector.FACTORY));
-    }
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, subWeights);
-    return new PayloadNearSpanWeight(subWeights, similarity);
+  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
+    return new PayloadNearSpanWeight(this, searcher);
   }
 
   @Override
@@ -136,20 +128,18 @@ public class PayloadNearQuery extends Sp
           && function.equals(other.function);
   }
 
-  public class PayloadNearSpanWeight extends SpanNearWeight {
-
-    public PayloadNearSpanWeight(List<SpanWeight> subWeights, SpanSimilarity similarity)
+  public class PayloadNearSpanWeight extends SpanWeight {
+    public PayloadNearSpanWeight(SpanQuery query, IndexSearcher searcher)
         throws IOException {
-      super(subWeights, similarity, PayloadSpanCollector.FACTORY);
+      super(query, searcher);
     }
 
     @Override
     public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
-      PayloadSpanCollector collector = (PayloadSpanCollector) collectorFactory.newCollector();
-      Spans spans = super.getSpans(context, acceptDocs, collector);
+      Spans spans = query.getSpans(context, acceptDocs, termContexts);
       return (spans == null)
               ? null
-              : new PayloadNearSpanScorer(spans, this, collector, similarity.simScorer(context));
+              : new PayloadNearSpanScorer(spans, this, similarity, similarity.simScorer(stats, context));
     }
     
     @Override
@@ -160,7 +150,7 @@ public class PayloadNearQuery extends Sp
         if (newDoc == doc) {
           float freq = scorer.freq();
           Explanation freqExplanation = Explanation.match(freq, "phraseFreq=" + freq);
-          SimScorer docScorer = similarity.simScorer(context);
+          SimScorer docScorer = similarity.simScorer(stats, context);
           Explanation scoreExplanation = docScorer.explain(doc, freqExplanation);
           Explanation expl = Explanation.match(
               scoreExplanation.getValue(),
@@ -185,13 +175,30 @@ public class PayloadNearQuery extends Sp
     Spans spans;
     protected float payloadScore;
     private int payloadsSeen;
-    private final PayloadSpanCollector collector;
 
-    protected PayloadNearSpanScorer(Spans spans, SpanWeight weight, PayloadSpanCollector collector,
-                                    Similarity.SimScorer docScorer) throws IOException {
+    protected PayloadNearSpanScorer(Spans spans, SpanWeight weight,
+        Similarity similarity, Similarity.SimScorer docScorer) throws IOException {
       super(spans, weight, docScorer);
       this.spans = spans;
-      this.collector = collector;
+    }
+
+    // Get the payloads associated with all underlying subspans
+    public void getPayloads(Spans[] subSpans) throws IOException {
+      for (int i = 0; i < subSpans.length; i++) {
+        if (subSpans[i] instanceof NearSpansOrdered) {
+          if (((NearSpansOrdered) subSpans[i]).isPayloadAvailable()) {
+            processPayloads(((NearSpansOrdered) subSpans[i]).getPayload(),
+                subSpans[i].startPosition(), subSpans[i].endPosition());
+          }
+          getPayloads(((NearSpansOrdered) subSpans[i]).getSubSpans());
+        } else if (subSpans[i] instanceof NearSpansUnordered) {
+          if (((NearSpansUnordered) subSpans[i]).isPayloadAvailable()) {
+            processPayloads(((NearSpansUnordered) subSpans[i]).getPayload(),
+                subSpans[i].startPosition(), subSpans[i].endPosition());
+          }
+          getPayloads(((NearSpansUnordered) subSpans[i]).getSubSpans());
+        }
+      }
     }
 
     // TODO change the whole spans api to use bytesRef, or nuke spans
@@ -230,9 +237,9 @@ public class PayloadNearQuery extends Sp
       do {
         int matchLength = spans.endPosition() - startPos;
         freq += docScorer.computeSlopFactor(matchLength);
-        collector.reset();
-        spans.collect(collector);
-        processPayloads(collector.getPayloads(), startPos, spans.endPosition());
+        Spans[] spansArr = new Spans[1];
+        spansArr[0] = spans;
+        getPayloads(spansArr);            
         startPos = spans.nextStartPosition();
       } while (startPos != Spans.NO_MORE_POSITIONS);
     }

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadSpanUtil.java Fri May 22 02:15:34 2015
@@ -17,10 +17,20 @@ package org.apache.lucene.search.payload
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.IndexReaderContext;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.DisjunctionMaxQuery;
@@ -34,15 +44,8 @@ import org.apache.lucene.search.spans.Sp
 import org.apache.lucene.search.spans.SpanOrQuery;
 import org.apache.lucene.search.spans.SpanQuery;
 import org.apache.lucene.search.spans.SpanTermQuery;
-import org.apache.lucene.search.spans.SpanWeight;
 import org.apache.lucene.search.spans.Spans;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
 /**
  * Experimental class to get set of payloads for most standard Lucene queries.
  * Operates like Highlighter - IndexReader should only contain doc of interest,
@@ -176,21 +179,25 @@ public class PayloadSpanUtil {
 
   private void getPayloads(Collection<byte []> payloads, SpanQuery query)
       throws IOException {
-
+    Map<Term,TermContext> termContexts = new HashMap<>();
+    TreeSet<Term> terms = new TreeSet<>();
     final IndexSearcher searcher = new IndexSearcher(context);
     searcher.setQueryCache(null);
-
-    SpanWeight w = (SpanWeight) searcher.createNormalizedWeight(query, false);
-
-    PayloadSpanCollector collector = new PayloadSpanCollector();
+    searcher.createNormalizedWeight(query, false).extractTerms(terms);
+    for (Term term : terms) {
+      termContexts.put(term, TermContext.build(context, term));
+    }
     for (LeafReaderContext leafReaderContext : context.leaves()) {
-      final Spans spans = w.getSpans(leafReaderContext, leafReaderContext.reader().getLiveDocs(), collector);
+      final Spans spans = query.getSpans(leafReaderContext, leafReaderContext.reader().getLiveDocs(), termContexts);
       if (spans != null) {
         while (spans.nextDoc() != Spans.NO_MORE_DOCS) {
           while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) {
-            collector.reset();
-            spans.collect(collector);
-            payloads.addAll(collector.getPayloads());
+            if (spans.isPayloadAvailable()) {
+              Collection<byte[]> payload = spans.getPayload();
+              for (byte [] bytes : payload) {
+                payloads.add(bytes);
+              }
+            }
           }
         }
       }

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java Fri May 22 02:15:34 2015
@@ -17,29 +17,26 @@ package org.apache.lucene.search.payload
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.Objects;
+
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.PostingsEnum;
 import org.apache.lucene.index.Term;
-import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.similarities.DefaultSimilarity;
 import org.apache.lucene.search.similarities.Similarity;
 import org.apache.lucene.search.similarities.Similarity.SimScorer;
-import org.apache.lucene.search.spans.BufferedSpanCollector;
-import org.apache.lucene.search.spans.SpanCollector;
 import org.apache.lucene.search.spans.SpanQuery;
 import org.apache.lucene.search.spans.SpanScorer;
-import org.apache.lucene.search.spans.SpanSimilarity;
 import org.apache.lucene.search.spans.SpanTermQuery;
 import org.apache.lucene.search.spans.SpanWeight;
 import org.apache.lucene.search.spans.Spans;
+import org.apache.lucene.search.spans.TermSpans;
 import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.BytesRef;
 
-import java.io.IOException;
-import java.util.Objects;
-
 /**
  * This class is very similar to
  * {@link org.apache.lucene.search.spans.SpanTermQuery} except that it factors
@@ -70,67 +67,33 @@ public class PayloadTermQuery extends Sp
 
   @Override
   public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
-    TermContext context = TermContext.build(searcher.getTopReaderContext(), term);
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, searcher.termStatistics(term, context));
-    return new PayloadTermWeight(context, similarity);
-  }
-
-  private static class PayloadTermCollector implements SpanCollector {
-
-    BytesRef payload;
-
-    @Override
-    public void reset() {
-      payload = null;
-    }
-
-    @Override
-    public int requiredPostings() {
-      return PostingsEnum.PAYLOADS;
-    }
-
-    @Override
-    public void collectLeaf(PostingsEnum postings, Term term) throws IOException {
-      payload = postings.getPayload();
-    }
-
-    @Override
-    public BufferedSpanCollector buffer() {
-      throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public SpanCollector bufferedCollector() {
-      throw new UnsupportedOperationException();
-    }
+    return new PayloadTermWeight(this, searcher);
   }
 
-  private class PayloadTermWeight extends SpanTermWeight {
+  protected class PayloadTermWeight extends SpanWeight {
 
-    public PayloadTermWeight(TermContext context, SpanSimilarity similarity)
+    public PayloadTermWeight(PayloadTermQuery query, IndexSearcher searcher)
         throws IOException {
-      super(context, similarity, PayloadSpanCollector.FACTORY);
+      super(query, searcher);
     }
 
     @Override
     public PayloadTermSpanScorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
-      PayloadTermCollector collector = new PayloadTermCollector();
-      Spans spans = super.getSpans(context, acceptDocs, collector);
+      TermSpans spans = (TermSpans) query.getSpans(context, acceptDocs, termContexts);
       return (spans == null)
               ? null
-              : new PayloadTermSpanScorer(spans, this, collector, similarity.simScorer(context));
+              : new PayloadTermSpanScorer(spans, this, similarity.simScorer(stats, context));
     }
 
     protected class PayloadTermSpanScorer extends SpanScorer {
       protected BytesRef payload;
       protected float payloadScore;
       protected int payloadsSeen;
-      private final PayloadTermCollector payloadCollector;
+      private final TermSpans termSpans;
 
-      public PayloadTermSpanScorer(Spans spans, SpanWeight weight, PayloadTermCollector collector,
-                                   Similarity.SimScorer docScorer) throws IOException {
+      public PayloadTermSpanScorer(TermSpans spans, SpanWeight weight, Similarity.SimScorer docScorer) throws IOException {
         super(spans, weight, docScorer);
-        this.payloadCollector = collector;
+        termSpans = spans; // CHECKME: generics to use SpansScorer.spans as TermSpans.
       }
 
       @Override
@@ -146,22 +109,29 @@ public class PayloadTermQuery extends Sp
 
           freq += docScorer.computeSlopFactor(matchLength);
           numMatches++;
-          payloadCollector.reset();
-          spans.collect(payloadCollector);
-          processPayload();
+          processPayload(similarity);
 
           startPos = spans.nextStartPosition();
         } while (startPos != Spans.NO_MORE_POSITIONS);
       }
 
-      protected void processPayload() throws IOException {
-
-        float payloadFactor = payloadCollector.payload == null ? 1F :
-            docScorer.computePayloadFactor(docID(), spans.startPosition(), spans.endPosition(), payloadCollector.payload);
-        payloadScore = function.currentScore(docID(), term.field(), spans.startPosition(), spans.endPosition(),
-                                             payloadsSeen, payloadScore, payloadFactor);
-        payloadsSeen++;
-
+      protected void processPayload(Similarity similarity) throws IOException {
+        if (spans.isPayloadAvailable()) {
+          final PostingsEnum postings = termSpans.getPostings();
+          payload = postings.getPayload();
+          if (payload != null) {
+            payloadScore = function.currentScore(docID(), term.field(),
+                                                 spans.startPosition(), spans.endPosition(), payloadsSeen, payloadScore,
+                                                 docScorer.computePayloadFactor(docID(), spans.startPosition(), spans.endPosition(), payload));
+          } else {
+            payloadScore = function.currentScore(docID(), term.field(),
+                                                 spans.startPosition(), spans.endPosition(), payloadsSeen, payloadScore, 1F);
+          }
+          payloadsSeen++;
+
+        } else {
+          // zero out the payload?
+        }
       }
 
       /**
@@ -208,7 +178,7 @@ public class PayloadTermQuery extends Sp
         if (newDoc == doc) {
           float freq = scorer.sloppyFreq();
           Explanation freqExplanation = Explanation.match(freq, "phraseFreq=" + freq);
-          SimScorer docScorer = similarity.simScorer(context);
+          SimScorer docScorer = similarity.simScorer(stats, context);
           Explanation scoreExplanation = docScorer.explain(doc, freqExplanation);
           Explanation expl = Explanation.match(
               scoreExplanation.getValue(),

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/ContainSpans.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/ContainSpans.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/ContainSpans.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/ContainSpans.java Fri May 22 02:15:34 2015
@@ -19,6 +19,7 @@ package org.apache.lucene.search.spans;
 
 import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Objects;
 
 abstract class ContainSpans extends ConjunctionSpans {
@@ -48,8 +49,12 @@ abstract class ContainSpans extends Conj
   }
 
   @Override
-  public void collect(SpanCollector collector) throws IOException {
-    sourceSpans.collect(collector);
+  public boolean isPayloadAvailable() throws IOException {
+    return sourceSpans.isPayloadAvailable();
   }
 
+  @Override
+  public Collection<byte[]> getPayload() throws IOException {
+    return sourceSpans.getPayload();
+  }
 }

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FieldMaskingSpanQuery.java Fri May 22 02:15:34 2015
@@ -17,14 +17,21 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+import java.util.Objects;
+
+import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Weight;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.ToStringUtils;
 
-import java.io.IOException;
-import java.util.Objects;
-
 /**
  * <p>Wrapper to allow {@link SpanQuery} objects participate in composite 
  * single-field SpanQueries by 'lying' about their search field. That is, 
@@ -88,10 +95,20 @@ public class FieldMaskingSpanQuery exten
 
   // :NOTE: getBoost and setBoost are not proxied to the maskedQuery
   // ...this is done to be more consistent with things like SpanFirstQuery
+  
+  @Override
+  public Spans getSpans(LeafReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException {
+    return maskedQuery.getSpans(context, acceptDocs, termContexts);
+  }
+
+  @Override
+  public void extractTerms(Set<Term> terms) {
+    maskedQuery.extractTerms(terms);
+  }  
 
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    return maskedQuery.createWeight(searcher, needsScores, factory);
+  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
+    return maskedQuery.createWeight(searcher, needsScores);
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FilterSpans.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FilterSpans.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FilterSpans.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/FilterSpans.java Fri May 22 02:15:34 2015
@@ -17,11 +17,12 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
-import org.apache.lucene.search.TwoPhaseIterator;
-
 import java.io.IOException;
+import java.util.Collection;
 import java.util.Objects;
 
+import org.apache.lucene.search.TwoPhaseIterator;
+
 /**
  * A {@link Spans} implementation wrapping another spans instance,
  * allowing to filter spans matches easily by implementing {@link #accept}
@@ -109,13 +110,18 @@ public abstract class FilterSpans extend
     return atFirstInCurrentDoc ? -1
           : (startPos != NO_MORE_POSITIONS) ? in.endPosition() : NO_MORE_POSITIONS;
   }
-
+  
   @Override
-  public void collect(SpanCollector collector) throws IOException {
-    in.collect(collector);
+  public final Collection<byte[]> getPayload() throws IOException {
+    return in.getPayload();
   }
 
   @Override
+  public final boolean isPayloadAvailable() throws IOException {
+    return in.isPayloadAvailable();
+  }
+  
+  @Override
   public final long cost() {
     return in.cost();
   }

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansOrdered.java Fri May 22 02:15:34 2015
@@ -19,9 +19,12 @@ package org.apache.lucene.search.spans;
 
 import java.io.IOException;
 import java.util.List;
+import java.util.Collection;
 
 /** A Spans that is formed from the ordered subspans of a SpanNearQuery
- * where the subspans do not overlap and have a maximum slop between them.
+ * where the subspans do not overlap and have a maximum slop between them,
+ * and that does not need to collect payloads.
+ * To also collect payloads, see {@link NearSpansPayloadOrdered}.
  * <p>
  * The formed spans only contains minimum slop matches.<br>
  * The matching slop is computed from the distance(s) between
@@ -38,9 +41,6 @@ import java.util.List;
  * <pre>t1 t2 .. t3      </pre>
  * <pre>      t1 .. t2 t3</pre>
  *
- * Because the algorithm used to minimize the size of a match consumes
- * child Spans eagerly, this uses a BufferedSpanCollector to collect
- * information from subspans.
  *
  * Expert:
  * Only public for subclassing.  Most implementations should not need this class
@@ -51,13 +51,9 @@ public class NearSpansOrdered extends Ne
   protected int matchStart = -1;
   protected int matchEnd = -1;
 
-  protected final SpanCollector collector;
-  protected BufferedSpanCollector buffer;
-
-  public NearSpansOrdered(SpanNearQuery query, List<Spans> subSpans, SpanCollector collector) throws IOException {
+  public NearSpansOrdered(SpanNearQuery query, List<Spans> subSpans) throws IOException {
     super(query, subSpans);
     this.atFirstInCurrentDoc = true; // -1 startPosition/endPosition also at doc -1
-    this.collector = collector;
   }
 
   @Override
@@ -144,15 +140,10 @@ public class NearSpansOrdered extends Ne
     matchStart = lastSubSpans.startPosition();
     matchEnd = lastSubSpans.endPosition();
 
-    buffer = collector.buffer();
-    buffer.collectCandidate(subSpans[subSpans.length - 1]);
-    buffer.accept();
-
     int matchSlop = 0;
     int lastStart = matchStart;
     for (int i = subSpans.length - 2; i >= 0; i--) {
       Spans prevSpans = subSpans[i];
-      buffer.collectCandidate(prevSpans);
 
       int prevStart = prevSpans.startPosition();
       int prevEnd = prevSpans.endPosition();
@@ -169,11 +160,8 @@ public class NearSpansOrdered extends Ne
         // prevSpans still before (lastStart, lastEnd)
         prevStart = ppStart;
         prevEnd = ppEnd;
-        buffer.collectCandidate(prevSpans);
       }
 
-      buffer.accept();
-
       assert prevStart <= matchStart;
       if (matchStart > prevEnd) { // Only non overlapping spans add to slop.
         matchSlop += (matchStart - prevEnd);
@@ -202,10 +190,13 @@ public class NearSpansOrdered extends Ne
   }
 
   @Override
-  public void collect(SpanCollector collector) {
-    assert collector == this.collector
-        : "You must collect using the same SpanCollector as was passed to the NearSpans constructor";
-    buffer.replay();
+  public Collection<byte[]> getPayload() throws IOException {
+    return null;
+  }
+
+  @Override
+  public boolean isPayloadAvailable() {
+    return false;
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java Fri May 22 02:15:34 2015
@@ -22,7 +22,10 @@ import org.apache.lucene.util.PriorityQu
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Similar to {@link NearSpansOrdered}, but for the unordered case.
@@ -115,8 +118,13 @@ public class NearSpansUnordered extends
     }
 
     @Override
-    public void collect(SpanCollector collector) throws IOException {
-      in.collect(collector);
+    public Collection<byte[]> getPayload() throws IOException {
+      return in.getPayload();
+    }
+
+    @Override
+    public boolean isPayloadAvailable() throws IOException {
+      return in.isPayloadAvailable();
     }
 
     @Override
@@ -241,11 +249,31 @@ public class NearSpansUnordered extends
           : maxEndPositionCell.endPosition();
   }
 
+
+  /**
+   * WARNING: The List is not necessarily in order of the positions.
+   * @return Collection of <code>byte[]</code> payloads
+   * @throws IOException if there is a low-level I/O error
+   */
   @Override
-  public void collect(SpanCollector collector) throws IOException {
+  public Collection<byte[]> getPayload() throws IOException {
+    Set<byte[]> matchPayload = new HashSet<>();
     for (SpansCell cell : subSpanCells) {
-      cell.collect(collector);
+      if (cell.isPayloadAvailable()) {
+        matchPayload.addAll(cell.getPayload());
+      }
+    }
+    return matchPayload;
+  }
+
+  @Override
+  public boolean isPayloadAvailable() throws IOException {
+    for (SpansCell cell : subSpanCells) {
+      if (cell.isPayloadAvailable()) {
+        return true;
+      }
     }
+    return false;
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainQuery.java Fri May 22 02:15:34 2015
@@ -17,21 +17,20 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
-import org.apache.lucene.index.IndexReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Set;
+import java.util.Objects;
+
 import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.util.Bits;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-
 abstract class SpanContainQuery extends SpanQuery implements Cloneable {
-
   SpanQuery big;
   SpanQuery little;
 
@@ -49,48 +48,26 @@ abstract class SpanContainQuery extends
   @Override
   public String getField() { return big.getField(); }
 
-  public abstract class SpanContainWeight extends SpanWeight {
-
-    final SpanWeight bigWeight;
-    final SpanWeight littleWeight;
-
-    public SpanContainWeight(SpanSimilarity similarity, SpanCollectorFactory factory,
-                             SpanWeight bigWeight, SpanWeight littleWeight) throws IOException {
-      super(SpanContainQuery.this, similarity, factory);
-      this.bigWeight = bigWeight;
-      this.littleWeight = littleWeight;
-    }
-
-    /**
-     * Extract terms from both <code>big</code> and <code>little</code>.
-     */
-    @Override
-    public void extractTerms(Set<Term> terms) {
-      bigWeight.extractTerms(terms);
-      littleWeight.extractTerms(terms);
-    }
+  /** Extract terms from both <code>big</code> and <code>little</code>. */
+  @Override
+  public void extractTerms(Set<Term> terms) {
+    big.extractTerms(terms);
+    little.extractTerms(terms);
+  }
 
-    ArrayList<Spans> prepareConjunction(final LeafReaderContext context, final Bits acceptDocs, SpanCollector collector) throws IOException {
-      Spans bigSpans = bigWeight.getSpans(context, acceptDocs, collector);
-      if (bigSpans == null) {
-        return null;
-      }
-      Spans littleSpans = littleWeight.getSpans(context, acceptDocs, collector);
-      if (littleSpans == null) {
-        return null;
-      }
-      ArrayList<Spans> bigAndLittle = new ArrayList<>();
-      bigAndLittle.add(bigSpans);
-      bigAndLittle.add(littleSpans);
-      return bigAndLittle;
+  ArrayList<Spans> prepareConjunction(final LeafReaderContext context, final Bits acceptDocs, final Map<Term,TermContext> termContexts) throws IOException {
+    Spans bigSpans = big.getSpans(context, acceptDocs, termContexts);
+    if (bigSpans == null) {
+      return null;
     }
-
-    @Override
-    public void extractTermContexts(Map<Term, TermContext> contexts) {
-      bigWeight.extractTermContexts(contexts);
-      littleWeight.extractTermContexts(contexts);
+    Spans littleSpans = little.getSpans(context, acceptDocs, termContexts);
+    if (littleSpans == null) {
+      return null;
     }
-
+    ArrayList<Spans> bigAndLittle = new ArrayList<>();
+    bigAndLittle.add(bigSpans);
+    bigAndLittle.add(littleSpans);
+    return bigAndLittle;
   }
 
   String toString(String field, String name) {

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainingQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainingQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainingQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanContainingQuery.java Fri May 22 02:15:34 2015
@@ -17,13 +17,15 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
-import org.apache.lucene.index.LeafReaderContext;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.util.Bits;
-
 import java.io.IOException;
+import java.util.Map;
 import java.util.ArrayList;
 
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.TermContext;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.util.Bits;
+
 /** Keep matches that contain another Spans. */
 public class SpanContainingQuery extends SpanContainQuery {
   /** Construct a SpanContainingQuery matching spans from <code>big</code>
@@ -46,79 +48,63 @@ public class SpanContainingQuery extends
           (SpanQuery) big.clone(),
           (SpanQuery) little.clone());
   }
-
+  
+  /** 
+   * Return spans from <code>big</code> that contain at least one spans from <code>little</code>.
+   * The payload is from the spans of <code>big</code>.
+   */
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    SpanWeight bigWeight = big.createWeight(searcher, false, factory);
-    SpanWeight littleWeight = little.createWeight(searcher, false, factory);
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, bigWeight, littleWeight);
-    return new SpanContainingWeight(similarity, factory, bigWeight, littleWeight);
-  }
-
-  public class SpanContainingWeight extends SpanContainWeight {
-
-    public SpanContainingWeight(SpanSimilarity similarity, SpanCollectorFactory factory,
-                                SpanWeight bigWeight, SpanWeight littleWeight) throws IOException {
-      super(similarity, factory, bigWeight, littleWeight);
+  public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, final Map<Term,TermContext> termContexts) throws IOException {
+    ArrayList<Spans> containerContained = prepareConjunction(context, acceptDocs, termContexts);
+    if (containerContained == null) {
+      return null;
     }
-
-    /**
-     * Return spans from <code>big</code> that contain at least one spans from <code>little</code>.
-     * The payload is from the spans of <code>big</code>.
-     */
-    @Override
-    public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, SpanCollector collector) throws IOException {
-      ArrayList<Spans> containerContained = prepareConjunction(context, acceptDocs, collector);
-      if (containerContained == null) {
-        return null;
+    
+    Spans big = containerContained.get(0);
+    Spans little = containerContained.get(1);
+
+    return new ContainSpans(big, little, big) {
+
+      @Override
+      boolean twoPhaseCurrentDocMatches() throws IOException {
+        oneExhaustedInCurrentDoc = false;
+        assert littleSpans.startPosition() == -1;
+        while (bigSpans.nextStartPosition() != NO_MORE_POSITIONS) {
+          while (littleSpans.startPosition() < bigSpans.startPosition()) {
+            if (littleSpans.nextStartPosition() == NO_MORE_POSITIONS) {
+              oneExhaustedInCurrentDoc = true;
+              return false;
+            }
+          }
+          if (bigSpans.endPosition() >= littleSpans.endPosition()) {
+            atFirstInCurrentDoc = true;
+            return true;
+          }
+        } 
+        oneExhaustedInCurrentDoc = true;
+        return false;
       }
 
-      Spans big = containerContained.get(0);
-      Spans little = containerContained.get(1);
-
-      return new ContainSpans(big, little, big) {
-
-        @Override
-        boolean twoPhaseCurrentDocMatches() throws IOException {
-          oneExhaustedInCurrentDoc = false;
-          assert littleSpans.startPosition() == -1;
-          while (bigSpans.nextStartPosition() != NO_MORE_POSITIONS) {
-            while (littleSpans.startPosition() < bigSpans.startPosition()) {
-              if (littleSpans.nextStartPosition() == NO_MORE_POSITIONS) {
-                oneExhaustedInCurrentDoc = true;
-                return false;
-              }
-            }
-            if (bigSpans.endPosition() >= littleSpans.endPosition()) {
-              atFirstInCurrentDoc = true;
-              return true;
+      @Override
+      public int nextStartPosition() throws IOException {
+        if (atFirstInCurrentDoc) {
+          atFirstInCurrentDoc = false;
+          return bigSpans.startPosition();
+        }
+        while (bigSpans.nextStartPosition() != NO_MORE_POSITIONS) {
+          while (littleSpans.startPosition() < bigSpans.startPosition()) {
+            if (littleSpans.nextStartPosition() == NO_MORE_POSITIONS) {
+              oneExhaustedInCurrentDoc = true;
+              return NO_MORE_POSITIONS;
             }
           }
-          oneExhaustedInCurrentDoc = true;
-          return false;
-        }
-
-        @Override
-        public int nextStartPosition() throws IOException {
-          if (atFirstInCurrentDoc) {
-            atFirstInCurrentDoc = false;
+          if (bigSpans.endPosition() >= littleSpans.endPosition()) {
             return bigSpans.startPosition();
           }
-          while (bigSpans.nextStartPosition() != NO_MORE_POSITIONS) {
-            while (littleSpans.startPosition() < bigSpans.startPosition()) {
-              if (littleSpans.nextStartPosition() == NO_MORE_POSITIONS) {
-                oneExhaustedInCurrentDoc = true;
-                return NO_MORE_POSITIONS;
-              }
-            }
-            if (bigSpans.endPosition() >= littleSpans.endPosition()) {
-              return bigSpans.startPosition();
-            }
-          }
-          oneExhaustedInCurrentDoc = true;
-          return NO_MORE_POSITIONS;
         }
-      };
-    }
+        oneExhaustedInCurrentDoc = true;
+        return NO_MORE_POSITIONS;
+      }
+    };
   }
 }
\ No newline at end of file

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java Fri May 22 02:15:34 2015
@@ -37,7 +37,7 @@ public class SpanFirstQuery extends Span
   }
 
   @Override
-  protected AcceptStatus acceptPosition(Spans spans, SpanCollector collector) throws IOException {
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
     assert spans.startPosition() != spans.endPosition() : "start equals end: " + spans.startPosition();
     if (spans.startPosition() >= end)
       return AcceptStatus.NO_MORE_IN_CURRENT_DOC;

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanMultiTermQueryWrapper.java Fri May 22 02:15:34 2015
@@ -17,18 +17,21 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+import java.util.Objects;
+
+import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
-import org.apache.lucene.search.BooleanClause.Occur;
-import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.MultiTermQuery;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoringRewrite;
 import org.apache.lucene.search.TopTermsRewrite;
-
-import java.io.IOException;
-import java.util.Objects;
+import org.apache.lucene.search.ScoringRewrite;
+import org.apache.lucene.search.BooleanClause.Occur; // javadocs only
+import org.apache.lucene.util.Bits;
 
 /**
  * Wraps any {@link MultiTermQuery} as a {@link SpanQuery}, 
@@ -72,6 +75,11 @@ public class SpanMultiTermQueryWrapper<Q
     }
   }
 
+  @Override
+  protected void extractTerms(Set<Term> terms) {
+    throw new IllegalStateException("Rewrite first");
+  }
+
   /**
    * Expert: returns the rewriteMethod
    */
@@ -89,17 +97,17 @@ public class SpanMultiTermQueryWrapper<Q
   public final void setRewriteMethod(SpanRewriteMethod rewriteMethod) {
     query.setRewriteMethod(rewriteMethod);
   }
-
+  
   @Override
-  public String getField() {
-    return query.getField();
+  public Spans getSpans(LeafReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException {
+    throw new UnsupportedOperationException("Query should have been rewritten");
   }
 
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    throw new IllegalArgumentException("Rewrite first!");
+  public String getField() {
+    return query.getField();
   }
-
+  
   /** Returns the wrapped query */
   public Query getWrappedQuery() {
     return query;

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNearQuery.java Fri May 22 02:15:34 2015
@@ -17,23 +17,22 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
-import org.apache.lucene.index.IndexReader;
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
 import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
 import org.apache.lucene.index.Terms;
-import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.ToStringUtils;
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 /** Matches spans which are near one another.  One can specify <i>slop</i>, the
  * maximum number of intervening unmatched positions, as well as whether
  * matches are required to be in-order.
@@ -91,6 +90,13 @@ public class SpanNearQuery extends SpanQ
   public String getField() { return field; }
 
   @Override
+  public void extractTerms(Set<Term> terms) {
+    for (final SpanQuery clause : clauses) {
+      clause.extractTerms(terms);
+    }
+  }
+
+  @Override
   public String toString(String field) {
     StringBuilder buffer = new StringBuilder();
     buffer.append("spanNear([");
@@ -112,61 +118,27 @@ public class SpanNearQuery extends SpanQ
   }
 
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    List<SpanWeight> subWeights = new ArrayList<>();
-    for (SpanQuery q : clauses) {
-      subWeights.add(q.createWeight(searcher, false, factory));
-    }
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, subWeights);
-    return new SpanNearWeight(subWeights, similarity, factory);
-  }
-
-  public class SpanNearWeight extends SpanWeight {
+  public Spans getSpans(final LeafReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException {
+    ArrayList<Spans> subSpans = new ArrayList<>(clauses.size());
 
-    final List<SpanWeight> subWeights;
-
-    public SpanNearWeight(List<SpanWeight> subWeights, SpanSimilarity similarity, SpanCollectorFactory factory) throws IOException {
-      super(SpanNearQuery.this, similarity, factory);
-      this.subWeights = subWeights;
-    }
-
-    @Override
-    public void extractTermContexts(Map<Term, TermContext> contexts) {
-      for (SpanWeight w : subWeights) {
-        w.extractTermContexts(contexts);
+    for (SpanQuery seq : clauses) {
+      Spans subSpan = seq.getSpans(context, acceptDocs, termContexts);
+      if (subSpan != null) {
+        subSpans.add(subSpan);
+      } else {
+        return null; // all required
       }
     }
 
-    @Override
-    public Spans getSpans(final LeafReaderContext context, Bits acceptDocs, SpanCollector collector) throws IOException {
-
-      Terms terms = context.reader().terms(field);
-      if (terms == null) {
-        return null; // field does not exist
-      }
-
-      ArrayList<Spans> subSpans = new ArrayList<>(clauses.size());
-      SpanCollector subSpanCollector = inOrder ? collector.bufferedCollector() : collector;
-      for (SpanWeight w : subWeights) {
-        Spans subSpan = w.getSpans(context, acceptDocs, subSpanCollector);
-        if (subSpan != null) {
-          subSpans.add(subSpan);
-        } else {
-          return null; // all required
-        }
-      }
-
-      // all NearSpans require at least two subSpans
-      return (!inOrder) ? new NearSpansUnordered(SpanNearQuery.this, subSpans)
-          : new NearSpansOrdered(SpanNearQuery.this, subSpans, collector);
-    }
-
-    @Override
-    public void extractTerms(Set<Term> terms) {
-      for (SpanWeight w : subWeights) {
-        w.extractTerms(terms);
-      }
-    }
+    Terms terms = context.reader().terms(field);
+    if (terms == null) {
+      return null; // field does not exist
+    }
+    
+    // all NearSpans require at least two subSpans
+    return (! inOrder) ? new NearSpansUnordered(this, subSpans)
+          : collectPayloads && terms.hasPayloads() ? new NearSpansPayloadOrdered(this, subSpans)
+          : new NearSpansOrdered(this, subSpans);
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java Fri May 22 02:15:34 2015
@@ -22,7 +22,6 @@ import org.apache.lucene.index.IndexRead
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TwoPhaseIterator;
 import org.apache.lucene.util.Bits;
@@ -79,6 +78,9 @@ public class SpanNotQuery extends SpanQu
   public String getField() { return include.getField(); }
 
   @Override
+  public void extractTerms(Set<Term> terms) { include.extractTerms(terms); }
+
+  @Override
   public String toString(String field) {
     StringBuilder buffer = new StringBuilder();
     buffer.append("spanNot(");
@@ -102,100 +104,70 @@ public class SpanNotQuery extends SpanQu
     return spanNotQuery;
   }
 
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    SpanWeight includeWeight = include.createWeight(searcher, false, factory);
-    SpanWeight excludeWeight = exclude.createWeight(searcher, false, factory);
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, includeWeight);
-    return new SpanNotWeight(similarity, factory, includeWeight, excludeWeight);
-  }
-
-  public class SpanNotWeight extends SpanWeight {
-
-    final SpanWeight includeWeight;
-    final SpanWeight excludeWeight;
-
-    public SpanNotWeight(SpanSimilarity similarity, SpanCollectorFactory factory,
-                         SpanWeight includeWeight, SpanWeight excludeWeight) throws IOException {
-      super(SpanNotQuery.this, similarity, factory);
-      this.includeWeight = includeWeight;
-      this.excludeWeight = excludeWeight;
+  @Override
+  public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, final Map<Term,TermContext> termContexts) throws IOException {
+    final Spans includeSpans = include.getSpans(context, acceptDocs, termContexts);
+    if (includeSpans == null) {
+      return null;
     }
 
-    @Override
-    public void extractTermContexts(Map<Term, TermContext> contexts) {
-      includeWeight.extractTermContexts(contexts);
+    final Spans excludeSpans = exclude.getSpans(context, acceptDocs, termContexts);
+    if (excludeSpans == null) {
+      return includeSpans;
     }
-
-    @Override
-    public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, SpanCollector collector) throws IOException {
-      final Spans includeSpans = includeWeight.getSpans(context, acceptDocs, collector);
-      if (includeSpans == null) {
-        return null;
-      }
-
-      final Spans excludeSpans = excludeWeight.getSpans(context, acceptDocs, collector);
-      if (excludeSpans == null) {
-        return includeSpans;
-      }
-
-      final TwoPhaseIterator excludeTwoPhase = excludeSpans.asTwoPhaseIterator();
-      final DocIdSetIterator excludeApproximation = excludeTwoPhase == null ? null : excludeTwoPhase.approximation();
-
-      return new FilterSpans(includeSpans) {
-        // last document we have checked matches() against for the exclusion, and failed
-        // when using approximations, so we don't call it again, and pass thru all inclusions.
-        int lastApproxDoc = -1;
-        boolean lastApproxResult = false;
-
-        @Override
-        protected AcceptStatus accept(Spans candidate) throws IOException {
-          // TODO: this logic is ugly and sneaky, can we clean it up?
-          int doc = candidate.docID();
-          if (doc > excludeSpans.docID()) {
-            // catch up 'exclude' to the current doc
-            if (excludeTwoPhase != null) {
-              if (excludeApproximation.advance(doc) == doc) {
-                lastApproxDoc = doc;
-                lastApproxResult = excludeTwoPhase.matches();
-              }
-            } else {
-              excludeSpans.advance(doc);
+    
+    final TwoPhaseIterator excludeTwoPhase = excludeSpans.asTwoPhaseIterator();
+    final DocIdSetIterator excludeApproximation = excludeTwoPhase == null ? null : excludeTwoPhase.approximation();
+    
+    return new FilterSpans(includeSpans) {
+      // last document we have checked matches() against for the exclusion, and failed
+      // when using approximations, so we don't call it again, and pass thru all inclusions.
+      int lastApproxDoc = -1;
+      boolean lastApproxResult = false;
+      
+      @Override
+      protected AcceptStatus accept(Spans candidate) throws IOException {
+        // TODO: this logic is ugly and sneaky, can we clean it up?
+        int doc = candidate.docID();
+        if (doc > excludeSpans.docID()) {
+          // catch up 'exclude' to the current doc
+          if (excludeTwoPhase != null) {
+            if (excludeApproximation.advance(doc) == doc) {
+              lastApproxDoc = doc;
+              lastApproxResult = excludeTwoPhase.matches();
             }
-          } else if (excludeTwoPhase != null && doc == excludeSpans.docID() && doc != lastApproxDoc) {
-            // excludeSpans already sitting on our candidate doc, but matches not called yet.
-            lastApproxDoc = doc;
-            lastApproxResult = excludeTwoPhase.matches();
-          }
-
-          if (doc != excludeSpans.docID() || (doc == lastApproxDoc && lastApproxResult == false)) {
-            return AcceptStatus.YES;
-          }
-
-          if (excludeSpans.startPosition() == -1) { // init exclude start position if needed
-            excludeSpans.nextStartPosition();
-          }
-
-          while (excludeSpans.endPosition() <= candidate.startPosition() - pre) {
-            // exclude end position is before a possible exclusion
-            if (excludeSpans.nextStartPosition() == NO_MORE_POSITIONS) {
-              return AcceptStatus.YES; // no more exclude at current doc.
-            }
-          }
-
-          // exclude end position far enough in current doc, check start position:
-          if (candidate.endPosition() + post <= excludeSpans.startPosition()) {
-            return AcceptStatus.YES;
           } else {
-            return AcceptStatus.NO;
+            excludeSpans.advance(doc);
           }
+        } else if (excludeTwoPhase != null && doc == excludeSpans.docID() && doc != lastApproxDoc) {
+          // excludeSpans already sitting on our candidate doc, but matches not called yet.
+          lastApproxDoc = doc;
+          lastApproxResult = excludeTwoPhase.matches();
         }
-      };
-    }
-
-    @Override
-    public void extractTerms(Set<Term> terms) {
-      includeWeight.extractTerms(terms);
-    }
+        
+        if (doc != excludeSpans.docID() || (doc == lastApproxDoc && lastApproxResult == false)) {
+          return AcceptStatus.YES;
+        }
+        
+        if (excludeSpans.startPosition() == -1) { // init exclude start position if needed
+          excludeSpans.nextStartPosition();
+        }
+        
+        while (excludeSpans.endPosition() <= candidate.startPosition() - pre) {
+          // exclude end position is before a possible exclusion
+          if (excludeSpans.nextStartPosition() == NO_MORE_POSITIONS) {
+            return AcceptStatus.YES; // no more exclude at current doc.
+          }
+        }
+        
+        // exclude end position far enough in current doc, check start position:
+        if (candidate.endPosition() + post <= excludeSpans.startPosition()) {
+          return AcceptStatus.YES;
+        } else {
+          return AcceptStatus.NO;
+        }
+      }
+    };
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanOrQuery.java Fri May 22 02:15:34 2015
@@ -17,25 +17,26 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
-import org.apache.lucene.index.IndexReader;
+import java.io.IOException;
+
+import java.util.List;
+import java.util.Collection;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
 import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.ToStringUtils;
+import org.apache.lucene.search.Query;
 import org.apache.lucene.search.DisiPriorityQueue;
 import org.apache.lucene.search.DisiWrapper;
-import org.apache.lucene.search.DisjunctionDISIApproximation;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TwoPhaseIterator;
-import org.apache.lucene.util.Bits;
-import org.apache.lucene.util.ToStringUtils;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import org.apache.lucene.search.DisjunctionDISIApproximation;
 
 
 /** Matches the union of its clauses.
@@ -73,6 +74,13 @@ public class SpanOrQuery extends SpanQue
   public String getField() { return field; }
 
   @Override
+  public void extractTerms(Set<Term> terms) {
+    for(final SpanQuery clause: clauses) {
+      clause.extractTerms(terms);
+    }
+  }
+
+  @Override
   public SpanOrQuery clone() {
     int sz = clauses.size();
     SpanQuery[] newClauses = new SpanQuery[sz];
@@ -137,223 +145,198 @@ public class SpanOrQuery extends SpanQue
     return h;
   }
 
-  @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    List<SpanWeight> subWeights = new ArrayList<>(clauses.size());
-    for (SpanQuery q : clauses) {
-      subWeights.add(q.createWeight(searcher, false, factory));
-    }
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, subWeights);
-    return new SpanOrWeight(similarity, factory, subWeights);
-  }
 
-  public class SpanOrWeight extends SpanWeight {
-
-    final List<SpanWeight> subWeights;
+  @Override
+  public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, final Map<Term,TermContext> termContexts)
+  throws IOException {
 
-    public SpanOrWeight(SpanSimilarity similarity, SpanCollectorFactory factory, List<SpanWeight> subWeights) throws IOException {
-      super(SpanOrQuery.this, similarity, factory);
-      this.subWeights = subWeights;
-    }
+    final ArrayList<Spans> subSpans = new ArrayList<>(clauses.size());
 
-    @Override
-    public void extractTerms(Set<Term> terms) {
-      for (final SpanWeight w: subWeights) {
-        w.extractTerms(terms);
+    for (SpanQuery sq : clauses) {
+      Spans spans = sq.getSpans(context, acceptDocs, termContexts);
+      if (spans != null) {
+        subSpans.add(spans);
       }
     }
 
-    @Override
-    public void extractTermContexts(Map<Term, TermContext> contexts) {
-      for (SpanWeight w : subWeights) {
-        w.extractTermContexts(contexts);
+    if (subSpans.size() == 0) {
+      return null;
+    } else if (subSpans.size() == 1) {
+      return subSpans.get(0);
+    }
+
+    final DisiPriorityQueue<Spans> byDocQueue = new DisiPriorityQueue<>(subSpans.size());
+    for (Spans spans : subSpans) {
+      byDocQueue.add(new DisiWrapper<>(spans));
+    }
+
+    final SpanPositionQueue byPositionQueue = new SpanPositionQueue(subSpans.size()); // when empty use -1
+
+    return new Spans() {
+      Spans topPositionSpans = null;
+
+      @Override
+      public int nextDoc() throws IOException {
+        topPositionSpans = null;
+        DisiWrapper<Spans> topDocSpans = byDocQueue.top();
+        int currentDoc = topDocSpans.doc;
+        do {
+          topDocSpans.doc = topDocSpans.iterator.nextDoc();
+          topDocSpans = byDocQueue.updateTop();
+        } while (topDocSpans.doc == currentDoc);
+        return topDocSpans.doc;
       }
-    }
 
-    @Override
-    public Spans getSpans(final LeafReaderContext context, final Bits acceptDocs, SpanCollector collector)
-        throws IOException {
-
-      final ArrayList<Spans> subSpans = new ArrayList<>(clauses.size());
-
-      for (SpanWeight w : subWeights) {
-        Spans spans = w.getSpans(context, acceptDocs, collector);
-        if (spans != null) {
-          subSpans.add(spans);
-        }
+      @Override
+      public int advance(int target) throws IOException {
+        topPositionSpans = null;
+        DisiWrapper<Spans> topDocSpans = byDocQueue.top();
+        do {
+          topDocSpans.doc = topDocSpans.iterator.advance(target);
+          topDocSpans = byDocQueue.updateTop();
+        } while (topDocSpans.doc < target);
+        return topDocSpans.doc;
       }
 
-      if (subSpans.size() == 0) {
-        return null;
-      } else if (subSpans.size() == 1) {
-        return subSpans.get(0);
-      }
-
-      final DisiPriorityQueue<Spans> byDocQueue = new DisiPriorityQueue<>(subSpans.size());
-      for (Spans spans : subSpans) {
-        byDocQueue.add(new DisiWrapper<>(spans));
-      }
-
-      final SpanPositionQueue byPositionQueue = new SpanPositionQueue(subSpans.size()); // when empty use -1
-
-      return new Spans() {
-        Spans topPositionSpans = null;
-
-        @Override
-        public int nextDoc() throws IOException {
-          topPositionSpans = null;
-          DisiWrapper<Spans> topDocSpans = byDocQueue.top();
-          int currentDoc = topDocSpans.doc;
-          do {
-            topDocSpans.doc = topDocSpans.iterator.nextDoc();
-            topDocSpans = byDocQueue.updateTop();
-          } while (topDocSpans.doc == currentDoc);
-          return topDocSpans.doc;
-        }
+      @Override
+      public int docID() {
+        DisiWrapper<Spans> topDocSpans = byDocQueue.top();
+        return topDocSpans.doc;
+      }
 
-        @Override
-        public int advance(int target) throws IOException {
-          topPositionSpans = null;
-          DisiWrapper<Spans> topDocSpans = byDocQueue.top();
-          do {
-            topDocSpans.doc = topDocSpans.iterator.advance(target);
-            topDocSpans = byDocQueue.updateTop();
-          } while (topDocSpans.doc < target);
-          return topDocSpans.doc;
+      @Override
+      public TwoPhaseIterator asTwoPhaseIterator() {
+        boolean hasApproximation = false;
+        for (DisiWrapper<Spans> w : byDocQueue) {
+          if (w.twoPhaseView != null) {
+            hasApproximation = true;
+            break;
+          }
         }
 
-        @Override
-        public int docID() {
-          DisiWrapper<Spans> topDocSpans = byDocQueue.top();
-          return topDocSpans.doc;
+        if (! hasApproximation) { // none of the sub spans supports approximations
+          return null;
         }
 
-        @Override
-        public TwoPhaseIterator asTwoPhaseIterator() {
-          boolean hasApproximation = false;
-          for (DisiWrapper<Spans> w : byDocQueue) {
-            if (w.twoPhaseView != null) {
-              hasApproximation = true;
-              break;
-            }
+        return new TwoPhaseIterator(new DisjunctionDISIApproximation<Spans>(byDocQueue)) {
+          @Override
+          public boolean matches() throws IOException {
+            return twoPhaseCurrentDocMatches();
           }
+        };
+      }
+      
+      int lastDocTwoPhaseMatched = -1;
 
-          if (!hasApproximation) { // none of the sub spans supports approximations
-            return null;
+      boolean twoPhaseCurrentDocMatches() throws IOException {
+        DisiWrapper<Spans> listAtCurrentDoc = byDocQueue.topList();
+        // remove the head of the list as long as it does not match
+        final int currentDoc = listAtCurrentDoc.doc;
+        while (listAtCurrentDoc.twoPhaseView != null) {
+          if (listAtCurrentDoc.twoPhaseView.matches()) {
+            // use this spans for positions at current doc:
+            listAtCurrentDoc.lastApproxMatchDoc = currentDoc;
+            break;
           }
-
-          return new TwoPhaseIterator(new DisjunctionDISIApproximation<Spans>(byDocQueue)) {
-            @Override
-            public boolean matches() throws IOException {
-              return twoPhaseCurrentDocMatches();
-            }
-          };
-        }
-
-        int lastDocTwoPhaseMatched = -1;
-
-        boolean twoPhaseCurrentDocMatches() throws IOException {
-          DisiWrapper<Spans> listAtCurrentDoc = byDocQueue.topList();
-          // remove the head of the list as long as it does not match
-          final int currentDoc = listAtCurrentDoc.doc;
-          while (listAtCurrentDoc.twoPhaseView != null) {
-            if (listAtCurrentDoc.twoPhaseView.matches()) {
-              // use this spans for positions at current doc:
-              listAtCurrentDoc.lastApproxMatchDoc = currentDoc;
-              break;
-            }
-            // do not use this spans for positions at current doc:
-            listAtCurrentDoc.lastApproxNonMatchDoc = currentDoc;
-            listAtCurrentDoc = listAtCurrentDoc.next;
-            if (listAtCurrentDoc == null) {
-              return false;
-            }
+          // do not use this spans for positions at current doc:
+          listAtCurrentDoc.lastApproxNonMatchDoc = currentDoc;
+          listAtCurrentDoc = listAtCurrentDoc.next;
+          if (listAtCurrentDoc == null) {
+            return false;
           }
-          lastDocTwoPhaseMatched = currentDoc;
-          topPositionSpans = null;
-          return true;
         }
+        lastDocTwoPhaseMatched = currentDoc;
+        topPositionSpans = null;
+        return true;
+      }
 
-        void fillPositionQueue() throws IOException { // called at first nextStartPosition
-          assert byPositionQueue.size() == 0;
-          // add all matching Spans at current doc to byPositionQueue
-          DisiWrapper<Spans> listAtCurrentDoc = byDocQueue.topList();
-          while (listAtCurrentDoc != null) {
-            Spans spansAtDoc = listAtCurrentDoc.iterator;
-            if (lastDocTwoPhaseMatched == listAtCurrentDoc.doc) { // matched by DisjunctionDisiApproximation
-              if (listAtCurrentDoc.twoPhaseView != null) { // matched by approximation
-                if (listAtCurrentDoc.lastApproxNonMatchDoc == listAtCurrentDoc.doc) { // matches() returned false
-                  spansAtDoc = null;
-                } else {
-                  if (listAtCurrentDoc.lastApproxMatchDoc != listAtCurrentDoc.doc) {
-                    if (!listAtCurrentDoc.twoPhaseView.matches()) {
-                      spansAtDoc = null;
-                    }
+      void fillPositionQueue() throws IOException { // called at first nextStartPosition
+        assert byPositionQueue.size() == 0;
+        // add all matching Spans at current doc to byPositionQueue
+        DisiWrapper<Spans> listAtCurrentDoc = byDocQueue.topList();
+        while (listAtCurrentDoc != null) {
+          Spans spansAtDoc = listAtCurrentDoc.iterator;
+          if (lastDocTwoPhaseMatched == listAtCurrentDoc.doc) { // matched by DisjunctionDisiApproximation
+            if (listAtCurrentDoc.twoPhaseView != null) { // matched by approximation
+              if (listAtCurrentDoc.lastApproxNonMatchDoc == listAtCurrentDoc.doc) { // matches() returned false
+                spansAtDoc = null;
+              } else {
+                if (listAtCurrentDoc.lastApproxMatchDoc != listAtCurrentDoc.doc) {
+                  if (! listAtCurrentDoc.twoPhaseView.matches()) {
+                    spansAtDoc = null;
                   }
                 }
-              }
-            }
-
-            if (spansAtDoc != null) {
-              assert spansAtDoc.docID() == listAtCurrentDoc.doc;
-              assert spansAtDoc.startPosition() == -1;
-              spansAtDoc.nextStartPosition();
-              assert spansAtDoc.startPosition() != NO_MORE_POSITIONS;
-              byPositionQueue.add(spansAtDoc);
+              } 
             }
-            listAtCurrentDoc = listAtCurrentDoc.next;
           }
-          assert byPositionQueue.size() > 0;
-        }
 
-        @Override
-        public int nextStartPosition() throws IOException {
-          if (topPositionSpans == null) {
-            byPositionQueue.clear();
-            fillPositionQueue(); // fills byPositionQueue at first position
-            topPositionSpans = byPositionQueue.top();
-          } else {
-            topPositionSpans.nextStartPosition();
-            topPositionSpans = byPositionQueue.updateTop();
+          if (spansAtDoc != null) {
+            assert spansAtDoc.docID() == listAtCurrentDoc.doc;
+            assert spansAtDoc.startPosition() == -1;
+            spansAtDoc.nextStartPosition();
+            assert spansAtDoc.startPosition() != NO_MORE_POSITIONS;
+            byPositionQueue.add(spansAtDoc);
           }
-          return topPositionSpans.startPosition();
+          listAtCurrentDoc = listAtCurrentDoc.next;
         }
-
-        @Override
-        public int startPosition() {
-          return topPositionSpans == null ? -1 : topPositionSpans.startPosition();
+        assert byPositionQueue.size() > 0;
+      }
+        
+      @Override
+      public int nextStartPosition() throws IOException {
+        if (topPositionSpans == null) {
+          byPositionQueue.clear();
+          fillPositionQueue(); // fills byPositionQueue at first position
+          topPositionSpans = byPositionQueue.top();
+        } else {
+          topPositionSpans.nextStartPosition();
+          topPositionSpans = byPositionQueue.updateTop();
         }
+        return topPositionSpans.startPosition();
+      }
 
-        @Override
-        public int endPosition() {
-          return topPositionSpans == null ? -1 : topPositionSpans.endPosition();
-        }
+      @Override
+      public int startPosition() {
+        return topPositionSpans == null ? -1 : topPositionSpans.startPosition();
+      }
 
-        @Override
-        public void collect(SpanCollector collector) throws IOException {
-          if (topPositionSpans != null)
-            topPositionSpans.collect(collector);
-        }
+      @Override
+      public int endPosition() {
+        return topPositionSpans == null ? -1 : topPositionSpans.endPosition();
+      }
 
-        @Override
-        public String toString() {
-          return "spanOr(" + SpanOrQuery.this + ")@" + docID() + ": " + startPosition() + " - " + endPosition();
-        }
+      @Override
+      public Collection<byte[]> getPayload() throws IOException {
+        return topPositionSpans == null
+                ? null
+                : topPositionSpans.isPayloadAvailable()
+                ? new ArrayList<>(topPositionSpans.getPayload())
+                : null;
+      }
 
-        long cost = -1;
+      @Override
+      public boolean isPayloadAvailable() throws IOException {
+        return (topPositionSpans != null) && topPositionSpans.isPayloadAvailable();
+      }
 
-        @Override
-        public long cost() {
-          if (cost == -1) {
-            cost = 0;
-            for (Spans spans : subSpans) {
-              cost += spans.cost();
-            }
+      @Override
+      public String toString() {
+        return "spanOr("+SpanOrQuery.this+")@"+docID()+": "+startPosition()+" - "+endPosition();
+      }
+
+      long cost = -1;
+
+      @Override
+      public long cost() {
+        if (cost == -1) {
+          cost = 0;
+          for (Spans spans : subSpans) {
+            cost += spans.cost();
           }
-          return cost;
         }
-      };
-    }
+        return cost;
+      }
+    };
   }
 
 }

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionCheckQuery.java Fri May 22 02:15:34 2015
@@ -21,7 +21,6 @@ import org.apache.lucene.index.LeafReade
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
-import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
 import org.apache.lucene.util.Bits;
@@ -48,9 +47,18 @@ public abstract class SpanPositionCheckQ
    * */
   public SpanQuery getMatch() { return match; }
 
+
+
   @Override
   public String getField() { return match.getField(); }
 
+
+
+  @Override
+  public void extractTerms(Set<Term> terms) {
+    match.extractTerms(terms);
+  }
+
   /**
    * Implementing classes are required to return whether the current position is a match for the passed in
    * "match" {@link SpanQuery}.
@@ -58,58 +66,25 @@ public abstract class SpanPositionCheckQ
    * This is only called if the underlying last {@link Spans#nextStartPosition()} for the
    * match indicated a valid start position.
    *
+   *
    * @param spans The {@link Spans} instance, positioned at the spot to check
-   * @param collector the {@link SpanCollector} associated with the Spans
    *
    * @return whether the match is accepted, rejected, or rejected and should move to the next doc.
    *
    * @see Spans#nextDoc()
    *
    */
-  protected abstract AcceptStatus acceptPosition(Spans spans, SpanCollector collector) throws IOException;
+  protected abstract AcceptStatus acceptPosition(Spans spans) throws IOException;
 
   @Override
-  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores, SpanCollectorFactory factory) throws IOException {
-    SpanWeight matchWeight = match.createWeight(searcher, false, factory);
-    SpanSimilarity similarity = SpanSimilarity.build(this, searcher, needsScores, matchWeight);
-    return new SpanPositionCheckWeight(matchWeight, similarity, factory);
-  }
-
-  public class SpanPositionCheckWeight extends SpanWeight {
-
-    final SpanWeight matchWeight;
-
-    public SpanPositionCheckWeight(SpanWeight matchWeight, SpanSimilarity similarity,
-                                   SpanCollectorFactory collectorFactory) throws IOException {
-      super(SpanPositionCheckQuery.this, similarity, collectorFactory);
-      this.matchWeight = matchWeight;
-    }
-
-    public SpanPositionCheckWeight(SpanWeight matchWeight, SpanSimilarity similarity) throws IOException {
-      this(matchWeight, similarity, SpanCollectorFactory.NO_OP_FACTORY);
-    }
-
-    @Override
-    public void extractTerms(Set<Term> terms) {
-      matchWeight.extractTerms(terms);
-    }
-
-    @Override
-    public void extractTermContexts(Map<Term, TermContext> contexts) {
-      matchWeight.extractTermContexts(contexts);
-    }
-
-    @Override
-    public Spans getSpans(final LeafReaderContext context, Bits acceptDocs, final SpanCollector collector) throws IOException {
-      Spans matchSpans = matchWeight.getSpans(context, acceptDocs, collector);
-      return (matchSpans == null) ? null : new FilterSpans(matchSpans) {
-        @Override
-        protected AcceptStatus accept(Spans candidate) throws IOException {
-          return acceptPosition(candidate, collector);
-        }
-      };
-    }
-
+  public Spans getSpans(final LeafReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException {
+    Spans matchSpans = match.getSpans(context, acceptDocs, termContexts);
+    return (matchSpans == null) ? null : new FilterSpans(matchSpans) {
+      @Override
+      protected AcceptStatus accept(Spans candidate) throws IOException {
+        return acceptPosition(candidate);
+      }
+    };
   }
 
   @Override

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanPositionRangeQuery.java Fri May 22 02:15:34 2015
@@ -40,7 +40,7 @@ public class SpanPositionRangeQuery exte
 
 
   @Override
-  protected AcceptStatus acceptPosition(Spans spans, SpanCollector collector) throws IOException {
+  protected AcceptStatus acceptPosition(Spans spans) throws IOException {
     assert spans.startPosition() != spans.endPosition();
     AcceptStatus res = (spans.startPosition() >= end)
                       ? AcceptStatus.NO_MORE_IN_CURRENT_DOC

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java?rev=1680978&r1=1680977&r2=1680978&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/search/spans/SpanQuery.java Fri May 22 02:15:34 2015
@@ -17,33 +17,43 @@ package org.apache.lucene.search.spans;
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Weight;
-
-import java.io.IOException;
+import org.apache.lucene.util.Bits;
 
 /** Base class for span-based queries. */
 public abstract class SpanQuery extends Query {
+  /** Expert: Returns the matches for this query in an index.  
+   *  Used internally to search for spans.
+   *  This may return null to indicate that the SpanQuery has no results.
+   */
+  public abstract Spans getSpans(LeafReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException;
 
   /**
-   * Returns the name of the field matched by this query.
+   * Extract terms from these spans.
+   * @lucene.internal
+   * @see Weight#extractTerms
    */
-  public abstract String getField();
+  protected abstract void extractTerms(Set<Term> terms);
 
   /**
-   * Create a SpanWeight for this query
-   * @param searcher the IndexSearcher to be searched across
-   * @param needsScores if the query needs scores
-   * @param collectorFactory a SpanCollectorFactory to use in collecting postings data
-   * @return a SpanWeight
-   * @throws IOException on error
+   * Returns the name of the field matched by this query.
+   * <p>
+   * Note that this may return null if the query matches no terms.
    */
-  public abstract SpanWeight createWeight(IndexSearcher searcher, boolean needsScores,
-                                          SpanCollectorFactory collectorFactory) throws IOException;
+  public abstract String getField();
 
   @Override
-  public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
-    return createWeight(searcher, needsScores, SpanCollectorFactory.NO_OP_FACTORY);
+  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
+    return new SpanWeight(this, searcher);
   }
+
 }