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/07/11 08:24:12 UTC

svn commit: r1360024 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/spatial/ lucene/spatial/src/java/org/apache/lucene/spatial/ lucene/spatial/src/java/org/apache/lucene/spatial/prefix/ lucene/spatial/src/java/org/apache/lucene/spatial/vector/ ...

Author: dsmiley
Date: Wed Jul 11 06:24:11 2012
New Revision: 1360024

URL: http://svn.apache.org/viewvc?rev=1360024&view=rev
Log:
LUCENE-4188 remove field storage from createField

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/java/org/apache/lucene/spatial/SpatialStrategy.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.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/prefix/TestRecursivePrefixTreeStrategy.java
    lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java Wed Jul 11 06:24:11 2012
@@ -19,6 +19,7 @@ package org.apache.lucene.spatial;
 
 import com.spatial4j.core.context.SpatialContext;
 import com.spatial4j.core.shape.Shape;
+import org.apache.lucene.document.StoredField;
 import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.queries.function.FunctionQuery;
 import org.apache.lucene.queries.function.ValueSource;
@@ -28,7 +29,12 @@ import org.apache.lucene.search.Query;
 import org.apache.lucene.spatial.query.SpatialArgs;
 
 /**
- * must be thread safe
+ * The SpatialStrategy encapsulates an approach to indexing and searching based on shapes.
+ * <p/>
+ * Note that a SpatialStrategy is not involved with the Lucene stored field values of shapes, which is
+ * immaterial to indexing & search.
+ * <p/>
+ * Thread-safe.
  *
  * @lucene.experimental
  */
@@ -75,11 +81,29 @@ public abstract class SpatialStrategy {
    * This is reasonable behavior if 'ignoreIncompatibleGeometry=true' and the
    * geometry is incompatible
    */
-  public abstract IndexableField createField(Shape shape, boolean index, boolean store);
+  public abstract IndexableField createField(Shape shape);
 
-  /** Corresponds with Solr's FieldType.createFields(). */
-  public IndexableField[] createFields(Shape shape, boolean index, boolean store) {
-    return new IndexableField[] { createField(shape, index, store) };
+  /**
+   * Corresponds with Solr's FieldType.createFields().
+   * <p/>
+   * Note: If you want to <i>store</i> the shape as a string for retrieval in search
+   * results, you could add it like this:
+   * <pre>document.add(new StoredField(fieldName,ctx.toString(shape)));</pre>
+   * The particular string representation used doesn't matter to the Strategy since it
+   * doesn't use it.
+   */
+  public IndexableField[] createFields(Shape shape) {
+    return new IndexableField[]{createField(shape)};
+  }
+
+  /**
+   * A convenience method for storing the shape in Lucene for retrieval in search results.
+   * After calling this, add it to the document: {@link org.apache.lucene.document.Document#add(org.apache.lucene.index.IndexableField)}.
+   * All this does is:
+   * <pre>return new StoredField(getFieldName(),ctx.toString(shape));</pre>
+   */
+  public StoredField createStoredField(Shape shape) {
+    return new StoredField(getFieldName(), ctx.toString(shape));
   }
 
   /**

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java Wed Jul 11 06:24:11 2012
@@ -24,7 +24,6 @@ import org.apache.lucene.analysis.TokenS
 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.FieldType;
-import org.apache.lucene.document.StoredField;
 import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.queries.function.ValueSource;
 import org.apache.lucene.spatial.SpatialStrategy;
@@ -63,7 +62,7 @@ public abstract class PrefixTreeStrategy
   }
 
   @Override
-  public IndexableField createField(Shape shape, boolean index, boolean store) {
+  public IndexableField createField(Shape shape) {
     int detailLevel = grid.getMaxLevelForPrecision(shape,distErrPct);
     List<Node> cells = grid.getNodes(shape, detailLevel, true);//true=intermediates cells
     //If shape isn't a point, add a full-resolution center-point so that
@@ -78,42 +77,17 @@ public abstract class PrefixTreeStrategy
     //TODO is CellTokenStream supposed to be re-used somehow? see Uwe's comments:
     //  http://code.google.com/p/lucene-spatial-playground/issues/detail?id=4
 
-    String fname = getFieldName();
-    if( store ) {
-      //TODO figure out how to re-use original string instead of reconstituting it.
-      String wkt = grid.getSpatialContext().toString(shape);
-      if( index ) {
-        Field f = new Field(fname,wkt,TYPE_STORED);
-        f.setTokenStream(new CellTokenStream(cells.iterator()));
-        return f;
-      }
-      return new StoredField(fname,wkt);
-    }
-    
-    if( index ) {
-      return new Field(fname,new CellTokenStream(cells.iterator()),TYPE_NOT_STORED);
-    }
-    
-    throw new UnsupportedOperationException("Fields need to be indexed or store ["+fname+"]");
+    return new Field(getFieldName(),new CellTokenStream(cells.iterator()), FIELD_TYPE);
   }
 
   /* Indexed, tokenized, not stored. */
-  public static final FieldType TYPE_NOT_STORED = new FieldType();
-
-  /* Indexed, tokenized, stored. */
-  public static final FieldType TYPE_STORED = new FieldType();
+  public static final FieldType FIELD_TYPE = new FieldType();
 
   static {
-    TYPE_NOT_STORED.setIndexed(true);
-    TYPE_NOT_STORED.setTokenized(true);
-    TYPE_NOT_STORED.setOmitNorms(true);
-    TYPE_NOT_STORED.freeze();
-
-    TYPE_STORED.setStored(true);
-    TYPE_STORED.setIndexed(true);
-    TYPE_STORED.setTokenized(true);
-    TYPE_STORED.setOmitNorms(true);
-    TYPE_STORED.freeze();
+    FIELD_TYPE.setIndexed(true);
+    FIELD_TYPE.setTokenized(true);
+    FIELD_TYPE.setOmitNorms(true);
+    FIELD_TYPE.freeze();
   }
 
   /** Outputs the tokenString of a cell, and if its a leaf, outputs it again with the leaf byte. */

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java Wed Jul 11 06:24:11 2012
@@ -24,7 +24,6 @@ import com.spatial4j.core.shape.Point;
 import com.spatial4j.core.shape.Rectangle;
 import com.spatial4j.core.shape.Shape;
 import org.apache.lucene.document.DoubleField;
-import org.apache.lucene.document.Field;
 import org.apache.lucene.document.FieldType;
 import org.apache.lucene.index.IndexableField;
 import org.apache.lucene.queries.function.FunctionQuery;
@@ -83,20 +82,14 @@ public class TwoDoublesStrategy extends 
   }
 
   @Override
-  public IndexableField[] createFields(Shape shape, boolean index, boolean store) {
+  public IndexableField[] createFields(Shape shape) {
     if( shape instanceof Point ) {
       Point point = (Point)shape;
-
-      IndexableField[] f = new IndexableField[(index ? 2 : 0) + (store ? 1 : 0)];
-      if (index) {
-        f[0] = createDouble(fieldNameX, point.getX(), index, store);
-        f[1] = createDouble(fieldNameY, point.getY(), index, store);
-      }
-      if(store) {
-        FieldType customType = new FieldType();
-        customType.setStored(true);
-        f[f.length-1] = new Field( getFieldName(), ctx.toString( shape ), customType );
-      }
+      FieldType doubleFieldType = new FieldType(DoubleField.TYPE_NOT_STORED);
+      doubleFieldType.setNumericPrecisionStep(precisionStep);
+      IndexableField[] f = new IndexableField[2];
+      f[0] = new DoubleField(fieldNameX, point.getX(), doubleFieldType);
+      f[1] = new DoubleField(fieldNameY, point.getY(), doubleFieldType);
       return f;
     }
     if( !ignoreIncompatibleGeometry ) {
@@ -105,20 +98,8 @@ public class TwoDoublesStrategy extends 
     return new IndexableField[0]; // nothing (solr does not support null)
   }
 
-  private IndexableField createDouble(String name, double v, boolean index, boolean store) {
-    if (!store && !index)
-      throw new IllegalArgumentException("field must be indexed or stored");
-
-    FieldType fieldType = new FieldType(DoubleField.TYPE_NOT_STORED);
-    fieldType.setStored(store);
-    fieldType.setIndexed(index);
-    fieldType.setNumericPrecisionStep(precisionStep);
-    return new DoubleField(name,v,fieldType);
-  }
-
   @Override
-  public IndexableField createField(Shape shape,
-                                    boolean index, boolean store) {
+  public IndexableField createField(Shape shape) {
     throw new UnsupportedOperationException("Point is poly field");
   }
 

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java Wed Jul 11 06:24:11 2012
@@ -77,7 +77,7 @@ public class PortedSolr3Test extends Str
   static class Param {
     SpatialStrategy strategy;
     String description;
-    
+
     Param(SpatialStrategy strategy, String description) {
       this.strategy = strategy;
       this.description = description;
@@ -193,9 +193,11 @@ public class PortedSolr3Test extends Str
   private Document newDoc(String id, Shape shape) {
     Document doc = new Document();
     doc.add(new StringField("id", id, Field.Store.YES));
-    for (IndexableField f : strategy.createFields(shape, true, storeShape)) {
+    for (IndexableField f : strategy.createFields(shape)) {
       doc.add(f);
     }
+    if (storeShape)
+      doc.add(strategy.createStoredField(shape));
     return doc;
   }
 

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=1360024&r1=1360023&r2=1360024&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 Wed Jul 11 06:24:11 2012
@@ -83,11 +83,14 @@ public abstract class StrategyTestCase e
       document.add(new StringField("id", data.id, Field.Store.YES));
       document.add(new StringField("name", data.name, Field.Store.YES));
       Shape shape = ctx.readShape(data.shape);
-      for (IndexableField f : strategy.createFields(shape, true, storeShape)) {
+      for (IndexableField f : strategy.createFields(shape)) {
         if( f != null ) { // null if incompatibleGeometry && ignore
           document.add(f);
         }
       }
+      if (storeShape)
+        document.add(strategy.createStoredField(shape));
+
       documents.add(document);
     }
     return documents;
@@ -112,6 +115,10 @@ public abstract class StrategyTestCase e
 
       String msg = q.line; //"Query: " + q.args.toString(ctx);
       SearchResults got = executeQuery(strategy.makeQuery(q.args), 100);
+      if (storeShape && got.numFound > 0) {
+        //check stored value is there & parses
+        assertNotNull(ctx.readShape(got.results.get(0).document.get(strategy.getFieldName())));
+      }
       if (concern.orderIsImportant) {
         Iterator<String> ids = q.ids.iterator();
         for (SearchResult r : got.results) {

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java Wed Jul 11 06:24:11 2012
@@ -153,9 +153,11 @@ public class TestRecursivePrefixTreeStra
   private Document newDoc(String id, Shape shape) {
     Document doc = new Document();
     doc.add(new StringField("id", id, Field.Store.YES));
-    for (IndexableField f : strategy.createFields(shape, true, storeShape)) {
+    for (IndexableField f : strategy.createFields(shape)) {
       doc.add(f);
     }
+    if (storeShape)
+      doc.add(strategy.createStoredField(shape));
     return doc;
   }
 

Modified: lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java?rev=1360024&r1=1360023&r2=1360024&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java (original)
+++ lucene/dev/branches/branch_4x/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java Wed Jul 11 06:24:11 2012
@@ -44,7 +44,8 @@ public class TestTermQueryPrefixGridStra
 
     Document losAngeles = new Document();
     losAngeles.add(new StringField("name", "Los Angeles", Field.Store.YES));
-    losAngeles.add(prefixGridStrategy.createField(point, true, true));
+    losAngeles.add(prefixGridStrategy.createField(point));
+    losAngeles.add(prefixGridStrategy.createStoredField(point));
 
     addDocumentsAndCommit(Arrays.asList(losAngeles));