You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rj...@apache.org on 2014/03/24 19:43:22 UTC

svn commit: r1580987 - in /lucene/dev/branches/lucene_solr_4_7: ./ dev-tools/ dev-tools/idea/solr/core/src/test/ lucene/ lucene/core/ lucene/core/src/java/org/apache/lucene/search/ solr/ solr/core/ solr/core/src/java/org/apache/solr/handler/component/ ...

Author: rjernst
Date: Mon Mar 24 18:43:21 2014
New Revision: 1580987

URL: http://svn.apache.org/r1580987
Log:
SOLR-5818: distrib search with custom comparator does not quite work correctly

Added:
    lucene/dev/branches/lucene_solr_4_7/solr/core/src/test-files/solr/collection1/conf/schema-field-sort-values.xml
      - copied unchanged from r1575370, lucene/dev/branches/branch_4x/solr/core/src/test-files/solr/collection1/conf/schema-field-sort-values.xml
    lucene/dev/branches/lucene_solr_4_7/solr/core/src/test/org/apache/solr/schema/WrappedIntField.java
      - copied unchanged from r1575370, lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/schema/WrappedIntField.java
    lucene/dev/branches/lucene_solr_4_7/solr/core/src/test/org/apache/solr/search/TestFieldSortValues.java
      - copied unchanged from r1575370, lucene/dev/branches/branch_4x/solr/core/src/test/org/apache/solr/search/TestFieldSortValues.java
Modified:
    lucene/dev/branches/lucene_solr_4_7/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/dev-tools/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/dev-tools/idea/solr/core/src/test/solr-core-tests.iml
    lucene/dev/branches/lucene_solr_4_7/lucene/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/lucene/core/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
    lucene/dev/branches/lucene_solr_4_7/solr/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/solr/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/lucene_solr_4_7/solr/core/   (props changed)
    lucene/dev/branches/lucene_solr_4_7/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java

Modified: lucene/dev/branches/lucene_solr_4_7/dev-tools/idea/solr/core/src/test/solr-core-tests.iml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_7/dev-tools/idea/solr/core/src/test/solr-core-tests.iml?rev=1580987&r1=1580986&r2=1580987&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_7/dev-tools/idea/solr/core/src/test/solr-core-tests.iml (original)
+++ lucene/dev/branches/lucene_solr_4_7/dev-tools/idea/solr/core/src/test/solr-core-tests.iml Mon Mar 24 18:43:21 2014
@@ -28,5 +28,6 @@
     <orderEntry type="module" scope="TEST" module-name="spatial" />
     <orderEntry type="module" scope="TEST" module-name="misc" />
     <orderEntry type="module" scope="TEST" module-name="join" />
+    <orderEntry type="module" scope="TEST" module-name="expressions" />
   </component>
 </module>

Modified: lucene/dev/branches/lucene_solr_4_7/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_7/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java?rev=1580987&r1=1580986&r2=1580987&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_7/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java (original)
+++ lucene/dev/branches/lucene_solr_4_7/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java Mon Mar 24 18:43:21 2014
@@ -693,8 +693,7 @@ public abstract class FieldComparator<T>
 
     @Override
     public int compare(int slot1, int slot2) {
-      // TODO: there are sneaky non-branch ways to compute
-      // -1/+1/0 sign
+      // In Java 6 there is no Long#compare(long,long):
       final long v1 = values[slot1];
       final long v2 = values[slot2];
       if (v1 > v2) {
@@ -717,6 +716,7 @@ public abstract class FieldComparator<T>
         v2 = missingValue;
       }
 
+      // In Java 6 there is no Long#compare(long,long):
       if (bottom > v2) {
         return 1;
       } else if (bottom < v2) {

Modified: lucene/dev/branches/lucene_solr_4_7/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_7/solr/CHANGES.txt?rev=1580987&r1=1580986&r2=1580987&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_7/solr/CHANGES.txt (original)
+++ lucene/dev/branches/lucene_solr_4_7/solr/CHANGES.txt Mon Mar 24 18:43:21 2014
@@ -64,6 +64,9 @@ Bug Fixes
   Includes a few minor bug fixes.
   (Mark Miller)
 
+* SOLR-5818: distrib search with custom comparator does not quite work correctly
+  (Ryan Ernst)
+
 * SOLR-5895: JavaBinLoader hides IOExceptions. (Mike Sokolov via shalin)
 
 * SOLR-5861: Recovery should not set onlyIfLeaderActive=true for slice in 'recovery'

Modified: lucene/dev/branches/lucene_solr_4_7/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_4_7/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java?rev=1580987&r1=1580986&r2=1580987&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_4_7/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java (original)
+++ lucene/dev/branches/lucene_solr_4_7/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java Mon Mar 24 18:43:21 2014
@@ -25,12 +25,14 @@ import org.apache.lucene.search.BooleanQ
 import org.apache.lucene.search.FieldComparator;
 import org.apache.lucene.search.Query;
 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.grouping.GroupDocs;
 import org.apache.lucene.search.grouping.SearchGroup;
 import org.apache.lucene.search.grouping.TopGroups;
 import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.InPlaceMergeSorter;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrDocumentList;
@@ -500,12 +502,41 @@ public class QueryComponent extends Sear
 
       // sort ids from lowest to highest so we can access them in order
       int nDocs = docList.size();
-      long[] sortedIds = new long[nDocs];
-      DocIterator it = rb.getResults().docList.iterator();
+      final long[] sortedIds = new long[nDocs];
+      final float[] scores = new float[nDocs]; // doc scores, parallel to sortedIds
+      DocList docs = rb.getResults().docList;
+      DocIterator it = docs.iterator();
       for (int i=0; i<nDocs; i++) {
         sortedIds[i] = (((long)it.nextDoc()) << 32) | i;
+        scores[i] = docs.hasScores() ? it.score() : Float.NaN;
       }
-      Arrays.sort(sortedIds);
+
+      // sort ids and scores together
+      new InPlaceMergeSorter() {
+        @Override
+        protected void swap(int i, int j) {
+          long tmpId = sortedIds[i];
+          float tmpScore = scores[i];
+          sortedIds[i] = sortedIds[j];
+          scores[i] = scores[j];
+          sortedIds[j] = tmpId;
+          scores[j] = tmpScore;
+        }
+
+        @Override
+        protected int compare(int i, int j) {
+          // In Java 6 there is no Long#compare(long,long):
+          final long v1 = sortedIds[i];
+          final long v2 = sortedIds[j];
+          if (v1 > v2) {
+            return 1;
+          } else if (v1 < v2) {
+            return -1;
+          } else {
+            return 0;
+          }
+        }
+      }.sort(0, sortedIds.length);
 
       SortSpec sortSpec = rb.getSortSpec();
       Sort sort = searcher.weightSort(sortSpec.getSort());
@@ -527,7 +558,9 @@ public class QueryComponent extends Sear
         int lastIdx = -1;
         int idx = 0;
 
-        for (long idAndPos : sortedIds) {
+        for (int i = 0; i < sortedIds.length; ++i) {
+          long idAndPos = sortedIds[i];
+          float score = scores[i];
           int doc = (int)(idAndPos >>> 32);
           int position = (int)idAndPos;
 
@@ -546,6 +579,7 @@ public class QueryComponent extends Sear
           }
 
           doc -= currentLeaf.docBase;  // adjust for what segment this is in
+          comparator.setScorer(new FakeScorer(doc, score));
           comparator.copy(0, doc);
           Object val = comparator.value(0);
           if (null != ft) val = ft.marshalSortValue(val); 
@@ -1147,4 +1181,50 @@ public class QueryComponent extends Sear
   public URL[] getDocs() {
     return null;
   }
+
+  /**
+   * Fake scorer for a single document
+   *
+   * TODO: when SOLR-5595 is fixed, this wont be needed, as we dont need to recompute sort values here from the comparator
+   */
+  private static class FakeScorer extends Scorer {
+    final int docid;
+    final float score;
+
+    FakeScorer(int docid, float score) {
+      super(null);
+      this.docid = docid;
+      this.score = score;
+    }
+
+    @Override
+    public int docID() {
+      return docid;
+    }
+
+    @Override
+    public float score() throws IOException {
+      return score;
+    }
+
+    @Override
+    public int freq() throws IOException {
+      throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int nextDoc() throws IOException {
+      throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public int advance(int target) throws IOException {
+      throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public long cost() {
+      return 1;
+    }
+  }
 }