You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2012/11/26 22:01:52 UTC

svn commit: r1413840 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/spatial/ lucene/spatial/src/test/org/apache/lucene/spatial/

Author: dsmiley
Date: Mon Nov 26 21:01:51 2012
New Revision: 1413840

URL: http://svn.apache.org/viewvc?rev=1413840&view=rev
Log:
LUCENE-4573 Improve SpatialExample.java to show distance value retrieval

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/spatial/   (props changed)
    lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/TestTestFramework.java

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java?rev=1413840&r1=1413839&r2=1413840&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/SpatialExample.java Mon Nov 26 21:01:51 2012
@@ -55,6 +55,8 @@ import java.io.IOException;
  */
 public class SpatialExample extends LuceneTestCase {
 
+  //Note: Test invoked via TestTestFramework.spatialExample()
+
   public static void main(String[] args) throws IOException {
     new SpatialExample().test();
   }
@@ -149,14 +151,27 @@ public class SpatialExample extends Luce
       Filter filter = strategy.makeFilter(args);
       TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), filter, 10, idSort);
       assertDocMatchedIds(indexSearcher, docs, 2);
+      //Now, lets get the distance for the 1st doc via computing from stored point value:
+      // (this computation is usually not redundant)
+      Document doc1 = indexSearcher.doc(docs.scoreDocs[0].doc);
+      String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
+      Point doc1Point = (Point) ctx.readShape(doc1Str);
+      double doc1DistDEG = ctx.getDistCalc().distance(args.getShape().getCenter(), doc1Point);
+      assertEquals(121.6d, DistanceUtils.degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1);
     }
-    //--Match all, order by distance
+    //--Match all, order by distance ascending
     {
       Point pt = ctx.makePoint(60, -50);
       ValueSource valueSource = strategy.makeDistanceValueSource(pt);//the distance (in degrees)
-      Sort reverseDistSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);//true=asc dist
-      TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, reverseDistSort);
+      Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);//false=asc dist
+      TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, distSort);
       assertDocMatchedIds(indexSearcher, docs, 4, 20, 2);
+      //To get the distance, we could compute from stored values like earlier.
+      // However in this example we sorted on it, and the distance will get
+      // computed redundantly.  If the distance is only needed for the top-X
+      // search results then that's not a big deal. Alternatively, try wrapping
+      // the ValueSource with CachingDoubleValueSource then retrieve the value
+      // from the ValueSource now. See LUCENE-4541 for an example.
     }
     //demo arg parsing
     {

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java?rev=1413840&r1=1413839&r2=1413840&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java Mon Nov 26 21:01:51 2012
@@ -80,7 +80,10 @@ public abstract class StrategyTestCase e
   }
 
   protected List<Document> getDocuments(String testDataFile) throws IOException {
-    Iterator<SampleData> sampleData = getSampleData(testDataFile);
+    return getDocuments(getSampleData(testDataFile));
+  }
+
+  protected List<Document> getDocuments(Iterator<SampleData> sampleData) {
     List<Document> documents = new ArrayList<Document>();
     while (sampleData.hasNext()) {
       SampleData data = sampleData.next();

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/TestTestFramework.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/TestTestFramework.java?rev=1413840&r1=1413839&r2=1413840&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/TestTestFramework.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/TestTestFramework.java Mon Nov 26 21:01:51 2012
@@ -59,4 +59,11 @@ public class TestTestFramework extends L
     Assert.assertEquals( SpatialOperation.Intersects, sf.args.getOperation() );
   }
 
+  @Test
+  public void spatialExample() throws IOException {
+    //kind of a hack so that SpatialExample is tested despite
+    // it not starting or ending with "Test".
+    SpatialExample.main(null);
+  }
+
 }