Apache Solr open-source search software
SOLR-6781: Bugfix- BBoxField didn't support dynamic fields.
David Wayne Smileyf60f243b1740
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 166e526834a..1eacc8f683b 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -433,6 +433,8 @@ Bug Fixes* SOLR-6684: Fix-up /export JSON. (Joel Bernstein)+* SOLR-6781: BBoxField didn't support dynamic fields. (David Smiley)+================== 4.10.2 ==================Bug Fixesdiff --git a/solr/core/src/java/org/apache/solr/schema/BBoxField.java b/solr/core/src/java/org/apache/solr/schema/BBoxField.javaindex 6e6cf77eb76..286c72cdae5 100644--- a/solr/core/src/java/org/apache/solr/schema/BBoxField.java+++ b/solr/core/src/java/org/apache/solr/schema/BBoxField.java@@ -34,8 +34,8 @@ import java.util.Map;public class BBoxField extends AbstractSpatialFieldType<BBoxStrategy> implements SchemaAware {private static final String PARAM_QUERY_TARGET_PROPORTION = "queryTargetProportion";private static final String PARAM_MIN_SIDE_LENGTH = "minSideLength";- private String numberFieldName;//required- private String booleanFieldName = "boolean";+ private String numberTypeName;//required+ private String booleanTypeName = "boolean";private IndexSchema schema;@@ -48,25 +48,25 @@ public class BBoxField extends AbstractSpatialFieldType<BBoxStrategy> implementsthrow new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The field type: " + typeName+ " must specify the numberType attribute.");}- numberFieldName = v;+ numberTypeName = v;v = args.remove("booleanType");if (v != null) {- booleanFieldName = v;+ booleanTypeName = v;}}@Overridepublic void inform(IndexSchema schema) {this.schema = schema;- FieldType numberType = schema.getFieldTypeByName(numberFieldName);- FieldType booleanType = schema.getFieldTypeByName(booleanFieldName);+ FieldType numberType = schema.getFieldTypeByName(numberTypeName);+ FieldType booleanType = schema.getFieldTypeByName(booleanTypeName);if (numberType == null) {- throw new RuntimeException("Cannot find number fieldType: " + numberFieldName);+ throw new RuntimeException("Cannot find number fieldType: " + numberTypeName);}if (booleanType == null) {- throw new RuntimeException("Cannot find boolean fieldType: " + booleanFieldName);+ throw new RuntimeException("Cannot find boolean fieldType: " + booleanTypeName);}if (!(booleanType instanceof BoolField)) {throw new RuntimeException("Must be a BoolField: " + booleanType);@@ -75,33 +75,47 @@ public class BBoxField extends AbstractSpatialFieldType<BBoxStrategy> implementsthrow new RuntimeException("Must be TrieDoubleField: " + numberType);}+ //note: this only works for explicit fields, not dynamic fieldsList<SchemaField> fields = new ArrayList<>(schema.getFields().values());//copy, because we modify during iterationfor (SchemaField sf : fields) {if (sf.getType() == this) {String name = sf.getName();- register(schema, name + BBoxStrategy.SUFFIX_MINX, numberType);- register(schema, name + BBoxStrategy.SUFFIX_MAXX, numberType);- register(schema, name + BBoxStrategy.SUFFIX_MINY, numberType);- register(schema, name + BBoxStrategy.SUFFIX_MAXY, numberType);- register(schema, name + BBoxStrategy.SUFFIX_XDL, booleanType);+ registerSubFields(schema, name, numberType, booleanType);}}}+ private void registerSubFields(IndexSchema schema, String name, FieldType numberType, FieldType booleanType) {+ register(schema, name + BBoxStrategy.SUFFIX_MINX, numberType);+ register(schema, name + BBoxStrategy.SUFFIX_MAXX, numberType);+ register(schema, name + BBoxStrategy.SUFFIX_MINY, numberType);+ register(schema, name + BBoxStrategy.SUFFIX_MAXY, numberType);+ register(schema, name + BBoxStrategy.SUFFIX_XDL, booleanType);+ }++ // note: Registering the field is probably optional; it makes it show up in the schema browser and may have other+ // benefits.private void register(IndexSchema schema, String name, FieldType fieldType) {SchemaField sf = new SchemaField(name, fieldType);schema.getFields().put(sf.getName(), sf);}@Override- protected BBoxStrategy newSpatialStrategy(String s) {- BBoxStrategy strategy = new BBoxStrategy(ctx, s);+ protected BBoxStrategy newSpatialStrategy(String fieldName) {+ //if it's a dynamic field, we register the sub-fields now.+ FieldType numberType = schema.getFieldTypeByName(numberTypeName);+ FieldType booleanType = schema.getFieldTypeByName(booleanTypeName);+ if (schema.isDynamicField(fieldName)) {+ registerSubFields(schema, fieldName, numberType, booleanType);+ }++ BBoxStrategy strategy = new BBoxStrategy(ctx, fieldName);//Solr's FieldType ought to expose Lucene FieldType. Instead as a hack we create a Field with a dummy value.- SchemaField field = schema.getField(strategy.getFieldName() + BBoxStrategy.SUFFIX_MINX);+ final SchemaField solrNumField = new SchemaField("_", numberType);//dummy temporg.apache.lucene.document.FieldType luceneType =- (org.apache.lucene.document.FieldType) field.createField(0.0, 1.0f).fieldType();- //and annoyingly this field isn't going to have a docValues format because Solr uses a separate Field for that- if (field.hasDocValues()) {+ (org.apache.lucene.document.FieldType) solrNumField.createField(0.0, 1.0f).fieldType();+ //and annoyingly this Field isn't going to have a docValues format because Solr uses a separate Field for that+ if (solrNumField.hasDocValues()) {luceneType = new org.apache.lucene.document.FieldType(luceneType);luceneType.setDocValuesType(DocValuesType.NUMERIC);}diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-spatial.xml b/solr/core/src/test-files/solr/collection1/conf/schema-spatial.xmlindex f74319ec396..dc17bd1123d 100644--- a/solr/core/src/test-files/solr/collection1/conf/schema-spatial.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema-spatial.xml@@ -65,6 +65,8 @@<field name="pointvector" type="pointvector" /><field name="bbox" type="bbox" />+ <dynamicField name="bboxD_*" type="bbox" indexed="true" />+</fields><uniqueKey>id</uniqueKey>diff --git a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.javaindex 27dfe5b8069..fe2a84ec79b 100644--- a/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java+++ b/solr/core/src/test/org/apache/solr/search/TestSolr4Spatial2.java@@ -39,7 +39,7 @@ public class TestSolr4Spatial2 extends SolrTestCaseJ4 {@Testpublic void testBBox() throws Exception {- String fieldName = "bbox";+ String fieldName = random().nextBoolean() ? "bbox" : "bboxD_dynamic";assertU(adoc("id", "0"));//nothingassertU(adoc("id", "1", fieldName, "ENVELOPE(-10, 20, 15, 10)"));assertU(adoc("id", "2", fieldName, "ENVELOPE(22, 22, 10, 10)"));//pt@@ -48,14 +48,16 @@ public class TestSolr4Spatial2 extends SolrTestCaseJ4 {assertJQ(req("q", "{!field f="+fieldName+" filter=false score=overlapRatio " +"queryTargetProportion=0.25}" +"Intersects(ENVELOPE(10,25,12,10))",- "fl", "id,score",+ "fl", "*,score","debug", "results"),//explain info"/response/docs/[0]/id=='2'","/response/docs/[0]/score==0.75]","/response/docs/[1]/id=='1'","/response/docs/[1]/score==0.26666668]","/response/docs/[2]/id=='0'",- "/response/docs/[2]/score==0.0"+ "/response/docs/[2]/score==0.0",++ "/response/docs/[1]/" + fieldName + "=='ENVELOPE(-10, 20, 15, 10)'"//stored value);//minSideLength with point query