Apache Solr open-source search software
SOLR-15570: Check stored or docValues when merging fields from the Luke schema response (#244)
Timothy Potter8e74f9fa2db8
diff --git a/solr/core/src/java/org/apache/solr/handler/sql/SolrSchema.java b/solr/core/src/java/org/apache/solr/handler/sql/SolrSchema.javaindex d527018f6db..b5c99e359c9 100644--- a/solr/core/src/java/org/apache/solr/handler/sql/SolrSchema.java+++ b/solr/core/src/java/org/apache/solr/handler/sql/SolrSchema.java@@ -19,6 +19,7 @@ package org.apache.solr.handler.sql;import java.io.Closeable;import java.io.IOException;import java.util.Date;+import java.util.EnumSet;import java.util.HashMap;import java.util.Map;import java.util.Properties;@@ -43,6 +44,7 @@ import org.apache.solr.client.solrj.response.LukeResponse;import org.apache.solr.common.cloud.Aliases;import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.luke.FieldFlag;import org.apache.solr.schema.DateValueFieldType;import org.apache.solr.schema.DoubleValueFieldType;import org.apache.solr.schema.FloatValueFieldType;@@ -127,6 +129,10 @@ class SolrSchema extends AbstractSchema implements Closeable {PKIAuthenticationPlugin.withServerIdentity(false);}}++ private boolean isStoredOrDocValues(final EnumSet<FieldFlag> flags) {+ return flags != null && (flags.contains(FieldFlag.STORED) || flags.contains(FieldFlag.DOC_VALUES));+ }RelProtoDataType getRelDataType(String collection) {// Temporary type factory, just for the duration of this method. Allowable@@ -139,8 +145,12 @@ class SolrSchema extends AbstractSchema implements Closeable {Map<String, LukeResponse.FieldInfo> fieldsInUseMap = getFieldInfo(collection);LukeResponse schema = getSchema(collection);+ // Only want fields that are stored or have docValues enabled+ Map<String, LukeResponse.FieldInfo> storedFields = schema.getFieldInfo().entrySet().stream()+ .filter(e -> isStoredOrDocValues(e.getValue().getFlags()))+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));// merge the actual fields in use returned by Luke with the declared fields in the schema that are empty- Map<String, LukeResponse.FieldInfo> combinedFields = Stream.of(fieldsInUseMap, schema.getFieldInfo())+ Map<String, LukeResponse.FieldInfo> combinedFields = Stream.of(fieldsInUseMap, storedFields).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));diff --git a/solr/core/src/test-files/solr/configsets/sql/conf/schema.xml b/solr/core/src/test-files/solr/configsets/sql/conf/schema.xmlindex f0872a8ac61..0638a79b928 100644--- a/solr/core/src/test-files/solr/configsets/sql/conf/schema.xml+++ b/solr/core/src/test-files/solr/configsets/sql/conf/schema.xml@@ -66,6 +66,9 @@<field name="pdatex" type="pdatex" indexed="true" stored="true"/><field name="pdatexs" type="pdatex" indexed="true" stored="true" multiValued="true"/>+ <field name="notstored" type="string" indexed="true" docValues="false" stored="false"/>+ <field name="dvonly" type="string" docValues="true" stored="false"/>+<!-- Field type demonstrating an Analyzer failure --><fieldType name="failtype1" class="solr.TextField"><analyzer type="index">diff --git a/solr/core/src/test/org/apache/solr/handler/TestSQLHandler.java b/solr/core/src/test/org/apache/solr/handler/TestSQLHandler.javaindex f7209a1744d..a1a2882998a 100644--- a/solr/core/src/test/org/apache/solr/handler/TestSQLHandler.java+++ b/solr/core/src/test/org/apache/solr/handler/TestSQLHandler.java@@ -2236,16 +2236,21 @@ public class TestSQLHandler extends SolrCloudTestCase {@Testpublic void testSelectEmptyField() throws Exception {new UpdateRequest()- .add("id", "01")- .add("id", "02")- .add("id", "03")- .add("id", "04")- .add("id", "05")+ .add("id", "01", "notstored", "X", "dvonly", "Y")+ .add("id", "02", "notstored", "X", "dvonly", "Y")+ .add("id", "03", "notstored", "X", "dvonly", "Y")+ .add("id", "04", "notstored", "X", "dvonly", "Y")+ .add("id", "05", "notstored", "X", "dvonly", "Y").commit(cluster.getSolrClient(), COLLECTIONORALIAS);// stringx is declared in the schema but has no docsexpectResults("SELECT id, stringx FROM $ALIAS", 5);+ expectResults("SELECT id, stringx FROM $ALIAS LIMIT 10", 5);+ expectResults("SELECT id, stringx, dvonly FROM $ALIAS", 5);+ expectResults("SELECT id, stringx, dvonly FROM $ALIAS LIMIT 10", 5);+// notafield_i matches a dynamic field pattern but has no docs, so don't allow thisexpectThrows(IOException.class, () -> expectResults("SELECT id, stringx, notafield_i FROM $ALIAS", 5));+ expectThrows(IOException.class, () -> expectResults("SELECT id, stringx, notstored FROM $ALIAS", 5));}}