diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index bec534244ea..d4d35f60fec 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -186,6 +186,9 @@ Improvements * SOLR-14802: geodist: Support most (all?) spatial field types as an argument like LLPSF, SRPTFT, and others. (Tom Edge, Craig Wrigglesworth) +* SOLR-14859: DateRangeField now throws errors when invalid field/fieldType options specified; no longer silently accepts incompatible option values + (Jason Gerlowski, Chris Hostetter, Munendra S N) + Optimizations --------------------- diff --git a/solr/core/src/java/org/apache/solr/schema/AbstractSpatialPrefixTreeFieldType.java b/solr/core/src/java/org/apache/solr/schema/AbstractSpatialPrefixTreeFieldType.java index eb7f1911152..f7b9912ccd9 100644 --- a/solr/core/src/java/org/apache/solr/schema/AbstractSpatialPrefixTreeFieldType.java +++ b/solr/core/src/java/org/apache/solr/schema/AbstractSpatialPrefixTreeFieldType.java @@ -18,14 +18,18 @@ package org.apache.solr.schema; import java.io.IOException; import java.lang.invoke.MethodHandles; +import java.util.HashMap; +import java.util.Locale; import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.index.IndexOptions; import org.apache.lucene.spatial.prefix.PrefixTreeStrategy; import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree; import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTreeFactory; import org.apache.lucene.spatial.query.SpatialArgsParser; +import org.apache.solr.common.SolrException; import org.apache.solr.util.MapListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,12 +43,54 @@ public abstract class AbstractSpatialPrefixTreeFieldType FIELD_TYPE_INVARIANTS = new HashMap<>(); + static { + FIELD_TYPE_INVARIANTS.put("omitNorms", "true"); + FIELD_TYPE_INVARIANTS.put("termPositions", "false"); + FIELD_TYPE_INVARIANTS.put("termOffsets", "false"); + FIELD_TYPE_INVARIANTS.put("omitTermFreqAndPositions", "true"); + FIELD_TYPE_INVARIANTS.put("omitPositions", "true"); + } + protected SpatialPrefixTree grid; private Double distErrPct; private Integer defaultFieldValuesArrayLen; private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + @Override + public void setArgs(IndexSchema schema, Map args) { + for (Map.Entry entry : FIELD_TYPE_INVARIANTS.entrySet()) { + final String key = entry.getKey(); + final String hardcodedValue = entry.getValue(); + final String userConfiguredValue = args.get(entry.getKey()); + + if (args.containsKey(key)) { + if (userConfiguredValue.equals(hardcodedValue)) { + log.warn("FieldType {} does not allow {} to be specified in schema, hardcoded behavior is {}={}", + getClass().getSimpleName(), key, key, hardcodedValue); + } else { + final String message = String.format(Locale.ROOT, "FieldType %s is incompatible with %s=%s; hardcoded " + + "behavior is %s=%s. Remove specification in schema", + getClass().getSimpleName(), key, userConfiguredValue, key, hardcodedValue); + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message); + } + } + } + args.putAll(FIELD_TYPE_INVARIANTS); + + super.setArgs(schema, args); + } + @Override protected void init(IndexSchema schema, Map args) { super.init(schema, args); @@ -71,6 +117,27 @@ public abstract class AbstractSpatialPrefixTreeFieldType + + + + + + + + + + + + + + + + + + + diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-daterangefield-type-options.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-daterangefield-type-options.xml new file mode 100644 index 00000000000..9489cf13aa5 --- /dev/null +++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-daterangefield-type-options.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java index c829c17c667..905868a031b 100644 --- a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java +++ b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java @@ -186,4 +186,9 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase { public void testSchemaWithDefaultSearchField() throws Exception { doTest("bad-schema-defaultsearchfield.xml", "Setting defaultSearchField in schema not supported since Solr 7"); } + + public void testDateRangeFieldWithInvalidOptions() throws Exception { + doTest("bad-schema-daterangefield-type-options.xml", "FieldType DateRangeField is incompatible with omitNorms=false"); + doTest("bad-schema-daterangefield-instance-options.xml", "daterange_field of type DateRangeField is incompatible with omitNorms=false"); + } } diff --git a/solr/core/src/test/org/apache/solr/schema/DateRangeFieldTest.java b/solr/core/src/test/org/apache/solr/schema/DateRangeFieldTest.java index edd25b0352e..c7b935128b9 100644 --- a/solr/core/src/test/org/apache/solr/schema/DateRangeFieldTest.java +++ b/solr/core/src/test/org/apache/solr/schema/DateRangeFieldTest.java @@ -35,6 +35,7 @@ public class DateRangeFieldTest extends SolrTestCaseJ4 { assertU(adoc("id", "3", "dateRange", "2020-05-21T12:00:00.000Z/DAY"));//DateMath syntax assertU(commit()); + //ensure stored value resolves datemath assertQ(req("q", "id:1", "fl", "dateRange"), "//result/doc/arr[@name='dateRange']/str[.='2014-05-21T12:00:00Z']");//no 000 ms assertQ(req("q", "id:2", "fl", "dateRange"), "//result/doc/arr[@name='dateRange']/str[.='[2000 TO 2014-05-21]']");//a range; same @@ -50,7 +51,10 @@ public class DateRangeFieldTest extends SolrTestCaseJ4 { assertQ(req("q", "dateRange:[1998 TO 2000}"), xpathMatches(0));//exclusive end, so we barely miss one doc + //show without local-params + assertQ(req("q", "dateRange:[* TO *]"), xpathMatches(0, 1, 2, 3)); + assertQ(req("q", "dateRange:*"), xpathMatches(0, 1, 2, 3)); assertQ(req("q", "dateRange:\"2014-05-21T12:00:00.000Z\""), xpathMatches(0, 1, 2)); assertQ(req("q", "dateRange:[1999 TO 2001]"), xpathMatches(0, 2)); } diff --git a/solr/core/src/test/org/apache/solr/schema/SpatialRPTFieldTypeTest.java b/solr/core/src/test/org/apache/solr/schema/SpatialRPTFieldTypeTest.java index c780d73454a..05166baf5af 100644 --- a/solr/core/src/test/org/apache/solr/schema/SpatialRPTFieldTypeTest.java +++ b/solr/core/src/test/org/apache/solr/schema/SpatialRPTFieldTypeTest.java @@ -262,7 +262,8 @@ public class SpatialRPTFieldTypeTest extends AbstractBadConfigTestBase { } fieldType.init(oldSchema, rptMap); fieldType.setTypeName("location_rpt"); - SchemaField newField = new SchemaField("geo", fieldType, SchemaField.STORED | SchemaField.INDEXED, null); + SchemaField newField = new SchemaField("geo", fieldType, SchemaField.STORED | SchemaField.INDEXED | SchemaField.OMIT_NORMS | SchemaField.OMIT_TF_POSITIONS, + null); IndexSchema newSchema = oldSchema.addField(newField); h.getCore().setLatestSchema(newSchema);