You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/12/10 14:43:51 UTC

svn commit: r1719081 [3/4] - in /lucene/dev/trunk: lucene/ lucene/core/src/java/org/apache/lucene/index/ lucene/core/src/java/org/apache/lucene/search/ lucene/core/src/java/org/apache/lucene/search/spans/ lucene/core/src/test/org/apache/lucene/search/ ...

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysScorer.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/DrillSidewaysScorer.java Thu Dec 10 13:43:50 2015
@@ -44,6 +44,7 @@ class DrillSidewaysScorer extends BulkSc
 
   // DrillDown DocsEnums:
   private final Scorer baseScorer;
+  private final DocIdSetIterator baseIterator;
 
   private final LeafReaderContext context;
 
@@ -60,13 +61,14 @@ class DrillSidewaysScorer extends BulkSc
     this.dims = dims;
     this.context = context;
     this.baseScorer = baseScorer;
+    this.baseIterator = baseScorer.iterator();
     this.drillDownCollector = drillDownCollector;
     this.scoreSubDocsAtOnce = scoreSubDocsAtOnce;
   }
 
   @Override
   public long cost() {
-    return baseScorer.cost();
+    return baseIterator.cost();
   }
 
   @Override
@@ -94,13 +96,8 @@ class DrillSidewaysScorer extends BulkSc
       dim.sidewaysLeafCollector.setScorer(scorer);
     }
 
-    // TODO: if we ever allow null baseScorer ... it will
-    // mean we DO score docs out of order ... hmm, or if we
-    // change up the order of the conjuntions below
-    assert baseScorer != null;
-
     // some scorers, eg ReqExlScorer, can hit NPE if cost is called after nextDoc
-    long baseQueryCost = baseScorer.cost();
+    long baseQueryCost = baseIterator.cost();
 
     final int numDims = dims.length;
 
@@ -115,7 +112,7 @@ class DrillSidewaysScorer extends BulkSc
     }
 
     // Position all scorers to their first matching doc:
-    baseScorer.nextDoc();
+    baseIterator.nextDoc();
     for (DocsAndCost dim : dims) {
       dim.approximation.nextDoc();
     }
@@ -157,7 +154,7 @@ class DrillSidewaysScorer extends BulkSc
 
     nextDoc: while (docID != PostingsEnum.NO_MORE_DOCS) {
       if (acceptDocs != null && acceptDocs.get(docID) == false) {
-        docID = baseScorer.nextDoc();
+        docID = baseIterator.nextDoc();
         continue;
       }
       LeafCollector failedCollector = null;
@@ -182,7 +179,7 @@ class DrillSidewaysScorer extends BulkSc
             // More than one dim fails on this document, so
             // it's neither a hit nor a near-miss; move to
             // next doc:
-            docID = baseScorer.nextDoc();
+            docID = baseIterator.nextDoc();
             continue nextDoc;
           } else {
             failedCollector = dim.sidewaysLeafCollector;
@@ -204,7 +201,7 @@ class DrillSidewaysScorer extends BulkSc
         collectNearMiss(failedCollector);
       }
 
-      docID = baseScorer.nextDoc();
+      docID = baseIterator.nextDoc();
     }
   }
 
@@ -316,9 +313,9 @@ class DrillSidewaysScorer extends BulkSc
         int ddDocID = docIDs[slot0];
         assert ddDocID != -1;
 
-        int baseDocID = baseScorer.docID();
+        int baseDocID = baseIterator.docID();
         if (baseDocID < ddDocID) {
-          baseDocID = baseScorer.advance(ddDocID);
+          baseDocID = baseIterator.advance(ddDocID);
         }
         if (baseDocID == ddDocID) {
           //if (DEBUG) {
@@ -437,7 +434,7 @@ class DrillSidewaysScorer extends BulkSc
       //  System.out.println("\ncycle nextChunkStart=" + nextChunkStart + " docIds[0]=" + docIDs[0]);
       //}
       int filledCount = 0;
-      int docID = baseScorer.docID();
+      int docID = baseIterator.docID();
       //if (DEBUG) {
       //  System.out.println("  base docID=" + docID);
       //}
@@ -456,7 +453,7 @@ class DrillSidewaysScorer extends BulkSc
           missingDims[slot] = 0;
           counts[slot] = 1;
         }
-        docID = baseScorer.nextDoc();
+        docID = baseIterator.nextDoc();
       }
 
       if (filledCount == 0) {
@@ -589,11 +586,6 @@ class DrillSidewaysScorer extends BulkSc
     public FakeScorer() {
       super(null);
     }
-    
-    @Override
-    public int advance(int target) {
-      throw new UnsupportedOperationException("FakeScorer doesn't support advance(int)");
-    }
 
     @Override
     public int docID() {
@@ -606,7 +598,7 @@ class DrillSidewaysScorer extends BulkSc
     }
 
     @Override
-    public int nextDoc() {
+    public DocIdSetIterator iterator() {
       throw new UnsupportedOperationException("FakeScorer doesn't support nextDoc()");
     }
     
@@ -616,11 +608,6 @@ class DrillSidewaysScorer extends BulkSc
     }
 
     @Override
-    public long cost() {
-      return baseScorer.cost();
-    }
-
-    @Override
     public Collection<ChildScorer> getChildren() {
       return Collections.singletonList(new ChildScorer(baseScorer, "MUST"));
     }
@@ -640,9 +627,9 @@ class DrillSidewaysScorer extends BulkSc
     LeafCollector sidewaysLeafCollector;
 
     DocsAndCost(Scorer scorer, Collector sidewaysCollector) {
-      final TwoPhaseIterator twoPhase = scorer.asTwoPhaseIterator();
+      final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
       if (twoPhase == null) {
-        this.approximation = scorer;
+        this.approximation = scorer.iterator();
         this.twoPhase = null;
       } else {
         this.approximation = twoPhase.approximation();

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRange.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRange.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRange.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRange.java Thu Dec 10 13:43:50 2015
@@ -164,10 +164,11 @@ public final class DoubleRange extends R
           if (fastMatchWeight == null) {
             approximation = DocIdSetIterator.all(maxDoc);
           } else {
-            approximation = fastMatchWeight.scorer(context);
-            if (approximation == null) {
+            Scorer s = fastMatchWeight.scorer(context);
+            if (s == null) {
               return null;
             }
+            approximation = s.iterator();
           }
 
           final FunctionValues values = valueSource.getValues(Collections.emptyMap(), context);

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRangeFacetCounts.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRangeFacetCounts.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRangeFacetCounts.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/DoubleRangeFacetCounts.java Thu Dec 10 13:43:50 2015
@@ -36,6 +36,7 @@ import org.apache.lucene.search.DocIdSet
 import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Weight;
 import org.apache.lucene.util.NumericUtils;
 
@@ -105,10 +106,11 @@ public class DoubleRangeFacetCounts exte
         final IndexSearcher searcher = new IndexSearcher(topLevelContext);
         searcher.setQueryCache(null);
         final Weight fastMatchWeight = searcher.createNormalizedWeight(fastMatchQuery, false);
-        fastMatchDocs = fastMatchWeight.scorer(hits.context);
-        if (fastMatchDocs == null) {
+        Scorer s = fastMatchWeight.scorer(hits.context);
+        if (s == null) {
           continue;
         }
+        fastMatchDocs = s.iterator();
       } else {
         fastMatchDocs = null;
       }

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRange.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRange.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRange.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRange.java Thu Dec 10 13:43:50 2015
@@ -156,10 +156,11 @@ public final class LongRange extends Ran
           if (fastMatchWeight == null) {
             approximation = DocIdSetIterator.all(maxDoc);
           } else {
-            approximation = fastMatchWeight.scorer(context);
-            if (approximation == null) {
+            Scorer s = fastMatchWeight.scorer(context);
+            if (s == null) {
               return null;
             }
+            approximation = s.iterator();
           }
 
           final FunctionValues values = valueSource.getValues(Collections.emptyMap(), context);

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRangeFacetCounts.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRangeFacetCounts.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRangeFacetCounts.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/range/LongRangeFacetCounts.java Thu Dec 10 13:43:50 2015
@@ -33,6 +33,7 @@ import org.apache.lucene.search.DocIdSet
 import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Weight;
 
 /** {@link Facets} implementation that computes counts for
@@ -86,10 +87,11 @@ public class LongRangeFacetCounts extend
         final IndexSearcher searcher = new IndexSearcher(topLevelContext);
         searcher.setQueryCache(null);
         final Weight fastMatchWeight = searcher.createNormalizedWeight(fastMatchQuery, false);
-        fastMatchDocs = fastMatchWeight.scorer(hits.context);
-        if (fastMatchDocs == null) {
+        Scorer s = fastMatchWeight.scorer(hits.context);
+        if (s == null) {
           continue;
         }
+        fastMatchDocs = s.iterator();
       } else {
         fastMatchDocs = null;
       }

Modified: lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FakeScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FakeScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FakeScorer.java (original)
+++ lucene/dev/trunk/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FakeScorer.java Thu Dec 10 13:43:50 2015
@@ -19,6 +19,7 @@ package org.apache.lucene.facet.taxonomy
 
 import java.io.IOException;
 
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.Scorer;
 
 class FakeScorer extends Scorer {
@@ -37,17 +38,7 @@ class FakeScorer extends Scorer {
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public int advance(int target) throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public long cost() {
+  public DocIdSetIterator iterator() {
     throw new UnsupportedOperationException();
   }
 

Modified: lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java (original)
+++ lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/BlockGroupingCollector.java Thu Dec 10 13:43:50 2015
@@ -478,7 +478,12 @@ public class BlockGroupingCollector exte
     subDocUpto = 0;
     docBase = readerContext.docBase;
     //System.out.println("setNextReader base=" + docBase + " r=" + readerContext.reader);
-    lastDocPerGroupBits = lastDocPerGroup.scorer(readerContext);
+    Scorer s = lastDocPerGroup.scorer(readerContext);
+    if (s == null) {
+      lastDocPerGroupBits = null;
+    } else {
+      lastDocPerGroupBits = s.iterator();
+    }
     groupEndDocID = -1;
 
     currentReaderContext = readerContext;

Modified: lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/FakeScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/FakeScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/FakeScorer.java (original)
+++ lucene/dev/trunk/lucene/grouping/src/java/org/apache/lucene/search/grouping/FakeScorer.java Thu Dec 10 13:43:50 2015
@@ -19,6 +19,7 @@ package org.apache.lucene.search.groupin
 
 import java.io.IOException;
 
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.Scorer;
 
 class FakeScorer extends Scorer {
@@ -37,17 +38,7 @@ class FakeScorer extends Scorer {
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public int advance(int target) throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public long cost() {
+  public DocIdSetIterator iterator() {
     throw new UnsupportedOperationException();
   }
 

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/BaseGlobalOrdinalScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/BaseGlobalOrdinalScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/BaseGlobalOrdinalScorer.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/BaseGlobalOrdinalScorer.java Thu Dec 10 13:43:50 2015
@@ -22,21 +22,20 @@ import org.apache.lucene.search.DocIdSet
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.TwoPhaseIterator;
 import org.apache.lucene.search.Weight;
-import org.apache.lucene.util.LongBitSet;
 
 import java.io.IOException;
 
 abstract class BaseGlobalOrdinalScorer extends Scorer {
 
   final SortedDocValues values;
-  final Scorer approximationScorer;
+  final DocIdSetIterator approximation;
 
   float score;
 
-  public BaseGlobalOrdinalScorer(Weight weight, SortedDocValues values, Scorer approximationScorer) {
+  public BaseGlobalOrdinalScorer(Weight weight, SortedDocValues values, DocIdSetIterator approximationScorer) {
     super(weight);
     this.values = values;
-    this.approximationScorer = approximationScorer;
+    this.approximation = approximationScorer;
   }
 
   @Override
@@ -46,46 +45,20 @@ abstract class BaseGlobalOrdinalScorer e
 
   @Override
   public int docID() {
-    return approximationScorer.docID();
+    return approximation.docID();
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    return advance(approximationScorer.docID() + 1);
+  public DocIdSetIterator iterator() {
+    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
   }
 
   @Override
-  public TwoPhaseIterator asTwoPhaseIterator() {
-    final DocIdSetIterator approximation = new DocIdSetIterator() {
-      @Override
-      public int docID() {
-        return approximationScorer.docID();
-      }
-
-      @Override
-      public int nextDoc() throws IOException {
-        return approximationScorer.nextDoc();
-      }
-
-      @Override
-      public int advance(int target) throws IOException {
-        return approximationScorer.advance(target);
-      }
-
-      @Override
-      public long cost() {
-        return approximationScorer.cost();
-      }
-    };
+  public TwoPhaseIterator twoPhaseIterator() {
     return createTwoPhaseIterator(approximation);
   }
 
   @Override
-  public long cost() {
-    return approximationScorer.cost();
-  }
-
-  @Override
   public int freq() throws IOException {
     return 1;
   }

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/FakeScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/FakeScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/FakeScorer.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/FakeScorer.java Thu Dec 10 13:43:50 2015
@@ -19,6 +19,7 @@ package org.apache.lucene.search.join;
 
 import java.io.IOException;
 
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.Scorer;
 
 class FakeScorer extends Scorer {
@@ -37,17 +38,7 @@ class FakeScorer extends Scorer {
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public int advance(int target) throws IOException {
-    throw new UnsupportedOperationException();
-  }
-
-  @Override
-  public long cost() {
+  public DocIdSetIterator iterator() {
     throw new UnsupportedOperationException();
   }
 

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsQuery.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsQuery.java Thu Dec 10 13:43:50 2015
@@ -135,9 +135,9 @@ final class GlobalOrdinalsQuery extends
         return null;
       }
       if (globalOrds != null) {
-        return new OrdinalMapScorer(this, score(), foundOrds, values, approximationScorer, globalOrds.getGlobalOrds(context.ord));
+        return new OrdinalMapScorer(this, score(), foundOrds, values, approximationScorer.iterator(), globalOrds.getGlobalOrds(context.ord));
       } {
-        return new SegmentOrdinalScorer(this, score(), foundOrds, values, approximationScorer);
+        return new SegmentOrdinalScorer(this, score(), foundOrds, values, approximationScorer.iterator());
       }
     }
 
@@ -148,7 +148,7 @@ final class GlobalOrdinalsQuery extends
     final LongBitSet foundOrds;
     final LongValues segmentOrdToGlobalOrdLookup;
 
-    public OrdinalMapScorer(Weight weight, float score, LongBitSet foundOrds, SortedDocValues values, Scorer approximationScorer, LongValues segmentOrdToGlobalOrdLookup) {
+    public OrdinalMapScorer(Weight weight, float score, LongBitSet foundOrds, SortedDocValues values, DocIdSetIterator approximationScorer, LongValues segmentOrdToGlobalOrdLookup) {
       super(weight, values, approximationScorer);
       this.score = score;
       this.foundOrds = foundOrds;
@@ -156,26 +156,12 @@ final class GlobalOrdinalsQuery extends
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      for (int docID = approximationScorer.advance(target); docID < NO_MORE_DOCS; docID = approximationScorer.nextDoc()) {
-        final long segmentOrd = values.getOrd(docID);
-        if (segmentOrd != -1) {
-          final long globalOrd = segmentOrdToGlobalOrdLookup.get(segmentOrd);
-          if (foundOrds.get(globalOrd)) {
-            return docID;
-          }
-        }
-      }
-      return NO_MORE_DOCS;
-    }
-
-    @Override
     protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
       return new TwoPhaseIterator(approximation) {
 
         @Override
         public boolean matches() throws IOException {
-          final long segmentOrd = values.getOrd(approximationScorer.docID());
+          final long segmentOrd = values.getOrd(approximation.docID());
           if (segmentOrd != -1) {
             final long globalOrd = segmentOrdToGlobalOrdLookup.get(segmentOrd);
             if (foundOrds.get(globalOrd)) {
@@ -197,32 +183,19 @@ final class GlobalOrdinalsQuery extends
 
     final LongBitSet foundOrds;
 
-    public SegmentOrdinalScorer(Weight weight, float score, LongBitSet foundOrds, SortedDocValues values, Scorer approximationScorer) {
+    public SegmentOrdinalScorer(Weight weight, float score, LongBitSet foundOrds, SortedDocValues values, DocIdSetIterator approximationScorer) {
       super(weight, values, approximationScorer);
       this.score = score;
       this.foundOrds = foundOrds;
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      for (int docID = approximationScorer.advance(target); docID < NO_MORE_DOCS; docID = approximationScorer.nextDoc()) {
-        final long segmentOrd = values.getOrd(docID);
-        if (segmentOrd != -1) {
-          if (foundOrds.get(segmentOrd)) {
-            return docID;
-          }
-        }
-      }
-      return NO_MORE_DOCS;
-    }
-
-    @Override
     protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
       return new TwoPhaseIterator(approximation) {
 
         @Override
         public boolean matches() throws IOException {
-          final long segmentOrd = values.getOrd(approximationScorer.docID());
+          final long segmentOrd = values.getOrd(approximation.docID());
           if (segmentOrd != -1) {
             if (foundOrds.get(segmentOrd)) {
               return true;

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java Thu Dec 10 13:43:50 2015
@@ -160,9 +160,9 @@ final class GlobalOrdinalsWithScoreQuery
       if (approximationScorer == null) {
         return null;
       } else if (globalOrds != null) {
-        return new OrdinalMapScorer(this, collector, values, approximationScorer, globalOrds.getGlobalOrds(context.ord));
+        return new OrdinalMapScorer(this, collector, values, approximationScorer.iterator(), globalOrds.getGlobalOrds(context.ord));
       } else {
-        return new SegmentOrdinalScorer(this, collector, values, approximationScorer);
+        return new SegmentOrdinalScorer(this, collector, values, approximationScorer.iterator());
       }
     }
 
@@ -173,34 +173,19 @@ final class GlobalOrdinalsWithScoreQuery
     final LongValues segmentOrdToGlobalOrdLookup;
     final GlobalOrdinalsWithScoreCollector collector;
 
-    public OrdinalMapScorer(Weight weight, GlobalOrdinalsWithScoreCollector collector, SortedDocValues values, Scorer approximationScorer, LongValues segmentOrdToGlobalOrdLookup) {
-      super(weight, values, approximationScorer);
+    public OrdinalMapScorer(Weight weight, GlobalOrdinalsWithScoreCollector collector, SortedDocValues values, DocIdSetIterator approximation, LongValues segmentOrdToGlobalOrdLookup) {
+      super(weight, values, approximation);
       this.segmentOrdToGlobalOrdLookup = segmentOrdToGlobalOrdLookup;
       this.collector = collector;
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      for (int docID = approximationScorer.advance(target); docID < NO_MORE_DOCS; docID = approximationScorer.nextDoc()) {
-        final long segmentOrd = values.getOrd(docID);
-        if (segmentOrd != -1) {
-          final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(segmentOrd);
-          if (collector.match(globalOrd)) {
-            score = collector.score(globalOrd);
-            return docID;
-          }
-        }
-      }
-      return NO_MORE_DOCS;
-    }
-
-    @Override
     protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
       return new TwoPhaseIterator(approximation) {
 
         @Override
         public boolean matches() throws IOException {
-          final long segmentOrd = values.getOrd(approximationScorer.docID());
+          final long segmentOrd = values.getOrd(approximation.docID());
           if (segmentOrd != -1) {
             final int globalOrd = (int) segmentOrdToGlobalOrdLookup.get(segmentOrd);
             if (collector.match(globalOrd)) {
@@ -223,32 +208,18 @@ final class GlobalOrdinalsWithScoreQuery
 
     final GlobalOrdinalsWithScoreCollector collector;
 
-    public SegmentOrdinalScorer(Weight weight, GlobalOrdinalsWithScoreCollector collector, SortedDocValues values, Scorer approximationScorer) {
-      super(weight, values, approximationScorer);
+    public SegmentOrdinalScorer(Weight weight, GlobalOrdinalsWithScoreCollector collector, SortedDocValues values, DocIdSetIterator approximation) {
+      super(weight, values, approximation);
       this.collector = collector;
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      for (int docID = approximationScorer.advance(target); docID < NO_MORE_DOCS; docID = approximationScorer.nextDoc()) {
-        final int segmentOrd = values.getOrd(docID);
-        if (segmentOrd != -1) {
-          if (collector.match(segmentOrd)) {
-            score = collector.score(segmentOrd);
-            return docID;
-          }
-        }
-      }
-      return NO_MORE_DOCS;
-    }
-
-    @Override
     protected TwoPhaseIterator createTwoPhaseIterator(DocIdSetIterator approximation) {
       return new TwoPhaseIterator(approximation) {
 
         @Override
         public boolean matches() throws IOException {
-          final int segmentOrd = values.getOrd(approximationScorer.docID());
+          final int segmentOrd = values.getOrd(approximation.docID());
           if (segmentOrd != -1) {
             if (collector.match(segmentOrd)) {
               score = collector.score(segmentOrd);

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/QueryBitSetProducer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/QueryBitSetProducer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/QueryBitSetProducer.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/QueryBitSetProducer.java Thu Dec 10 13:43:50 2015
@@ -27,9 +27,9 @@ import org.apache.lucene.index.LeafReade
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.ReaderUtil;
 import org.apache.lucene.search.DocIdSet;
-import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Weight;
 import org.apache.lucene.util.BitDocIdSet;
 import org.apache.lucene.util.BitSet;
@@ -68,12 +68,12 @@ public class QueryBitSetProducer impleme
       final IndexSearcher searcher = new IndexSearcher(topLevelContext);
       searcher.setQueryCache(null);
       final Weight weight = searcher.createNormalizedWeight(query, false);
-      final DocIdSetIterator it = weight.scorer(context);
+      final Scorer s = weight.scorer(context);
 
-      if (it == null) {
+      if (s == null) {
         docIdSet = DocIdSet.EMPTY;
       } else {
-        docIdSet = new BitDocIdSet(BitSet.of(it, context.reader().maxDoc()));
+        docIdSet = new BitDocIdSet(BitSet.of(s.iterator(), context.reader().maxDoc()));
       }
       cache.put(key, docIdSet);
     }

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java Thu Dec 10 13:43:50 2015
@@ -180,8 +180,6 @@ class TermsIncludingScoreQuery extends Q
     final float[] scores;
     final long cost;
 
-    int currentDoc = -1;
-
     SVInOrderScorer(Weight weight, TermsEnum termsEnum, int maxDoc, long cost) throws IOException {
       super(weight);
       FixedBitSet matchingDocs = new FixedBitSet(maxDoc);
@@ -210,7 +208,7 @@ class TermsIncludingScoreQuery extends Q
 
     @Override
     public float score() throws IOException {
-      return scores[currentDoc];
+      return scores[docID()];
     }
 
     @Override
@@ -220,23 +218,14 @@ class TermsIncludingScoreQuery extends Q
 
     @Override
     public int docID() {
-      return currentDoc;
-    }
-
-    @Override
-    public int nextDoc() throws IOException {
-      return currentDoc = matchingDocsIterator.nextDoc();
+      return matchingDocsIterator.docID();
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      return currentDoc = matchingDocsIterator.advance(target);
+    public DocIdSetIterator iterator() {
+      return matchingDocsIterator;
     }
 
-    @Override
-    public long cost() {
-      return cost;
-    }
   }
 
   // This scorer deals with the fact that a document can have more than one score from multiple related documents.

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java Thu Dec 10 13:43:50 2015
@@ -26,6 +26,7 @@ import java.util.Set;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
@@ -141,7 +142,7 @@ public class ToChildBlockJoinQuery exten
     @Override
     public Explanation explain(LeafReaderContext context, int doc) throws IOException {
       ToChildBlockJoinScorer scorer = (ToChildBlockJoinScorer) scorer(context);
-      if (scorer != null && scorer.advance(doc) == doc) {
+      if (scorer != null && scorer.iterator().advance(doc) == doc) {
         int parentDoc = scorer.getParentDoc();
         return Explanation.match(
           scorer.score(), 
@@ -155,6 +156,7 @@ public class ToChildBlockJoinQuery exten
 
   static class ToChildBlockJoinScorer extends Scorer {
     private final Scorer parentScorer;
+    private final DocIdSetIterator parentIt;
     private final BitSet parentBits;
     private final boolean doScores;
 
@@ -169,6 +171,7 @@ public class ToChildBlockJoinQuery exten
       this.doScores = doScores;
       this.parentBits = parentBits;
       this.parentScorer = parentScorer;
+      this.parentIt = parentScorer.iterator();
     }
 
     @Override
@@ -177,69 +180,127 @@ public class ToChildBlockJoinQuery exten
     }
 
     @Override
-    public int nextDoc() throws IOException {
-      //System.out.println("Q.nextDoc() parentDoc=" + parentDoc + " childDoc=" + childDoc);
+    public DocIdSetIterator iterator() {
+      return new DocIdSetIterator() {
 
-      while (true) {
-        if (childDoc+1 == parentDoc) {
-          // OK, we are done iterating through all children
-          // matching this one parent doc, so we now nextDoc()
-          // the parent.  Use a while loop because we may have
-          // to skip over some number of parents w/ no
-          // children:
-          while (true) {
-            parentDoc = parentScorer.nextDoc();
-            validateParentDoc();
+        @Override
+        public int docID() {
+          return childDoc;
+        }
 
-            if (parentDoc == 0) {
-              // Degenerate but allowed: first parent doc has no children
-              // TODO: would be nice to pull initial parent
-              // into ctor so we can skip this if... but it's
-              // tricky because scorer must return -1 for
-              // .doc() on init...
-              parentDoc = parentScorer.nextDoc();
-              validateParentDoc();
-            }
+        @Override
+        public int nextDoc() throws IOException {
+          //System.out.println("Q.nextDoc() parentDoc=" + parentDoc + " childDoc=" + childDoc);
 
-            if (parentDoc == NO_MORE_DOCS) {
-              childDoc = NO_MORE_DOCS;
-              //System.out.println("  END");
+          while (true) {
+            if (childDoc+1 == parentDoc) {
+              // OK, we are done iterating through all children
+              // matching this one parent doc, so we now nextDoc()
+              // the parent.  Use a while loop because we may have
+              // to skip over some number of parents w/ no
+              // children:
+              while (true) {
+                parentDoc = parentIt.nextDoc();
+                validateParentDoc();
+
+                if (parentDoc == 0) {
+                  // Degenerate but allowed: first parent doc has no children
+                  // TODO: would be nice to pull initial parent
+                  // into ctor so we can skip this if... but it's
+                  // tricky because scorer must return -1 for
+                  // .doc() on init...
+                  parentDoc = parentIt.nextDoc();
+                  validateParentDoc();
+                }
+
+                if (parentDoc == NO_MORE_DOCS) {
+                  childDoc = NO_MORE_DOCS;
+                  //System.out.println("  END");
+                  return childDoc;
+                }
+
+                // Go to first child for this next parentDoc:
+                childDoc = 1 + parentBits.prevSetBit(parentDoc-1);
+
+                if (childDoc == parentDoc) {
+                  // This parent has no children; continue
+                  // parent loop so we move to next parent
+                  continue;
+                }
+
+                if (childDoc < parentDoc) {
+                  if (doScores) {
+                    parentScore = parentScorer.score();
+                    parentFreq = parentScorer.freq();
+                  }
+                  //System.out.println("  " + childDoc);
+                  return childDoc;
+                } else {
+                  // Degenerate but allowed: parent has no children
+                }
+              }
+            } else {
+              assert childDoc < parentDoc: "childDoc=" + childDoc + " parentDoc=" + parentDoc;
+              childDoc++;
+              //System.out.println("  " + childDoc);
               return childDoc;
             }
+          }
+        }
 
-            // Go to first child for this next parentDoc:
-            childDoc = 1 + parentBits.prevSetBit(parentDoc-1);
+        @Override
+        public int advance(int childTarget) throws IOException {
+          if (childTarget >= parentDoc) {
+            if (childTarget == NO_MORE_DOCS) {
+              return childDoc = parentDoc = NO_MORE_DOCS;
+            }
+            parentDoc = parentIt.advance(childTarget + 1);
+            validateParentDoc();
 
-            if (childDoc == parentDoc) {
-              // This parent has no children; continue
-              // parent loop so we move to next parent
-              continue;
+            if (parentDoc == NO_MORE_DOCS) {
+              return childDoc = NO_MORE_DOCS;
             }
 
-            if (childDoc < parentDoc) {
-              if (doScores) {
-                parentScore = parentScorer.score();
-                parentFreq = parentScorer.freq();
+            // scan to the first parent that has children
+            while (true) {
+              final int firstChild = parentBits.prevSetBit(parentDoc-1) + 1;
+              if (firstChild != parentDoc) {
+                // this parent has children
+                childTarget = Math.max(childTarget, firstChild);
+                break;
               }
-              //System.out.println("  " + childDoc);
-              return childDoc;
-            } else {
-              // Degenerate but allowed: parent has no children
+              // parent with no children, move to the next one
+              parentDoc = parentIt.nextDoc();
+              validateParentDoc();
+              if (parentDoc == NO_MORE_DOCS) {
+                return childDoc = NO_MORE_DOCS;
+              }
+            }
+
+            if (doScores) {
+              parentScore = parentScorer.score();
+              parentFreq = parentScorer.freq();
             }
           }
-        } else {
-          assert childDoc < parentDoc: "childDoc=" + childDoc + " parentDoc=" + parentDoc;
-          childDoc++;
+
+          assert childTarget < parentDoc;
+          assert !parentBits.get(childTarget);
+          childDoc = childTarget;
           //System.out.println("  " + childDoc);
           return childDoc;
         }
-      }
+
+        @Override
+        public long cost() {
+          return parentIt.cost();
+        }
+      };
     }
 
     /** Detect mis-use, where provided parent query in fact
      *  sometimes returns child documents.  */
     private void validateParentDoc() {
-      if (parentDoc != NO_MORE_DOCS && !parentBits.get(parentDoc)) {
+      if (parentDoc != DocIdSetIterator.NO_MORE_DOCS && !parentBits.get(parentDoc)) {
         throw new IllegalStateException(INVALID_QUERY_MESSAGE + parentDoc);
       }
     }
@@ -258,53 +319,6 @@ public class ToChildBlockJoinQuery exten
     public int freq() throws IOException {
       return parentFreq;
     }
-
-    @Override
-    public int advance(int childTarget) throws IOException {
-      if (childTarget >= parentDoc) {
-        if (childTarget == NO_MORE_DOCS) {
-          return childDoc = parentDoc = NO_MORE_DOCS;
-        }
-        parentDoc = parentScorer.advance(childTarget + 1);
-        validateParentDoc();
-
-        if (parentDoc == NO_MORE_DOCS) {
-          return childDoc = NO_MORE_DOCS;
-        }
-
-        // scan to the first parent that has children
-        while (true) {
-          final int firstChild = parentBits.prevSetBit(parentDoc-1) + 1;
-          if (firstChild != parentDoc) {
-            // this parent has children
-            childTarget = Math.max(childTarget, firstChild);
-            break;
-          }
-          // parent with no children, move to the next one
-          parentDoc = parentScorer.nextDoc();
-          validateParentDoc();
-          if (parentDoc == NO_MORE_DOCS) {
-            return childDoc = NO_MORE_DOCS;
-          }
-        }
-
-        if (doScores) {
-          parentScore = parentScorer.score();
-          parentFreq = parentScorer.freq();
-        }
-      }
-
-      assert childTarget < parentDoc;
-      assert !parentBits.get(childTarget);
-      childDoc = childTarget;
-      //System.out.println("  " + childDoc);
-      return childDoc;
-    }
-
-    @Override
-    public long cost() {
-      return parentScorer.cost();
-    }
     
     int getParentDoc() {
       return parentDoc;

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinIndexSearcher.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinIndexSearcher.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinIndexSearcher.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinIndexSearcher.java Thu Dec 10 13:43:50 2015
@@ -61,7 +61,8 @@ public class ToParentBlockJoinIndexSearc
         final LeafCollector leafCollector = collector.getLeafCollector(ctx);
         leafCollector.setScorer(scorer);
         final Bits liveDocs = ctx.reader().getLiveDocs();
-        for (int doc = scorer.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = scorer.nextDoc()) {
+        final DocIdSetIterator it = scorer.iterator();
+        for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {
           if (liveDocs == null || liveDocs.get(doc)) {
             leafCollector.collect(doc);
           }

Modified: lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java (original)
+++ lucene/dev/trunk/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java Thu Dec 10 13:43:50 2015
@@ -163,7 +163,7 @@ public class ToParentBlockJoinQuery exte
         return null;
       }
 
-      final int firstChildDoc = childScorer.nextDoc();
+      final int firstChildDoc = childScorer.iterator().nextDoc();
       if (firstChildDoc == DocIdSetIterator.NO_MORE_DOCS) {
         // No matches
         return null;
@@ -184,7 +184,7 @@ public class ToParentBlockJoinQuery exte
     @Override
     public Explanation explain(LeafReaderContext context, int doc) throws IOException {
       BlockJoinScorer scorer = (BlockJoinScorer) scorer(context);
-      if (scorer != null && scorer.advance(doc) == doc) {
+      if (scorer != null && scorer.iterator().advance(doc) == doc) {
         return scorer.explain(context.docBase);
       }
       return Explanation.noMatch("Not a match");
@@ -250,86 +250,142 @@ public class ToParentBlockJoinQuery exte
     }
 
     @Override
-    public int nextDoc() throws IOException {
-      //System.out.println("Q.nextDoc() nextChildDoc=" + nextChildDoc);
-      if (nextChildDoc == NO_MORE_DOCS) {
-        //System.out.println("  end");
-        return parentDoc = NO_MORE_DOCS;
-      }
+    public DocIdSetIterator iterator() {
+      return new DocIdSetIterator() {
+        final DocIdSetIterator childIt = childScorer.iterator();
+
+        @Override
+        public int nextDoc() throws IOException {
+          //System.out.println("Q.nextDoc() nextChildDoc=" + nextChildDoc);
+          if (nextChildDoc == NO_MORE_DOCS) {
+            //System.out.println("  end");
+            return parentDoc = NO_MORE_DOCS;
+          }
 
-      // Gather all children sharing the same parent as
-      // nextChildDoc
+          // Gather all children sharing the same parent as
+          // nextChildDoc
 
-      parentDoc = parentBits.nextSetBit(nextChildDoc);
+          parentDoc = parentBits.nextSetBit(nextChildDoc);
 
-      // Parent & child docs are supposed to be
-      // orthogonal:
-      if (nextChildDoc == parentDoc) {
-        throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
-      }
+          // Parent & child docs are supposed to be
+          // orthogonal:
+          if (nextChildDoc == parentDoc) {
+            throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
+          }
 
-      //System.out.println("  parentDoc=" + parentDoc);
-      assert parentDoc != DocIdSetIterator.NO_MORE_DOCS;
+          //System.out.println("  parentDoc=" + parentDoc);
+          assert parentDoc != DocIdSetIterator.NO_MORE_DOCS;
 
-      float totalScore = 0;
-      float maxScore = Float.NEGATIVE_INFINITY;
-      float minScore = Float.POSITIVE_INFINITY;
+          float totalScore = 0;
+          float maxScore = Float.NEGATIVE_INFINITY;
+          float minScore = Float.POSITIVE_INFINITY;
+
+          childDocUpto = 0;
+          parentFreq = 0;
+          do {
+
+            //System.out.println("  c=" + nextChildDoc);
+            if (pendingChildDocs != null && pendingChildDocs.length == childDocUpto) {
+              pendingChildDocs = ArrayUtil.grow(pendingChildDocs);
+            }
+            if (pendingChildScores != null && scoreMode != ScoreMode.None && pendingChildScores.length == childDocUpto) {
+              pendingChildScores = ArrayUtil.grow(pendingChildScores);
+            }
+            if (pendingChildDocs != null) {
+              pendingChildDocs[childDocUpto] = nextChildDoc;
+            }
+            if (scoreMode != ScoreMode.None) {
+              // TODO: specialize this into dedicated classes per-scoreMode
+              final float childScore = childScorer.score();
+              final int childFreq = childScorer.freq();
+              if (pendingChildScores != null) {
+                pendingChildScores[childDocUpto] = childScore;
+              }
+              maxScore = Math.max(childScore, maxScore);
+              minScore = Math.min(childScore, minScore);
+              totalScore += childScore;
+              parentFreq += childFreq;
+            }
+            childDocUpto++;
+            nextChildDoc = childIt.nextDoc();
+          } while (nextChildDoc < parentDoc);
+
+          // Parent & child docs are supposed to be
+          // orthogonal:
+          if (nextChildDoc == parentDoc) {
+            throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
+          }
 
-      childDocUpto = 0;
-      parentFreq = 0;
-      do {
+          switch(scoreMode) {
+          case Avg:
+            parentScore = totalScore / childDocUpto;
+            break;
+          case Max:
+            parentScore = maxScore;
+            break;
+          case Min:
+            parentScore = minScore;
+            break;
+          case Total:
+            parentScore = totalScore;
+            break;
+          case None:
+            break;
+          }
 
-        //System.out.println("  c=" + nextChildDoc);
-        if (pendingChildDocs != null && pendingChildDocs.length == childDocUpto) {
-          pendingChildDocs = ArrayUtil.grow(pendingChildDocs);
-        }
-        if (pendingChildScores != null && scoreMode != ScoreMode.None && pendingChildScores.length == childDocUpto) {
-          pendingChildScores = ArrayUtil.grow(pendingChildScores);
+          //System.out.println("  return parentDoc=" + parentDoc + " childDocUpto=" + childDocUpto);
+          return parentDoc;
         }
-        if (pendingChildDocs != null) {
-          pendingChildDocs[childDocUpto] = nextChildDoc;
+
+        @Override
+        public int advance(int parentTarget) throws IOException {
+
+          //System.out.println("Q.advance parentTarget=" + parentTarget);
+          if (parentTarget == NO_MORE_DOCS) {
+            return parentDoc = NO_MORE_DOCS;
+          }
+
+          if (parentTarget == 0) {
+            // Callers should only be passing in a docID from
+            // the parent space, so this means this parent
+            // has no children (it got docID 0), so it cannot
+            // possibly match.  We must handle this case
+            // separately otherwise we pass invalid -1 to
+            // prevSetBit below:
+            return nextDoc();
+          }
+
+          prevParentDoc = parentBits.prevSetBit(parentTarget-1);
+
+          //System.out.println("  rolled back to prevParentDoc=" + prevParentDoc + " vs parentDoc=" + parentDoc);
+          assert prevParentDoc >= parentDoc;
+          if (prevParentDoc > nextChildDoc) {
+            nextChildDoc = childIt.advance(prevParentDoc);
+            // System.out.println("  childScorer advanced to child docID=" + nextChildDoc);
+          //} else {
+            //System.out.println("  skip childScorer advance");
+          }
+
+          // Parent & child docs are supposed to be orthogonal:
+          if (nextChildDoc == prevParentDoc) {
+            throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
+          }
+
+          final int nd = nextDoc();
+          //System.out.println("  return nextParentDoc=" + nd);
+          return nd;
         }
-        if (scoreMode != ScoreMode.None) {
-          // TODO: specialize this into dedicated classes per-scoreMode
-          final float childScore = childScorer.score();
-          final int childFreq = childScorer.freq();
-          if (pendingChildScores != null) {
-            pendingChildScores[childDocUpto] = childScore;
-          }
-          maxScore = Math.max(childScore, maxScore);
-          minScore = Math.min(childScore, minScore);
-          totalScore += childScore;
-          parentFreq += childFreq;
+
+        @Override
+        public int docID() {
+          return parentDoc;
         }
-        childDocUpto++;
-        nextChildDoc = childScorer.nextDoc();
-      } while (nextChildDoc < parentDoc);
-
-      // Parent & child docs are supposed to be
-      // orthogonal:
-      if (nextChildDoc == parentDoc) {
-        throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
-      }
-
-      switch(scoreMode) {
-      case Avg:
-        parentScore = totalScore / childDocUpto;
-        break;
-      case Max:
-        parentScore = maxScore;
-        break;
-      case Min:
-        parentScore = minScore;
-        break;
-      case Total:
-        parentScore = totalScore;
-        break;
-      case None:
-        break;
-      }
 
-      //System.out.println("  return parentDoc=" + parentDoc + " childDocUpto=" + childDocUpto);
-      return parentDoc;
+        @Override
+        public long cost() {
+          return childIt.cost();
+        }
+      };
     }
 
     @Override
@@ -347,45 +403,6 @@ public class ToParentBlockJoinQuery exte
       return parentFreq;
     }
 
-    @Override
-    public int advance(int parentTarget) throws IOException {
-
-      //System.out.println("Q.advance parentTarget=" + parentTarget);
-      if (parentTarget == NO_MORE_DOCS) {
-        return parentDoc = NO_MORE_DOCS;
-      }
-
-      if (parentTarget == 0) {
-        // Callers should only be passing in a docID from
-        // the parent space, so this means this parent
-        // has no children (it got docID 0), so it cannot
-        // possibly match.  We must handle this case
-        // separately otherwise we pass invalid -1 to
-        // prevSetBit below:
-        return nextDoc();
-      }
-
-      prevParentDoc = parentBits.prevSetBit(parentTarget-1);
-
-      //System.out.println("  rolled back to prevParentDoc=" + prevParentDoc + " vs parentDoc=" + parentDoc);
-      assert prevParentDoc >= parentDoc;
-      if (prevParentDoc > nextChildDoc) {
-        nextChildDoc = childScorer.advance(prevParentDoc);
-        // System.out.println("  childScorer advanced to child docID=" + nextChildDoc);
-      //} else {
-        //System.out.println("  skip childScorer advance");
-      }
-
-      // Parent & child docs are supposed to be orthogonal:
-      if (nextChildDoc == prevParentDoc) {
-        throw new IllegalStateException("child query must only match non-parent docs, but parent docID=" + nextChildDoc + " matched childScorer=" + childScorer.getClass());
-      }
-
-      final int nd = nextDoc();
-      //System.out.println("  return nextParentDoc=" + nd);
-      return nd;
-    }
-
     public Explanation explain(int docBase) throws IOException {
       int start = docBase + prevParentDoc + 1; // +1 b/c prevParentDoc is previous parent doc
       int end = docBase + parentDoc - 1; // -1 b/c parentDoc is parent doc
@@ -393,11 +410,6 @@ public class ToParentBlockJoinQuery exte
       );
     }
 
-    @Override
-    public long cost() {
-      return childScorer.cost();
-    }
-
     /**
      * Instructs this scorer to keep track of the child docIds and score ids for retrieval purposes.
      */

Modified: lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java (original)
+++ lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java Thu Dec 10 13:43:50 2015
@@ -63,6 +63,7 @@ import org.apache.lucene.search.Query;
 import org.apache.lucene.search.QueryUtils;
 import org.apache.lucene.search.RandomApproximationQuery;
 import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TermQuery;
@@ -1143,8 +1144,8 @@ public class TestBlockJoin extends Lucen
 
     ToParentBlockJoinQuery q = new ToParentBlockJoinQuery(tq, parentFilter, ScoreMode.Avg);
     Weight weight = s.createNormalizedWeight(q, true);
-    DocIdSetIterator disi = weight.scorer(s.getIndexReader().leaves().get(0));
-    assertEquals(1, disi.advance(1));
+    Scorer sc = weight.scorer(s.getIndexReader().leaves().get(0));
+    assertEquals(1, sc.iterator().advance(1));
     r.close();
     dir.close();
   }
@@ -1177,8 +1178,8 @@ public class TestBlockJoin extends Lucen
 
     ToParentBlockJoinQuery q = new ToParentBlockJoinQuery(tq, parentFilter, ScoreMode.Avg);
     Weight weight = s.createNormalizedWeight(q, true);
-    DocIdSetIterator disi = weight.scorer(s.getIndexReader().leaves().get(0));
-    assertEquals(2, disi.advance(0));
+    Scorer sc = weight.scorer(s.getIndexReader().leaves().get(0));
+    assertEquals(2, sc.iterator().advance(0));
     r.close();
     dir.close();
   }
@@ -1657,12 +1658,12 @@ public class TestBlockJoin extends Lucen
     ToChildBlockJoinQuery parentJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentFilter);
 
     Weight weight = s.createNormalizedWeight(parentJoinQuery, random().nextBoolean());
-    DocIdSetIterator advancingScorer = weight.scorer(s.getIndexReader().leaves().get(0));
-    DocIdSetIterator nextDocScorer = weight.scorer(s.getIndexReader().leaves().get(0));
+    Scorer advancingScorer = weight.scorer(s.getIndexReader().leaves().get(0));
+    Scorer nextDocScorer = weight.scorer(s.getIndexReader().leaves().get(0));
 
-    final int firstKid = nextDocScorer.nextDoc();
+    final int firstKid = nextDocScorer.iterator().nextDoc();
     assertTrue("firstKid not found", DocIdSetIterator.NO_MORE_DOCS != firstKid);
-    assertEquals(firstKid, advancingScorer.advance(0));
+    assertEquals(firstKid, advancingScorer.iterator().advance(0));
     
     r.close();
     dir.close();

Modified: lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java (original)
+++ lucene/dev/trunk/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java Thu Dec 10 13:43:50 2015
@@ -140,7 +140,7 @@ public class TestBlockJoinValidation ext
     } while (parentDocs.get(target + 1));
 
     try {
-      scorer.advance(target);
+      scorer.iterator().advance(target);
       fail();
     } catch (IllegalStateException expected) {
       assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));

Modified: lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/PKIndexSplitter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/PKIndexSplitter.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/PKIndexSplitter.java (original)
+++ lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/PKIndexSplitter.java Thu Dec 10 13:43:50 2015
@@ -24,6 +24,7 @@ import org.apache.lucene.index.IndexWrit
 import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.TermRangeQuery;
 import org.apache.lucene.search.Weight;
 import org.apache.lucene.store.Directory;
@@ -134,9 +135,9 @@ public class PKIndexSplitter {
       final int maxDoc = in.maxDoc();
       final FixedBitSet bits = new FixedBitSet(maxDoc);
       // ignore livedocs here, as we filter them later:
-      final DocIdSetIterator preserveIt = preserveWeight.scorer(context);
-      if (preserveIt != null) {
-        bits.or(preserveIt);
+      final Scorer preverveScorer = preserveWeight.scorer(context);
+      if (preverveScorer != null) {
+        bits.or(preverveScorer.iterator());
       }
       if (negateFilter) {
         bits.flip(0, maxDoc);

Modified: lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/Sorter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/Sorter.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/Sorter.java (original)
+++ lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/index/Sorter.java Thu Dec 10 13:43:50 2015
@@ -20,6 +20,7 @@ package org.apache.lucene.index;
 import java.io.IOException;
 import java.util.Comparator;
 
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.LeafFieldComparator;
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Sort;
@@ -269,18 +270,7 @@ final class Sorter {
       return doc;
     }
 
-    @Override
-    public int nextDoc() throws IOException {
-      throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public int advance(int target) throws IOException {
-      throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public long cost() {
+    public DocIdSetIterator iterator() {
       throw new UnsupportedOperationException();
     }
 

Modified: lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java (original)
+++ lucene/dev/trunk/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java Thu Dec 10 13:43:50 2015
@@ -122,11 +122,11 @@ public class BlockJoinComparatorSource e
         IndexSearcher searcher = new IndexSearcher(ReaderUtil.getTopLevelContext(context));
         searcher.setQueryCache(null);
         final Weight weight = searcher.createNormalizedWeight(parentsFilter, false);
-        final DocIdSetIterator parents = weight.scorer(context);
+        final Scorer parents = weight.scorer(context);
         if (parents == null) {
           throw new IllegalStateException("LeafReader " + context.reader() + " contains no parents!");
         }
-        parentBits = BitSet.of(parents, context.reader().maxDoc());
+        parentBits = BitSet.of(parents.iterator(), context.reader().maxDoc());
         parentLeafComparators = new LeafFieldComparator[parentComparators.length];
         for (int i = 0; i < parentComparators.length; i++) {
           parentLeafComparators[i] = parentComparators[i].getLeafComparator(context);

Modified: lucene/dev/trunk/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java (original)
+++ lucene/dev/trunk/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java Thu Dec 10 13:43:50 2015
@@ -27,9 +27,9 @@ import org.apache.lucene.document.Field.
 import org.apache.lucene.document.NumericDocValuesField;
 import org.apache.lucene.document.StringField;
 import org.apache.lucene.search.BlockJoinComparatorSource;
-import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.SortField;
 import org.apache.lucene.search.TermQuery;
@@ -37,7 +37,6 @@ import org.apache.lucene.search.Weight;
 import org.apache.lucene.util.ArrayUtil;
 import org.apache.lucene.util.BitSet;
 import org.apache.lucene.util.LuceneTestCase;
-import org.junit.Ignore;
 
 public class TestBlockJoinSorter extends LuceneTestCase {
 
@@ -74,8 +73,8 @@ public class TestBlockJoinSorter extends
     final Query parentsFilter = new TermQuery(new Term("parent", "true"));
 
     final Weight weight = searcher.createNormalizedWeight(parentsFilter, false);
-    final DocIdSetIterator parents = weight.scorer(indexReader.leaves().get(0));
-    final BitSet parentBits = BitSet.of(parents, reader.maxDoc());
+    final Scorer parents = weight.scorer(indexReader.leaves().get(0));
+    final BitSet parentBits = BitSet.of(parents.iterator(), reader.maxDoc());
     final NumericDocValues parentValues = reader.getNumericDocValues("parent_val");
     final NumericDocValues childValues = reader.getNumericDocValues("child_val");
 

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java Thu Dec 10 13:43:50 2015
@@ -112,9 +112,9 @@ public class BoostingQuery extends Query
           if (contextScorer == null) {
             return matchScorer;
           }
-          TwoPhaseIterator contextTwoPhase = contextScorer.asTwoPhaseIterator();
+          TwoPhaseIterator contextTwoPhase = contextScorer.twoPhaseIterator();
           DocIdSetIterator contextApproximation = contextTwoPhase == null
-              ? contextScorer
+              ? contextScorer.iterator()
               : contextTwoPhase.approximation();
           return new FilterScorer(matchScorer) {
             @Override

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/CustomScoreQuery.java Thu Dec 10 13:43:50 2015
@@ -294,7 +294,7 @@ public class CustomScoreQuery extends Qu
       int doc = docID();
       if (doc > valSrcDocID) {
         for (Scorer valSrcScorer : valSrcScorers) {
-          valSrcScorer.advance(doc);
+          valSrcScorer.iterator().advance(doc);
         }
         valSrcDocID = doc;
       }

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionQuery.java Thu Dec 10 13:43:50 2015
@@ -24,6 +24,7 @@ import java.util.Set;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.Term;
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.Explanation;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
@@ -94,7 +95,7 @@ public class FunctionQuery extends Query
     final FunctionWeight weight;
     final int maxDoc;
     final float qWeight;
-    int doc=-1;
+    final DocIdSetIterator iterator;
     final FunctionValues vals;
 
     public AllScorer(LeafReaderContext context, FunctionWeight w, float qWeight) throws IOException {
@@ -103,35 +104,23 @@ public class FunctionQuery extends Query
       this.qWeight = qWeight;
       this.reader = context.reader();
       this.maxDoc = reader.maxDoc();
+      iterator = DocIdSetIterator.all(context.reader().maxDoc());
       vals = func.getValues(weight.context, context);
     }
 
     @Override
-    public int docID() {
-      return doc;
-    }
-
-    // instead of matching all docs, we could also embed a query.
-    // the score could either ignore the subscore, or boost it.
-    // Containment:  floatline(foo:myTerm, "myFloatField", 1.0, 0.0f)
-    // Boost:        foo:myTerm^floatline("myFloatField",1.0,0.0f)
-    @Override
-    public int nextDoc() throws IOException {
-      ++doc;
-      if (doc>=maxDoc) {
-        return doc=NO_MORE_DOCS;
-      }
-      return doc;
+    public DocIdSetIterator iterator() {
+      return iterator;
     }
 
     @Override
-    public int advance(int target) throws IOException {
-      return slowAdvance(target);
+    public int docID() {
+      return iterator.docID();
     }
 
     @Override
     public float score() throws IOException {
-      float score = qWeight * vals.floatVal(doc);
+      float score = qWeight * vals.floatVal(docID());
 
       // Current Lucene priority queues can't handle NaN and -Infinity, so
       // map to -Float.MAX_VALUE. This conditional handles both -infinity
@@ -140,11 +129,6 @@ public class FunctionQuery extends Query
     }
 
     @Override
-    public long cost() {
-      return maxDoc;
-    }
-
-    @Override
     public int freq() throws IOException {
       return 1;
     }

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionRangeQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionRangeQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionRangeQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/FunctionRangeQuery.java Thu Dec 10 13:43:50 2015
@@ -132,7 +132,7 @@ public class FunctionRangeQuery extends
       //  which can be slow since if that doc doesn't match, it has to linearly find the next matching
       ValueSourceScorer scorer = scorer(context);
       if (scorer.matches(doc)) {
-        scorer.advance(doc);
+        scorer.iterator().advance(doc);
         return Explanation.match(scorer.score(), FunctionRangeQuery.this.toString(), functionValues.explain(doc));
       } else {
         return Explanation.noMatch(FunctionRangeQuery.this.toString(), functionValues.explain(doc));

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSourceScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSourceScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSourceScorer.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/ValueSourceScorer.java Thu Dec 10 13:43:50 2015
@@ -48,10 +48,11 @@ public abstract class ValueSourceScorer
   protected ValueSourceScorer(IndexReader reader, FunctionValues values) {
     super(null);//no weight
     this.values = values;
-    this.twoPhaseIterator = new TwoPhaseIterator(DocIdSetIterator.all(reader.maxDoc())) { // no approximation!
+    final DocIdSetIterator approximation = DocIdSetIterator.all(reader.maxDoc()); // no approximation!
+    this.twoPhaseIterator = new TwoPhaseIterator(approximation) {
       @Override
       public boolean matches() throws IOException {
-        return ValueSourceScorer.this.matches(docID());
+        return ValueSourceScorer.this.matches(approximation.docID());
       }
 
       @Override
@@ -66,23 +67,18 @@ public abstract class ValueSourceScorer
   public abstract boolean matches(int doc);
 
   @Override
-  public TwoPhaseIterator asTwoPhaseIterator() {
-    return twoPhaseIterator;
-  }
-
-  @Override
-  public int docID() {
-    return disi.docID();
+  public DocIdSetIterator iterator() {
+    return disi;
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    return disi.nextDoc();
+  public TwoPhaseIterator twoPhaseIterator() {
+    return twoPhaseIterator;
   }
 
   @Override
-  public int advance(int target) throws IOException {
-    return disi.advance(target);
+  public int docID() {
+    return disi.docID();
   }
 
   @Override
@@ -100,8 +96,4 @@ public abstract class ValueSourceScorer
     return 1;
   }
 
-  @Override
-  public long cost() {
-    return disi.cost();
-  }
 }

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/QueryValueSource.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/QueryValueSource.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/QueryValueSource.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/QueryValueSource.java Thu Dec 10 13:43:50 2015
@@ -25,11 +25,11 @@ import org.apache.lucene.index.ReaderUti
 import org.apache.lucene.queries.function.FunctionValues;
 import org.apache.lucene.queries.function.ValueSource;
 import org.apache.lucene.queries.function.docvalues.FloatDocValues;
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Scorer;
 import org.apache.lucene.search.Weight;
-import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.mutable.MutableValue;
 import org.apache.lucene.util.mutable.MutableValueFloat;
 
@@ -86,6 +86,7 @@ class QueryDocValues extends FloatDocVal
   final Query q;
 
   Scorer scorer;
+  DocIdSetIterator it;
   int scorerDoc; // the document the scorer is on
   boolean noMatches=false;
 
@@ -129,12 +130,13 @@ class QueryDocValues extends FloatDocVal
           noMatches = true;
           return defVal;
         }
+        it = scorer.iterator();
         scorerDoc = -1;
       }
       lastDocRequested = doc;
 
       if (scorerDoc < doc) {
-        scorerDoc = scorer.advance(doc);
+        scorerDoc = it.advance(doc);
       }
 
       if (scorerDoc > doc) {
@@ -161,11 +163,12 @@ class QueryDocValues extends FloatDocVal
           noMatches = true;
           return false;
         }
+        it = scorer.iterator();
       }
       lastDocRequested = doc;
 
       if (scorerDoc < doc) {
-        scorerDoc = scorer.advance(doc);
+        scorerDoc = it.advance(doc);
       }
 
       if (scorerDoc > doc) {
@@ -221,10 +224,11 @@ class QueryDocValues extends FloatDocVal
             mval.exists = false;
             return;
           }
+          it = scorer.iterator();
           lastDocRequested = doc;
 
           if (scorerDoc < doc) {
-            scorerDoc = scorer.advance(doc);
+            scorerDoc = it.advance(doc);
           }
 
           if (scorerDoc > doc) {

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/PayloadScoreQuery.java Thu Dec 10 13:43:50 2015
@@ -27,11 +27,13 @@ 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.Scorer;
 import org.apache.lucene.search.similarities.ClassicSimilarity;
 import org.apache.lucene.search.similarities.Similarity;
+import org.apache.lucene.search.similarities.Similarity.SimScorer;
+import org.apache.lucene.search.spans.FilterSpans;
 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.SpanWeight;
 import org.apache.lucene.search.spans.Spans;
 import org.apache.lucene.util.BytesRef;
@@ -132,11 +134,13 @@ public class PayloadScoreQuery extends S
     }
 
     @Override
-    public Scorer scorer(LeafReaderContext context) throws IOException {
+    public PayloadSpanScorer scorer(LeafReaderContext context) throws IOException {
       Spans spans = getSpans(context, Postings.PAYLOADS);
       if (spans == null)
         return null;
-      return new PayloadSpans(spans, this, innerWeight.getSimScorer(context));
+      SimScorer docScorer = innerWeight.getSimScorer(context);
+      PayloadSpans payloadSpans = new PayloadSpans(spans, docScorer);
+      return new PayloadSpanScorer(this, payloadSpans, docScorer);
     }
 
     @Override
@@ -156,8 +160,8 @@ public class PayloadScoreQuery extends S
 
     @Override
     public Explanation explain(LeafReaderContext context, int doc) throws IOException {
-      PayloadSpans scorer = (PayloadSpans) scorer(context);
-      if (scorer == null || scorer.advance(doc) != doc)
+      PayloadSpanScorer scorer = scorer(context);
+      if (scorer == null || scorer.iterator().advance(doc) != doc)
         return Explanation.noMatch("No match");
 
       scorer.freq();  // force freq calculation
@@ -173,42 +177,22 @@ public class PayloadScoreQuery extends S
     }
   }
 
-  private class PayloadSpans extends Spans implements SpanCollector {
+  private class PayloadSpans extends FilterSpans implements SpanCollector {
 
-    private int payloadsSeen;
-    private float payloadScore;
-    private final Spans in;
+    private final SimScorer docScorer;
+    public int payloadsSeen;
+    public float payloadScore;
 
-    private PayloadSpans(Spans spans, SpanWeight weight, Similarity.SimScorer docScorer) throws IOException {
-      super(weight, docScorer);
-      this.in = spans;
+    private PayloadSpans(Spans in, SimScorer docScorer) {
+      super(in);
+      this.docScorer = docScorer;
     }
-
+    
     @Override
-    public int nextStartPosition() throws IOException {
-      return in.nextStartPosition();
+    protected AcceptStatus accept(Spans candidate) throws IOException {
+      return AcceptStatus.YES;
     }
-
-    @Override
-    public int startPosition() {
-      return in.startPosition();
-    }
-
-    @Override
-    public int endPosition() {
-      return in.endPosition();
-    }
-
-    @Override
-    public int width() {
-      return in.width();
-    }
-
-    @Override
-    public void collect(SpanCollector collector) throws IOException {
-      in.collect(collector);
-    }
-
+    
     @Override
     protected void doStartCurrentDoc() {
       payloadScore = 0;
@@ -216,11 +200,6 @@ public class PayloadScoreQuery extends S
     }
 
     @Override
-    protected void doCurrentSpans() throws IOException {
-      in.collect(this);
-    }
-
-    @Override
     public void collectLeaf(PostingsEnum postings, int position, Term term) throws IOException {
       BytesRef payload = postings.getPayload();
       if (payload == null)
@@ -231,12 +210,30 @@ public class PayloadScoreQuery extends S
       payloadsSeen++;
     }
 
+    @Override
+    public void reset() {}
+
+    @Override
+    protected void doCurrentSpans() throws IOException {
+      in.collect(this);
+    }
+  }
+
+  private class PayloadSpanScorer extends SpanScorer {
+
+    private final PayloadSpans spans;
+
+    private PayloadSpanScorer(SpanWeight weight, PayloadSpans spans, Similarity.SimScorer docScorer) throws IOException {
+      super(weight, spans, docScorer);
+      this.spans = spans;
+    }
+
     protected float getPayloadScore() {
-      return function.docScore(docID(), getField(), payloadsSeen, payloadScore);
+      return function.docScore(docID(), getField(), spans.payloadsSeen, spans.payloadScore);
     }
 
     protected Explanation getPayloadExplanation() {
-      return function.explain(docID(), getField(), payloadsSeen, payloadScore);
+      return function.explain(docID(), getField(), spans.payloadsSeen, spans.payloadScore);
     }
 
     protected float getSpanScore() throws IOException {
@@ -250,35 +247,6 @@ public class PayloadScoreQuery extends S
       return getPayloadScore();
     }
 
-    @Override
-    public void reset() {
-
-    }
-
-    @Override
-    public int docID() {
-      return in.docID();
-    }
-
-    @Override
-    public int nextDoc() throws IOException {
-      return in.nextDoc();
-    }
-
-    @Override
-    public int advance(int target) throws IOException {
-      return in.advance(target);
-    }
-
-    @Override
-    public long cost() {
-      return in.cost();
-    }
-
-    @Override
-    public float positionsCost() {
-      return in.positionsCost();
-    }
   }
 
 }

Modified: lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java (original)
+++ lucene/dev/trunk/lucene/queries/src/java/org/apache/lucene/queries/payloads/SpanPayloadCheckQuery.java Thu Dec 10 13:43:50 2015
@@ -27,11 +27,12 @@ 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.Scorer;
+import org.apache.lucene.search.similarities.Similarity;
 import org.apache.lucene.search.spans.FilterSpans;
 import org.apache.lucene.search.spans.FilterSpans.AcceptStatus;
 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.SpanWeight;
 import org.apache.lucene.search.spans.Spans;
 import org.apache.lucene.util.BytesRef;
@@ -90,7 +91,7 @@ public class SpanPayloadCheckQuery exten
     public Spans getSpans(final LeafReaderContext context, Postings requiredPostings) throws IOException {
       final PayloadChecker collector = new PayloadChecker();
       Spans matchSpans = matchWeight.getSpans(context, requiredPostings.atLeast(Postings.PAYLOADS));
-      return (matchSpans == null) ? null : new FilterSpans(matchSpans, getSimScorer(context)) {
+      return (matchSpans == null) ? null : new FilterSpans(matchSpans) {
         @Override
         protected AcceptStatus accept(Spans candidate) throws IOException {
           collector.reset();
@@ -101,7 +102,7 @@ public class SpanPayloadCheckQuery exten
     }
 
     @Override
-    public Scorer scorer(LeafReaderContext context) throws IOException {
+    public SpanScorer scorer(LeafReaderContext context) throws IOException {
       if (field == null)
         return null;
 
@@ -110,7 +111,12 @@ public class SpanPayloadCheckQuery exten
         throw new IllegalStateException("field \"" + field + "\" was indexed without position data; cannot run SpanQuery (query=" + parentQuery + ")");
       }
 
-      return getSpans(context, Postings.PAYLOADS);
+      final Spans spans = getSpans(context, Postings.PAYLOADS);
+      if (spans == null) {
+        return null;
+      }
+      final Similarity.SimScorer docScorer = getSimScorer(context);
+      return new SpanScorer(this, spans, docScorer);
     }
   }
 

Modified: lucene/dev/trunk/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonScorer.java?rev=1719081&r1=1719080&r2=1719081&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonScorer.java (original)
+++ lucene/dev/trunk/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonScorer.java Thu Dec 10 13:43:50 2015
@@ -130,71 +130,86 @@ class TermAutomatonScorer extends Scorer
   }
 
   @Override
-  public int nextDoc() throws IOException {
-    // we only need to advance docs that are positioned since all docs in the
-    // pq are guaranteed to be beyond the current doc already
-    for(int i=0;i<numSubsOnDoc;i++) {
-      EnumAndScorer sub = subsOnDoc[i];
-      if (sub.posEnum.nextDoc() != NO_MORE_DOCS) {
-        sub.posLeft = sub.posEnum.freq()-1;
-        sub.pos = sub.posEnum.nextPosition();
+  public DocIdSetIterator iterator() {
+    return new DocIdSetIterator() {
+      @Override
+      public int docID() {
+        return docID;
       }
-    }
-    pushCurrentDoc();
-    return doNext();
-  }
 
-  @Override
-  public int advance(int target) throws IOException {
-    // Both positioned docs and docs in the pq might be behind target
+      @Override
+      public long cost() {
+        return cost;
+      }
 
-    // 1. Advance the PQ
-    if (docIDQueue.size() > 0) {
-      EnumAndScorer top = docIDQueue.top();
-      while (top.posEnum.docID() < target) {
-        if (top.posEnum.advance(target) != NO_MORE_DOCS) {
-          top.posLeft = top.posEnum.freq()-1;
-          top.pos = top.posEnum.nextPosition();
+      @Override
+      public int nextDoc() throws IOException {
+        // we only need to advance docs that are positioned since all docs in the
+        // pq are guaranteed to be beyond the current doc already
+        for(int i=0;i<numSubsOnDoc;i++) {
+          EnumAndScorer sub = subsOnDoc[i];
+          if (sub.posEnum.nextDoc() != NO_MORE_DOCS) {
+            sub.posLeft = sub.posEnum.freq()-1;
+            sub.pos = sub.posEnum.nextPosition();
+          }
         }
-        top = docIDQueue.updateTop();
+        pushCurrentDoc();
+        return doNext();
       }
-    }
 
-    // 2. Advance subsOnDoc
-    for(int i=0;i<numSubsOnDoc;i++) {
-      EnumAndScorer sub = subsOnDoc[i];
-      if (sub.posEnum.advance(target) != NO_MORE_DOCS) {
-        sub.posLeft = sub.posEnum.freq()-1;
-        sub.pos = sub.posEnum.nextPosition();
-      }
-    }
-    pushCurrentDoc();
-    return doNext();
-  }
+      @Override
+      public int advance(int target) throws IOException {
+        // Both positioned docs and docs in the pq might be behind target
+
+        // 1. Advance the PQ
+        if (docIDQueue.size() > 0) {
+          EnumAndScorer top = docIDQueue.top();
+          while (top.posEnum.docID() < target) {
+            if (top.posEnum.advance(target) != NO_MORE_DOCS) {
+              top.posLeft = top.posEnum.freq()-1;
+              top.pos = top.posEnum.nextPosition();
+            }
+            top = docIDQueue.updateTop();
+          }
+        }
 
-  private int doNext() throws IOException {
-    assert numSubsOnDoc == 0;
-    assert docIDQueue.top().posEnum.docID() > docID;
-    while (true) {
-      //System.out.println("  doNext: cycle");
-      popCurrentDoc();
-      //System.out.println("    docID=" + docID);
-      if (docID == NO_MORE_DOCS) {
-        return docID;
-      }
-      countMatches();
-      if (freq > 0) {
-        return docID;
+        // 2. Advance subsOnDoc
+        for(int i=0;i<numSubsOnDoc;i++) {
+          EnumAndScorer sub = subsOnDoc[i];
+          if (sub.posEnum.advance(target) != NO_MORE_DOCS) {
+            sub.posLeft = sub.posEnum.freq()-1;
+            sub.pos = sub.posEnum.nextPosition();
+          }
+        }
+        pushCurrentDoc();
+        return doNext();
       }
-      for(int i=0;i<numSubsOnDoc;i++) {
-        EnumAndScorer sub = subsOnDoc[i];
-        if (sub.posEnum.nextDoc() != NO_MORE_DOCS) {
-          sub.posLeft = sub.posEnum.freq()-1;
-          sub.pos = sub.posEnum.nextPosition();
+
+      private int doNext() throws IOException {
+        assert numSubsOnDoc == 0;
+        assert docIDQueue.top().posEnum.docID() > docID;
+        while (true) {
+          //System.out.println("  doNext: cycle");
+          popCurrentDoc();
+          //System.out.println("    docID=" + docID);
+          if (docID == NO_MORE_DOCS) {
+            return docID;
+          }
+          countMatches();
+          if (freq > 0) {
+            return docID;
+          }
+          for(int i=0;i<numSubsOnDoc;i++) {
+            EnumAndScorer sub = subsOnDoc[i];
+            if (sub.posEnum.nextDoc() != NO_MORE_DOCS) {
+              sub.posLeft = sub.posEnum.freq()-1;
+              sub.pos = sub.posEnum.nextPosition();
+            }
+          }
+          pushCurrentDoc();
         }
       }
-      pushCurrentDoc();
-    }
+    };
   }
 
   private PosState getPosition(int pos) {
@@ -351,11 +366,6 @@ class TermAutomatonScorer extends Scorer
     return docScorer.score(docID, freq);
   }
 
-  @Override
-  public long cost() {
-    return cost;
-  }
-
   static class TermRunAutomaton extends RunAutomaton {
     public TermRunAutomaton(Automaton a, int termCount) {
       super(a, termCount, true);