lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hoss...@apache.org
Subject svn commit: r1715945 - in /lucene/dev/branches/branch_5x: ./ solr/ solr/core/ solr/core/src/java/org/apache/solr/search/ solr/core/src/java/org/apache/solr/search/similarities/ solr/core/src/test-files/solr/collection1/conf/ solr/core/src/test/org/apac...
Date Mon, 23 Nov 2015 20:20:26 GMT
Author: hossman
Date: Mon Nov 23 20:20:26 2015
New Revision: 1715945

URL: http://svn.apache.org/viewvc?rev=1715945&view=rev
Log:
SOLR-8329: SchemaSimilarityFactory now supports a 'defaultSimFromFieldType' init option for
using a fieldType name to identify which Similarity to use as a default (merge r1715881)

Added:
    lucene/dev/branches/branch_5x/solr/core/src/test-files/solr/collection1/conf/bad-schema-sim-default-does-not-exist.xml
      - copied unchanged from r1715881, lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sim-default-does-not-exist.xml
    lucene/dev/branches/branch_5x/solr/core/src/test-files/solr/collection1/conf/bad-schema-sim-default-has-no-explicit-sim.xml
      - copied unchanged from r1715881, lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sim-default-has-no-explicit-sim.xml
    lucene/dev/branches/branch_5x/solr/core/src/test-files/solr/collection1/conf/schema-sim-default-override.xml
      - copied unchanged from r1715881, lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sim-default-override.xml
    lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/search/similarities/TestPerFieldSimilarityWithDefaultOverride.java
      - copied unchanged from r1715881, lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestPerFieldSimilarityWithDefaultOverride.java
Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/solr/   (props changed)
    lucene/dev/branches/branch_5x/solr/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/solr/core/   (props changed)
    lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/   (props changed)
    lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/similarities/SchemaSimilarityFactory.java
    lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java

Modified: lucene/dev/branches/branch_5x/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/CHANGES.txt?rev=1715945&r1=1715944&r2=1715945&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/solr/CHANGES.txt Mon Nov 23 20:20:26 2015
@@ -96,6 +96,9 @@ New Features
 * SOLR-6168: Add a 'sort' local param to the collapse QParser to support using complex sort
options
   to select the representitive doc for each collapsed group. (Umesh Prasad, hossman)
 
+* SOLR-8329: SchemaSimilarityFactory now supports a 'defaultSimFromFieldType' init option
for using
+  a fieldType name to identify which Similarity to use as a default. (hossman)
+
 Bug Fixes
 ----------------------
 

Modified: lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/similarities/SchemaSimilarityFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/similarities/SchemaSimilarityFactory.java?rev=1715945&r1=1715944&r2=1715945&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/similarities/SchemaSimilarityFactory.java
(original)
+++ lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/search/similarities/SchemaSimilarityFactory.java
Mon Nov 23 20:20:26 2015
@@ -20,6 +20,8 @@ package org.apache.solr.search.similarit
 import org.apache.lucene.search.similarities.ClassicSimilarity;
 import org.apache.lucene.search.similarities.PerFieldSimilarityWrapper;
 import org.apache.lucene.search.similarities.Similarity;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.SolrException.ErrorCode;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.core.SolrCore;
 import org.apache.solr.schema.FieldType;
@@ -30,27 +32,57 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * SimilarityFactory that returns a {@link PerFieldSimilarityWrapper}
- * that delegates to the field type, if it's configured, otherwise
- * {@link ClassicSimilarity}.
+ * <code>SimilarityFactory</code> that returns a global {@link PerFieldSimilarityWrapper}
+ * that delegates to the field type, if it's configured.  For field type's that
+ * do not have a <code>Similarity</code> explicitly configured, the global <code>Similarity</code>

+ * will use per fieldtype defaults -- either based on an explicitly configured 
+ * <code>defaultSimFromFieldType</code> of an implicit {@link ClassicSimilarity}.
  *
  * <p>
- * <b>NOTE:</b> Users should be aware that in addition to supporting 
- * <code>Similarity</code> configurations specified on individual 
- * field types, this factory also differs in behavior from 
- * {@link ClassicSimilarityFactory} because of other differences in the 
- * implementations of <code>PerFieldSimilarityWrapper</code> and 
- * {@link ClassicSimilarity} - notably in methods such as 
- * {@link Similarity#coord} and {@link Similarity#queryNorm}.  
+ * The <code>defaultSimFromFieldType</code> option accepts the name of any fieldtype,
and uses 
+ * whatever <code>Similarity</code> is explicitly configured for that fieldType
as thedefault for 
+ * all other field types.  For example:
+ * </p>
+ * <pre class="prettyprint">
+ *   &lt;similarity class="solr.SchemaSimilarityFactory" &gt;
+ *     &lt;str name="defaultSimFromFieldType"&gt;type-using-custom-dfr&lt;/str&gt;
+ *   &lt;/similarity&gt;
+ *   ...
+ *   &lt;fieldType name="type-using-custom-dfr" class="solr.TextField"&gt;
+ *     ...
+ *     &lt;similarity class="solr.DFRSimilarityFactory"&gt;
+ *       &lt;str name="basicModel"&gt;I(F)&lt;/str&gt;
+ *       &lt;str name="afterEffect"&gt;B&lt;/str&gt;
+ *       &lt;str name="normalization"&gt;H3&lt;/str&gt;
+ *       &lt;float name="mu"&gt;900&lt;/float&gt;
+ *     &lt;/similarity&gt;
+ *   &lt;/fieldType&gt;
+ * </pre>
+ * <p>
+ * In the example above, any fieldtypes that do not define their own <code>&lt;/similarity/&gt;</code>

+ * will use the <code>Similarity</code> configured for the <code>type-using-custom-dfr</code>.
+ * </p>
+ * 
+ * <p>
+ * <b>NOTE:</b> Users should be aware that even when this factory uses a single
default 
+ * <code>Similarity</code> for some or all fields in a Query, the behavior can
be inconsistent 
+ * with the behavior of explicitly configuring that same <code>Similarity</code>
globally, because 
+ * of differences in how some multi-field / multi-clause behavior is defined in 
+ * <code>PerFieldSimilarityWrapper</code>.  In particular please consider carefully
the documentation 
+ * &amp; implementation of {@link Similarity#coord} and {@link Similarity#queryNorm}
in 
+ * {@link ClassicSimilarity} compared to {@link PerFieldSimilarityWrapper}
  * </p>
  *
  * @see FieldType#getSimilarity
  */
 public class SchemaSimilarityFactory extends SimilarityFactory implements SolrCoreAware {
-  private Similarity similarity; // set by init
-  private Similarity defaultSimilarity = new ClassicSimilarity();
 
-  private volatile SolrCore core;
+  private static final String INIT_OPT = "defaultSimFromFieldType";
+  
+  private String defaultSimFromFieldType; // set by init, if null use sensible implicit default
+  
+  private volatile SolrCore core; // set by inform(SolrCore)
+  private volatile Similarity similarity; // lazy instantiated
 
   @Override
   public void inform(SolrCore core) {
@@ -59,19 +91,8 @@ public class SchemaSimilarityFactory ext
   
   @Override
   public void init(SolrParams args) {
+    defaultSimFromFieldType = args.get(INIT_OPT, null);
     super.init(args);
-    similarity = new PerFieldSimilarityWrapper() {
-      @Override
-      public Similarity get(String name) {
-        FieldType fieldType = core.getLatestSchema().getFieldTypeNoEx(name);
-        if (fieldType == null) {
-          return defaultSimilarity;
-        } else {
-          Similarity similarity = fieldType.getSimilarity();
-          return similarity == null ? defaultSimilarity : similarity;
-        }
-      }
-    };
   }
 
   @Override
@@ -79,6 +100,45 @@ public class SchemaSimilarityFactory ext
     if (null == core) {
       throw new IllegalStateException("SchemaSimilarityFactory can not be used until SolrCoreAware.inform
has been called");
     }
+    if (null == similarity) {
+      // Need to instantiate lazily, can't do this in inform(SolrCore) because of chicken/egg
+      // circular initialization hell with core.getLatestSchema() to lookup defaultSimFromFieldType
+      
+      Similarity defaultSim = null;
+      if (null == defaultSimFromFieldType) {
+        // nothing configured, choose a sensible implicit default...
+        defaultSim = new ClassicSimilarity();
+      } else {
+        FieldType defSimFT = core.getLatestSchema().getFieldTypeByName(defaultSimFromFieldType);
+        if (null == defSimFT) {
+          throw new SolrException(ErrorCode.SERVER_ERROR,
+                                  "SchemaSimilarityFactory configured with " + INIT_OPT +
"='" +
+                                  defaultSimFromFieldType + "' but that <fieldType>
does not exist");
+                                  
+        }
+        defaultSim = defSimFT.getSimilarity();
+        if (null == defaultSim) {
+          throw new SolrException(ErrorCode.SERVER_ERROR,
+                                  "SchemaSimilarityFactory configured with " + INIT_OPT +
"='" + 
+                                  defaultSimFromFieldType +
+                                  "' but that <fieldType> does not define a <similarity>");
+        }
+      }
+      assert null != defaultSim;
+      final Similarity defaultSimilarity = defaultSim;
+      similarity = new PerFieldSimilarityWrapper() {
+        @Override
+        public Similarity get(String name) {
+          FieldType fieldType = core.getLatestSchema().getFieldTypeNoEx(name);
+          if (fieldType == null) {
+            return defaultSimilarity;
+          } else {
+            Similarity similarity = fieldType.getSimilarity();
+            return similarity == null ? defaultSimilarity : similarity;
+          }
+        }
+      };
+    }
     return similarity;
   }
 }

Modified: lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java?rev=1715945&r1=1715944&r2=1715945&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
(original)
+++ lucene/dev/branches/branch_5x/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
Mon Nov 23 20:20:26 2015
@@ -119,4 +119,14 @@ public class BadIndexSchemaTest extends
     doTest("bad-schema-bogus-analysis-parameters.xml", "Unknown parameters");
   }
 
+  public void testSimDefaultFieldTypeHasNoExplicitSim() throws Exception {
+    doTest("bad-schema-sim-default-has-no-explicit-sim.xml",
+           "ft-has-no-sim");
+  }
+  
+  public void testSimDefaultFieldTypeDoesNotExist() throws Exception {
+    doTest("bad-schema-sim-default-does-not-exist.xml",
+           "ft-does-not-exist");
+  }
+  
 }



Mime
View raw message