Skip to content

Apache Lucene open-source search software

LUCENE-4188 remove field storage from createField

David Wayne Smiley401bfde9bd55
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java
index 59df15e4d57..b1cf5e50c7d 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/SpatialStrategy.java
@@ -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));
}
/**
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java
index bb20b4f2b23..226a8660568 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/bbox/BBoxStrategy.java
@@ -23,7 +23,7 @@ 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.FieldInfo.IndexOptions;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.function.FunctionQuery;
@@ -41,9 +41,6 @@ import org.apache.lucene.spatial.query.SpatialArgs;
import org.apache.lucene.spatial.query.SpatialOperation;
import org.apache.lucene.spatial.query.UnsupportedSpatialOperation;
-import java.text.NumberFormat;
-import java.util.Locale;
-
/**
* Based on GeoPortal's
@@ -95,61 +92,21 @@ public class BBoxStrategy extends SpatialStrategy {
//---------------------------------
@Override
- public IndexableField[] createFields(Shape shape, boolean index, boolean store) {
-
+ public IndexableField[] createFields(Shape shape) {
Rectangle bbox = shape.getBoundingBox();
- IndexableField[] fields = new IndexableField[store?6:5];
- fields[0] = createDouble(field_minX, bbox.getMinX(), index, store);
- fields[1] = createDouble(field_maxX, bbox.getMaxX(), index, store);
- fields[2] = createDouble(field_minY, bbox.getMinY(), index, store);
- fields[3] = createDouble(field_maxY, bbox.getMaxY(), index, store);
-
- FieldType ft = new FieldType();
- ft.setIndexed(index);
- ft.setStored(store);
- ft.setTokenized(false);
- ft.setOmitNorms(true);
- ft.setIndexOptions(IndexOptions.DOCS_ONLY);
- ft.freeze();
-
- Field xdl = new Field( field_xdl, bbox.getCrossesDateLine()?"T":"F", ft );
- fields[4] = xdl;
- if( store ) {
- FieldType ff = new FieldType();
- ff.setIndexed(false);
- ff.setStored(true);
- ff.setOmitNorms(true);
- ff.setIndexOptions(IndexOptions.DOCS_ONLY);
- ff.freeze();
-
- NumberFormat nf = NumberFormat.getInstance( Locale.ROOT );
- nf.setMaximumFractionDigits( 5 );
- nf.setMinimumFractionDigits( 5 );
- nf.setGroupingUsed(false);
- String ext =
- nf.format( bbox.getMinX() ) + ' ' +
- nf.format( bbox.getMinY() ) + ' ' +
- nf.format( bbox.getMaxX() ) + ' ' +
- nf.format( bbox.getMaxY() ) + ' ';
- fields[5] = new Field( field_bbox, ext, ff );
- }
+ FieldType doubleFieldType = new FieldType(DoubleField.TYPE_NOT_STORED);
+ doubleFieldType.setNumericPrecisionStep(precisionStep);
+ IndexableField[] fields = new IndexableField[5];
+ fields[0] = new DoubleField(field_minX, bbox.getMinX(), doubleFieldType);
+ fields[1] = new DoubleField(field_maxX, bbox.getMaxX(), doubleFieldType);
+ fields[2] = new DoubleField(field_minY, bbox.getMinY(), doubleFieldType);
+ fields[3] = new DoubleField(field_maxY, bbox.getMaxY(), doubleFieldType);
+ fields[4] = new Field( field_xdl, bbox.getCrossesDateLine()?"T":"F", StringField.TYPE_NOT_STORED);
return fields;
}
- 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("BBOX is poly field");
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java
index 2a9610260a1..ed2b3172f0a 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/PrefixTreeStrategy.java
@@ -24,7 +24,6 @@ import org.apache.lucene.analysis.TokenStream;
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 extends SpatialStrategy {
}
@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 extends SpatialStrategy {
//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. */
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java b/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java
index 2a69f7a7fce..37b6680e7fa 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/vector/TwoDoublesStrategy.java
@@ -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 SpatialStrategy {
}
@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 SpatialStrategy {
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");
}
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java b/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java
index a4c4201895c..dc11534095d 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/PortedSolr3Test.java
@@ -77,7 +77,7 @@ public class PortedSolr3Test extends StrategyTestCase {
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 StrategyTestCase {
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;
}
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java b/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java
index 9e6fe821146..e71e5d97231 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/StrategyTestCase.java
@@ -84,11 +84,14 @@ public abstract class StrategyTestCase extends SpatialTestCase {
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;
@@ -113,6 +116,10 @@ public abstract class StrategyTestCase extends SpatialTestCase {
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) {
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java
index 3e7fbf18429..636a42023d0 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestRecursivePrefixTreeStrategy.java
@@ -153,9 +153,11 @@ public class TestRecursivePrefixTreeStrategy extends StrategyTestCase {
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;
}
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java
index d58b83bb42d..c2324052f99 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/TestTermQueryPrefixGridStrategy.java
@@ -44,7 +44,8 @@ public class TestTermQueryPrefixGridStrategy extends SpatialTestCase {
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));