You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by br...@apache.org on 2019/11/28 15:15:24 UTC

[lucene-solr] branch master updated: SOLR-13968: Support postingsFormat and docValuesFormat in schema fields.

This is an automated email from the ASF dual-hosted git repository.

broustant pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/master by this push:
     new b4cc3c6  SOLR-13968: Support postingsFormat and docValuesFormat in schema fields.
b4cc3c6 is described below

commit b4cc3c6ad2db6555f7a1be89adb79397e5c76232
Author: Bruno Roustant <br...@salesforce.com>
AuthorDate: Thu Nov 28 16:13:10 2019 +0100

    SOLR-13968: Support postingsFormat and docValuesFormat in schema fields.
    
    Closes #1039
---
 solr/CHANGES.txt                                   |  2 +
 .../org/apache/solr/schema/FieldProperties.java    |  7 +-
 .../src/java/org/apache/solr/schema/FieldType.java |  2 -
 .../java/org/apache/solr/schema/SchemaField.java   | 20 +++++-
 .../collection1/conf/schema_postingsformat.xml     | 41 +++++++++++
 .../org/apache/solr/schema/TestSchemaField.java    | 84 ++++++++++++++++++++++
 6 files changed, 151 insertions(+), 5 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 458a002..687b526 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 e0bdfd5..b77fe2b 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 397f80f..1e6e348 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 100a963..23af619 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 0000000..7d452a5
--- /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 0000000..a7cd08e
--- /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());
+  }
+}