Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-5561: Fix implicit DefaultSimilarityFactory initialization in IndexSchema to properly specify discountOverlap option

Chris M. Hostetter601f536f9a67
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index f690ef6b51a..cb820429e01 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -71,6 +71,13 @@ Upgrading from Solr 4.6.0
* CloudSolrServer and LBHttpSolrServer no longer declare MalformedURLException
as thrown from their constructors.
+
+* Due to a bug in previous versions the default value of the 'discountOverlap' property
+ of DefaultSimilarity was not being set appropriately if you were using the implicit
+ DefaultSimilarityFactory instead of explicitly configuring it. To preserve
+ consistent behavior for people who upgrade, the implicit behavior is now contingent
+ on the <luceneMatchVersion/> -- discountOverlap=false for 4.6 and below,
+ discountOverlap=true for 4.7 and above. See SOLR-5561 for more information.
Detailed Change List
----------------------
@@ -305,6 +312,10 @@ Bug Fixes
* SOLR-5709: Highlighting grouped duplicate docs from different shards with
group.limit > 1 throws ArrayIndexOutOfBoundsException. (Steve Rowe)
+* SOLR-5561: Fix implicit DefaultSimilarityFactory initialization in IndexSchema
+ to properly specify discountOverlap option.
+ (Isaac Hebsh, Ahmet Arslan, Vitaliy Zhovtyuk, hossman)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index 241aa9a7909..4a9e4540eb6 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -477,6 +477,12 @@ public class IndexSchema {
similarityFactory = readSimilarity(loader, node);
if (similarityFactory == null) {
similarityFactory = new DefaultSimilarityFactory();
+ final NamedList similarityParams = new NamedList();
+ Version luceneVersion = getDefaultLuceneMatchVersion();
+ if (!luceneVersion.onOrAfter(Version.LUCENE_47)) {
+ similarityParams.add(DefaultSimilarityFactory.DISCOUNT_OVERLAPS, false);
+ }
+ similarityFactory.init(SolrParams.toSolrParams(similarityParams));
} else {
isExplicitSimilarity = true;
}
diff --git a/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java
index 4c3bdb74ed7..1f1b911aac8 100644
--- a/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java
+++ b/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java
@@ -38,12 +38,22 @@ import org.apache.solr.schema.SimilarityFactory;
* @lucene.experimental
*/
public class DefaultSimilarityFactory extends SimilarityFactory {
- protected boolean discountOverlaps;
+
+ /** Init param name for specifying the value to use in
+ * {@link DefaultSimilarity#setDiscountOverlaps(boolean)}
+ */
+ public static final String DISCOUNT_OVERLAPS = "discountOverlaps";
+
+ /**
+ * Controls the value of {@link DefaultSimilarity#setDiscountOverlaps(boolean)}
+ * on newly constructed instances of {@link DefaultSimilarity}
+ */
+ protected boolean discountOverlaps = true;
@Override
public void init(SolrParams params) {
super.init(params);
- discountOverlaps = params.getBool("discountOverlaps", true);
+ discountOverlaps = params.getBool(DISCOUNT_OVERLAPS, true);
}
@Override
diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestNonDefinedSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestNonDefinedSimilarityFactory.java
new file mode 100644
index 00000000000..8488efd0366
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/search/similarities/TestNonDefinedSimilarityFactory.java
@@ -0,0 +1,59 @@
+package org.apache.solr.search.similarities;
+
+/*
+ * 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.
+ */
+
+import org.apache.lucene.search.similarities.DefaultSimilarity;
+import org.apache.lucene.search.similarities.Similarity;
+import org.apache.lucene.util.Version;
+import org.junit.After;
+
+/**
+ * Verifies that the default behavior of the implicit {@link DefaultSimilarityFactory}
+ * (ie: no similarity configured in schema.xml at all) is consistnent with
+ * expectations based on the luceneMatchVersion
+ * @see <a href="https://issues.apache.org/jira/browse/SOLR-5561">SOLR-5561</a>
+ */
+public class TestNonDefinedSimilarityFactory extends BaseSimilarityTestCase {
+
+ @After
+ public void cleanup() throws Exception {
+ deleteCore();
+ }
+
+ public void testCurrent() throws Exception {
+ // no sys prop set, rely on LUCENE_CURRENT
+ initCore("solrconfig-basic.xml","schema-tiny.xml");
+ DefaultSimilarity sim = getSimilarity("text", DefaultSimilarity.class);
+ assertEquals(true, sim.getDiscountOverlaps());
+ }
+
+ public void test47() throws Exception {
+ System.setProperty("tests.luceneMatchVersion", Version.LUCENE_47.toString());
+ initCore("solrconfig-basic.xml","schema-tiny.xml");
+ DefaultSimilarity sim = getSimilarity("text", DefaultSimilarity.class);
+ assertEquals(true, sim.getDiscountOverlaps());
+ }
+
+ public void test46() throws Exception {
+ System.setProperty("tests.luceneMatchVersion", Version.LUCENE_46.toString());
+ initCore("solrconfig-basic.xml","schema-tiny.xml");
+ DefaultSimilarity sim = getSimilarity("text", DefaultSimilarity.class);
+ assertEquals(false, sim.getDiscountOverlaps());
+ }
+
+}