Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-13968: Support postingsFormat and docValuesFormat in schema fields.

Bruno Roustantb4cc3c6ad2db
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 458a0027b63..687b526726d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -154,6 +154,8 @@ Improvements
* SOLR-13969: Clean up and document AuditEvent API (janhoy)
+* SOLR-13968: Support postingsFormat and docValuesFormat in schema fields. (Bruno Roustant)
+
Optimizations
---------------------
(No changes)
diff --git a/solr/core/src/java/org/apache/solr/schema/FieldProperties.java b/solr/core/src/java/org/apache/solr/schema/FieldProperties.java
index e0bdfd54337..b77fe2bf101 100644
--- a/solr/core/src/java/org/apache/solr/schema/FieldProperties.java
+++ b/solr/core/src/java/org/apache/solr/schema/FieldProperties.java
@@ -73,6 +73,8 @@ public abstract class FieldProperties {
}
}
+ static final String POSTINGS_FORMAT = "postingsFormat";
+ static final String DOC_VALUES_FORMAT = "docValuesFormat";
/** Returns the symbolic name for the property. */
static String getPropertyName(int property) {
@@ -85,13 +87,16 @@ public abstract class FieldProperties {
return 1 << i;
}
}
- if (failOnError && !"default".equals(name)) {
+ if (failOnError && !isPropertyIgnored(name)) {
throw new IllegalArgumentException("Invalid field property: " + name);
} else {
return 0;
}
}
+ private static boolean isPropertyIgnored(String name) {
+ return name.equals("default") || name.equals(POSTINGS_FORMAT) || name.equals(DOC_VALUES_FORMAT);
+ }
static String propertiesToString(int properties) {
StringBuilder sb = new StringBuilder();
diff --git a/solr/core/src/java/org/apache/solr/schema/FieldType.java b/solr/core/src/java/org/apache/solr/schema/FieldType.java
index 397f80f139d..1e6e3484e06 100644
--- a/solr/core/src/java/org/apache/solr/schema/FieldType.java
+++ b/solr/core/src/java/org/apache/solr/schema/FieldType.java
@@ -985,8 +985,6 @@ public abstract class FieldType extends FieldProperties {
public static final String FILTER = "filter";
public static final String FILTERS = "filters";
- private static final String POSTINGS_FORMAT = "postingsFormat";
- private static final String DOC_VALUES_FORMAT = "docValuesFormat";
protected static final String AUTO_GENERATE_PHRASE_QUERIES = "autoGeneratePhraseQueries";
protected static final String ENABLE_GRAPH_QUERIES = "enableGraphQueries";
private static final String ARGS = "args";
diff --git a/solr/core/src/java/org/apache/solr/schema/SchemaField.java b/solr/core/src/java/org/apache/solr/schema/SchemaField.java
index 100a963c1c1..23af61917d7 100644
--- a/solr/core/src/java/org/apache/solr/schema/SchemaField.java
+++ b/solr/core/src/java/org/apache/solr/schema/SchemaField.java
@@ -48,7 +48,7 @@ public final class SchemaField extends FieldProperties implements IndexableField
boolean required = false; // this can't be final since it may be changed dynamically
/** Declared field property overrides */
- Map<String,?> args = Collections.emptyMap();
+ Map<String,Object> args = Collections.emptyMap();
/** Create a new SchemaField with the given name and type,
@@ -154,7 +154,23 @@ public final class SchemaField extends FieldProperties implements IndexableField
return type.getSortField(this, top);
}
- /**
+ /**
+ * Expert/advanced method to get the field {@link org.apache.lucene.codecs.PostingsFormat}.
+ * @return The {@code postingsFormat} declared; or null if unspecified.
+ */
+ public String getPostingsFormat() {
+ return (String) args.getOrDefault(POSTINGS_FORMAT, type.getPostingsFormat());
+ }
+
+ /**
+ * Expert/advanced method to get the field {@link org.apache.lucene.codecs.DocValuesFormat}.
+ * @return The {@code docValuesFormat} declared; or null if unspecified.
+ */
+ public String getDocValuesFormat() {
+ return (String) args.getOrDefault(DOC_VALUES_FORMAT, type.getDocValuesFormat());
+ }
+
+ /**
* Sanity checks that the properties of this field type are plausible
* for a field that may be used in sorting, throwing an appropriate
* exception (including the field name) if it is not. FieldType subclasses
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema_postingsformat.xml b/solr/core/src/test-files/solr/collection1/conf/schema_postingsformat.xml
new file mode 100644
index 00000000000..7d452a59d17
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/schema_postingsformat.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<schema name="test">
+
+ <fieldType name="str_none" class="solr.StrField"/>
+ <fieldType name="str_direct_asserting" class="solr.StrField" postingsFormat="Direct" docValuesFormat="Asserting"/>
+ <fieldType name="str_standard_simple" class="solr.StrField" postingsFormat="Lucene84" docValuesFormat="SimpleTextDocValuesFormat"/>
+
+ <field name="str_none_f" type="str_none"/>
+ <field name="str_direct_asserting_f" type="str_direct_asserting"/>
+ <field name="str_standard_simple_f" type="str_standard_simple"/>
+
+ <field name="str_none_lucene80_f" type="str_none" postingsFormat="Lucene80"/>
+ <field name="str_standard_lucene80_f" type="str_standard_simple" postingsFormat="Lucene80"/>
+
+ <field name="str_none_asserting_f" type="str_none" docValuesFormat="Asserting"/>
+ <field name="str_standard_asserting_f" type="str_standard_simple" docValuesFormat="Asserting"/>
+
+ <dynamicField name="*_lucene80" type="str_direct_asserting" postingsFormat="Lucene80"/>
+ <dynamicField name="*_direct" type="str_direct_asserting"/>
+ <dynamicField name="*_lucene70" type="str_none" postingsFormat="Lucene70"/>
+
+ <dynamicField name="*_asserting" type="str_none" docValuesFormat="Asserting"/>
+ <dynamicField name="*_simple" type="str_direct_asserting" docValuesFormat="SimpleTextDocValuesFormat"/>
+
+</schema>
diff --git a/solr/core/src/test/org/apache/solr/schema/TestSchemaField.java b/solr/core/src/test/org/apache/solr/schema/TestSchemaField.java
new file mode 100644
index 00000000000..a7cd08ed893
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/schema/TestSchemaField.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.schema;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+public class TestSchemaField extends SolrTestCaseJ4 {
+
+ @BeforeClass
+ public static void create() throws Exception {
+ initCore("solrconfig_codec.xml","schema_postingsformat.xml");
+ }
+
+ @Before
+ public void cleanup() {
+ clearIndex();
+ }
+
+ public void testFieldTypes() {
+ assertFieldTypeFormats("str_none", null, null);
+ assertFieldTypeFormats("str_direct_asserting", "Direct", "Asserting");
+ assertFieldTypeFormats("str_standard_simple", "Lucene84", "SimpleTextDocValuesFormat");
+ }
+
+ private void assertFieldTypeFormats(String fieldTypeName, String expectedPostingsFormat, String expectedDocValuesFormat) {
+ FieldType ft = h.getCore().getLatestSchema().getFieldTypeByName(fieldTypeName);
+ assertNotNull("Field type " + fieldTypeName + " not found - schema got changed?", ft);
+ assertEquals("Field type " + ft.getTypeName() + " wrong " + FieldProperties.POSTINGS_FORMAT
+ + " - schema got changed?",
+ expectedPostingsFormat, ft.getNamedPropertyValues(true).get(FieldProperties.POSTINGS_FORMAT));
+ assertEquals("Field type " + ft.getTypeName() + " wrong " + FieldProperties.DOC_VALUES_FORMAT
+ + " - schema got changed?",
+ expectedDocValuesFormat, ft.getNamedPropertyValues(true).get(FieldProperties.DOC_VALUES_FORMAT));
+ }
+
+ public void testFields() {
+ assertFieldFormats("str_none_f", null, null);
+ assertFieldFormats("str_direct_asserting_f", "Direct", "Asserting");
+ assertFieldFormats("str_standard_simple_f", "Lucene84", "SimpleTextDocValuesFormat");
+
+ assertFieldFormats("str_none_lucene80_f", "Lucene80", null);
+ assertFieldFormats("str_standard_lucene80_f", "Lucene80", "SimpleTextDocValuesFormat");
+
+ assertFieldFormats("str_none_asserting_f", null, "Asserting");
+ assertFieldFormats("str_standard_asserting_f", "Lucene84", "Asserting");
+ }
+
+ public void testDynamicFields() {
+ assertFieldFormats("any_lucene80", "Lucene80", "Asserting");
+ assertFieldFormats("any_direct", "Direct", "Asserting");
+ assertFieldFormats("any_lucene70", "Lucene70", null);
+
+ assertFieldFormats("any_asserting", null, "Asserting");
+ assertFieldFormats("any_simple", "Direct", "SimpleTextDocValuesFormat");
+ }
+
+ private void assertFieldFormats(String fieldName, String expectedPostingsFormat, String expectedDocValuesFormat) {
+ SchemaField f = h.getCore().getLatestSchema().getField(fieldName);
+ assertNotNull("Field " + fieldName + " not found - schema got changed?", f);
+ assertEquals("Field " + f.getName() + " wrong " + FieldProperties.POSTINGS_FORMAT
+ + " - schema got changed?",
+ expectedPostingsFormat, f.getPostingsFormat());
+ assertEquals("Field " + f.getName() + " wrong " + FieldProperties.DOC_VALUES_FORMAT
+ + " - schema got changed?",
+ expectedDocValuesFormat, f.getDocValuesFormat());
+ }
+}