Apache Lucene open-source search software
SOLR-1365: New SweetSpotSimilarityFactory allows customizable TF/IDF based Similarity when you know the optimal "Sweet Spot" of values for the field length and TF scoring factors
Chris M. Hostetterdb6ca427baed
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 533ae893c6a..e08e111f3c1 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -94,6 +94,10 @@ New Features* SOLR-4210: Requests to a Collection that does not exist on the receiving nodeshould be proxied to a suitable node. (Mark Miller, Po Rui)+* SOLR-1365: New SweetSpotSimilarityFactory allows customizable TF/IDF based+ Similarity when you know the optimal "Sweet Spot" of values for the field+ length and TF scoring factors. (hossman)+Bug Fixes----------------------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.javaindex 61d150c13a3..4c3bdb74ed7 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,7 +38,7 @@ import org.apache.solr.schema.SimilarityFactory;* @lucene.experimental*/public class DefaultSimilarityFactory extends SimilarityFactory {- private boolean discountOverlaps;+ protected boolean discountOverlaps;@Overridepublic void init(SolrParams params) {diff --git a/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java b/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.javanew file mode 100644index 00000000000..6a6c582b974--- /dev/null+++ b/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java@@ -0,0 +1,187 @@+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.misc.SweetSpotSimilarity;+import org.apache.lucene.search.similarities.DefaultSimilarity; // jdoc+import org.apache.lucene.search.similarities.Similarity;+import org.apache.solr.common.params.SolrParams;+import org.apache.solr.schema.SimilarityFactory;+import org.apache.solr.common.SolrException;+import static org.apache.solr.common.SolrException.ErrorCode.*;+/**+ * <p>Factory for {@link SweetSpotSimilarity}.</p>+ * <p>+ * <code>SweetSpotSimilarity</code> is an extension of+ * {@link DefaultSimilarity} that provides additional tuning options for+ * specifying the "sweetspot" of optimal <code>tf</code> and+ * <code>lengthNorm</code> values in the source data.+ * </p>+ * <p>+ * In addition to the <code>discountOverlaps</code> init param supported by+ * {@link DefaultSimilarityFactory} The following sets of init params are+ * supported by this factory:+ * </p>+ * <ul>+ * <li>Length Norm Settings: <ul>+ * <li><code>lengthNormMin</code> (int)</li>+ * <li><code>lengthNormMax</code> (int)</li>+ * <li><code>lengthNormSteepness</code> (float)</li>+ * </ul></li>+ * <li>Baseline TF Settings: <ul>+ * <li><code>baselineTfBase</code> (float)</li>+ * <li><code>baselineTfMin</code> (float)</li>+ * </ul></li>+ * <li>Hyperbolic TF Settings: <ul>+ * <li><code>hyperbolicTfMin</code> (float)</li>+ * <li><code>hyperbolicTfMax</code> (float)</li>+ * <li><code>hyperbolicTfBase</code> (double)</li>+ * <li><code>hyperbolicTfOffset</code> (float)</li>+ * </ul></li>+ * </ul>+ * <p>+ * Note:+ * </p>+ * <ul>+ * <li>If any individual settings from one of the above mentioned sets+ * are specified, then all settings from that set must be specified.+ * </li>+ * <li>If Baseline TF settings are spcified, then Hyperbolic TF settings+ * are not permitted, and vice versa. (The settings specified will+ * determine wether {@link SweetSpotSimilarity#baselineTf} or+ * {@link SweetSpotSimilarity#hyperbolicTf} will be used.+ * </li>+ * </ul>+ * <p>+ * Example usage...+ * </p>+ * <pre class="prettyprint">+ * <!-- using baseline TF -->+ * <fieldType name="text_baseline" class="solr.TextField"+ * indexed="true" stored="false">+ * <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ * <similarity class="solr.SweetSpotSimilarityFactory">+ * <!-- TF -->+ * <float name="baselineTfMin">6.0</float>+ * <float name="baselineTfBase">1.5</float>+ * <!-- plateau norm -->+ * <int name="lengthNormMin">3</int>+ * <int name="lengthNormMax">5</int>+ * <float name="lengthNormSteepness">0.5</float>+ * </similarity>+ * </fieldType>+ *+ * <!-- using hyperbolic TF -->+ * <fieldType name="text_hyperbolic" class="solr.TextField"+ * indexed="true" stored="false" >+ * <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ * <similarity class="solr.SweetSpotSimilarityFactory">+ * <float name="hyperbolicTfMin">3.3</float>+ * <float name="hyperbolicTfMax">7.7</float>+ * <double name="hyperbolicTfBase">2.718281828459045</double> <!-- e -->+ * <float name="hyperbolicTfOffset">5.0</float>+ * <!-- plateau norm, shallower slope -->+ * <int name="lengthNormMin">1</int>+ * <int name="lengthNormMax">5</int>+ * <float name="lengthNormSteepness">0.2</float>+ * </similarity>+ * </fieldType>+ * </pre>+ * @see SweetSpotSimilarity The javadocs for the individual methods in+ * <code>SweetSpotSimilarity</code> for SVG diagrams showing how the+ * each function behaves with various settings/inputs.+ */+public class SweetSpotSimilarityFactory extends DefaultSimilarityFactory {+ private SweetSpotSimilarity sim = null;++ @Override+ public void init(SolrParams params) {+ super.init(params);++ Integer ln_min = params.getInt("lengthNormMin");+ Integer ln_max = params.getInt("lengthNormMax");+ Float ln_steep = params.getFloat("lengthNormSteepness");+ if (! allOrNoneNull(ln_min, ln_max, ln_steep)) {+ throw new SolrException(SERVER_ERROR, "Overriding default lengthNorm settings requires all to be specified: lengthNormMin, lengthNormMax, lengthNormSteepness");+ }++ Float hyper_min = params.getFloat("hyperbolicTfMin");+ Float hyper_max = params.getFloat("hyperbolicTfMax");+ Double hyper_base = params.getDouble("hyperbolicTfBase");+ Float hyper_offset = params.getFloat("hyperbolicTfOffset");+ if (! allOrNoneNull(hyper_min, hyper_max, hyper_base, hyper_offset)) {+ throw new SolrException(SERVER_ERROR, "Overriding default hyperbolicTf settings requires all to be specified: hyperbolicTfMin, hyperbolicTfMax, hyperbolicTfBase, hyperbolicTfOffset");+ }++ Float baseline_base = params.getFloat("baselineTfBase");+ Float baseline_min = params.getFloat("baselineTfMin");+ if (! allOrNoneNull(baseline_min, baseline_base)) {+ throw new SolrException(SERVER_ERROR, "Overriding default baselineTf settings requires all to be specified: baselineTfBase, baselineTfMin");+ }++ // sanity check that they aren't trying to use two diff tf impls+ if ((null != hyper_min) && (null != baseline_min)) {+ throw new SolrException(SERVER_ERROR, "Can not mix hyperbolicTf settings with baselineTf settings");+ }++ // pick Similarity impl based on wether hyper tf settings are set+ sim = (null != hyper_min) ? new HyperbolicSweetSpotSimilarity()+ : new SweetSpotSimilarity();++ if (null != ln_min) {+ // overlaps already handled by super factory+ sim.setLengthNormFactors(ln_min, ln_max, ln_steep, this.discountOverlaps);+ }++ if (null != hyper_min) {+ sim.setHyperbolicTfFactors(hyper_min, hyper_max, hyper_base, hyper_offset);+ }++ if (null != baseline_min) {+ sim.setBaselineTfFactors(baseline_base, baseline_min);+ }+ }++ @Override+ public Similarity getSimilarity() {+ assert sim != null : "SweetSpotSimilarityFactory was not initalized";+ return sim;+ }++ /**+ * Returns true if either: all of the specified arguments are null;+ * or none of the specified arguments are null+ */+ private static boolean allOrNoneNull(Object... args) {+ int nulls = 0;+ int objs = 0;+ for (Object o : args) {+ objs++;+ if (null == o) nulls++;+ }+ return (0 == nulls || nulls == objs);+ }++ private static final class HyperbolicSweetSpotSimilarity+ extends SweetSpotSimilarity {+ @Override+ public float tf(int freq) {+ return hyperbolicTf(freq);+ }+ };+}diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xmlnew file mode 100644index 00000000000..99028c18a7c--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml@@ -0,0 +1,48 @@+<?xml version="1.0" ?>+<!--+ 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="bad-schema-sweetspot-both-tf" version="1.0">+ <types>++ <fieldType name="string" class="solr.StrField" />++ <fieldType name="bad_ss_ft" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <!-- BAD: using both dypes of TF settings -->+ <float name="baselineTfMin">6.0</float>+ <float name="baselineTfBase">1.5</float>+ <float name="hyperbolicTfMin">3.3</float>+ <float name="hyperbolicTfMax">7.7</float>+ <double name="hyperbolicTfBase">5.0</double>+ <float name="hyperbolicTfOffset">5.0</float>+ </similarity>+ </fieldType>+ </types>+++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="false"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xmlnew file mode 100644index 00000000000..cf34ec8e21b--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml@@ -0,0 +1,44 @@+<?xml version="1.0" ?>+<!--+ 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="bad-schema-sweetspot-partial-baseline" version="1.0">+ <types>++ <fieldType name="string" class="solr.StrField" />++ <fieldType name="bad_ss_ft" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <!-- BAD: missing a baseline setting -->+ <float name="baselineTfMin">6.0</float>+ <!-- <float name="baselineTfBase">1.5</float> -->+ </similarity>+ </fieldType>+ </types>+++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="false"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xmlnew file mode 100644index 00000000000..61e18ad73c7--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml@@ -0,0 +1,46 @@+<?xml version="1.0" ?>+<!--+ 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="bad-schema-sweetspot-partial-hyperbolic" version="1.0">+ <types>++ <fieldType name="string" class="solr.StrField" />++ <fieldType name="bad_ss_ft" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <!-- BAD: missing a hyperbolic setting -->+ <float name="hyperbolicTfMin">3.3</float>+ <!-- <float name="hyperbolicTfMax">7.7</float> -->+ <double name="hyperbolicTfBase">5.0</double>+ <float name="hyperbolicTfOffset">5.0</float>+ </similarity>+ </fieldType>+ </types>+++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="false"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xmlnew file mode 100644index 00000000000..ef4e8042b3c--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml@@ -0,0 +1,45 @@+<?xml version="1.0" ?>+<!--+ 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="bad-schema-sweetspot-partial-norms" version="1.0">+ <types>++ <fieldType name="string" class="solr.StrField" />++ <fieldType name="bad_ss_ft" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <!-- BAD: missing a norm setting -->+ <int name="lengthNormMin">3</int>+ <!-- <int name="lengthNormMax">5</int> -->+ <float name="lengthNormSteepness">0.5</float>+ </similarity>+ </fieldType>+ </types>+++ <fields>+ <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="false"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml b/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xmlnew file mode 100644index 00000000000..350e2e90851--- /dev/null+++ b/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml@@ -0,0 +1,76 @@+<?xml version="1.0" ?>+<!--+ 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.+-->++<!-- Test schema file for SweetSpotSimilarityFactory -->++<schema name="test" version="1.0">+ <types>+ <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/>++ <!-- default parameters -->+ <fieldType name="text" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory"/>+ </fieldType>++ <!-- using baseline TF -->+ <fieldType name="text_baseline" class="solr.TextField"+ indexed="true" stored="false">+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <!-- TF -->+ <float name="baselineTfMin">6.0</float>+ <float name="baselineTfBase">1.5</float>+ <!-- plateau norm -->+ <int name="lengthNormMin">3</int>+ <int name="lengthNormMax">5</int>+ <float name="lengthNormSteepness">0.5</float>+ </similarity>+ </fieldType>++ <!-- using hyperbolic TF -->+ <fieldType name="text_hyperbolic" class="solr.TextField"+ indexed="true" stored="false" >+ <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>+ <similarity class="solr.SweetSpotSimilarityFactory">+ <float name="hyperbolicTfMin">3.3</float>+ <float name="hyperbolicTfMax">7.7</float>+ <double name="hyperbolicTfBase">2.718281828459045</double> <!-- e -->+ <float name="hyperbolicTfOffset">5.0</float>+ <!-- plateau norm, shallower slope -->+ <int name="lengthNormMin">1</int>+ <int name="lengthNormMax">5</int>+ <float name="lengthNormSteepness">0.2</float>+ </similarity>+ </fieldType>++ </types>++ <fields>+ <field name="id" type="int" indexed="true" stored="true" multiValued="false" required="false"/>+ <field name="text" type="text" />+ <field name="text_baseline" type="text_baseline" />+ <field name="text_hyperbolic" type="text_hyperbolic" />+ </fields>++ <defaultSearchField>text</defaultSearchField>+ <uniqueKey>id</uniqueKey>++ <similarity class="solr.SchemaSimilarityFactory"/>+</schema>diff --git a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.javaindex 746fd48f1b1..63f43651205 100644--- a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java+++ b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java@@ -89,4 +89,14 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase {doTest("bad-schema-unsupported-docValues.xml", "does not support doc values");}+ public void testSweetSpotSimBadConfig() throws Exception {+ doTest("bad-schema-sweetspot-both-tf.xml", "Can not mix");+ doTest("bad-schema-sweetspot-partial-baseline.xml",+ "Overriding default baselineTf");+ doTest("bad-schema-sweetspot-partial-hyperbolic.xml",+ "Overriding default hyperbolicTf");+ doTest("bad-schema-sweetspot-partial-norms.xml",+ "Overriding default lengthNorm");+ }+}diff --git a/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java b/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.javaindex d05867c7504..1fa62350120 100644--- a/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java+++ b/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java@@ -37,4 +37,17 @@ public abstract class BaseSimilarityTestCase extends SolrTestCaseJ4 {}return sim;}++ /**+ * Returns the similarity in use for the field,+ * after asserting that it implements the specified class+ */+ protected <T extends Similarity> T getSimilarity(String field,+ Class<T> clazz) {+ Similarity sim = getSimilarity(field);+ assertTrue("Similarity for Field " + field ++ " does not match expected class: " + clazz.getName(),+ clazz.isInstance(sim));+ return clazz.cast(sim);+ }}diff --git a/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java b/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.javanew file mode 100644index 00000000000..00bfe4e0293--- /dev/null+++ b/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java@@ -0,0 +1,109 @@+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.misc.SweetSpotSimilarity;+import org.apache.lucene.search.similarities.DefaultSimilarity;+import org.apache.lucene.search.similarities.Similarity;+import org.junit.BeforeClass;++/**+ * Tests {@link SweetSpotSimilarityFactory}+ */+public class TestSweetSpotSimilarityFactory extends BaseSimilarityTestCase {+ @BeforeClass+ public static void beforeClass() throws Exception {+ initCore("solrconfig-basic.xml","schema-sweetspot.xml");+ }++ /** default parameters */+ public void testDefaults() throws Exception {+ SweetSpotSimilarity sim = getSimilarity("text", SweetSpotSimilarity.class);++ // SSS tf w/defaults should behave just like DS+ DefaultSimilarity d = new DefaultSimilarity();+ for (int i = 0; i <=1000; i++) {+ assertEquals("tf: i="+i, d.tf(i), sim.tf(i), 0.0F);+ }++ // default norm sanity check+ assertEquals("norm 1", 1.00F, sim.computeLengthNorm(1), 0.0F);+ assertEquals("norm 4", 0.50F, sim.computeLengthNorm(4), 0.0F);+ assertEquals("norm 16", 0.25F, sim.computeLengthNorm(16), 0.0F);+ }++ /** baseline with parameters */+ public void testBaselineParameters() throws Exception {+ SweetSpotSimilarity sim = getSimilarity("text_baseline",+ SweetSpotSimilarity.class);++ DefaultSimilarity d = new DefaultSimilarity();++ // constant up to 6+ for (int i = 1; i <=6; i++) {+ assertEquals("tf i="+i, 1.5F, sim.tf(i), 0.0F);+ }+ // less then default sim above 6+ for (int i = 6; i <=1000; i++) {+ assertTrue("tf: i="+i+" : s="+sim.tf(i)++ " < d="+d.tf(i),+ sim.tf(i) < d.tf(i));+ }++ // norms: plateau from 3-5+ assertEquals("norm 1 == 7",+ sim.computeLengthNorm(1), sim.computeLengthNorm(7), 0.0F);+ assertEquals("norm 2 == 6",+ sim.computeLengthNorm(1), sim.computeLengthNorm(7), 0.0F);+ assertEquals("norm 3", 1.00F, sim.computeLengthNorm(3), 0.0F);+ assertEquals("norm 4", 1.00F, sim.computeLengthNorm(4), 0.0F);+ assertEquals("norm 5", 1.00F, sim.computeLengthNorm(5), 0.0F);+ assertTrue("norm 6 too high: " + sim.computeLengthNorm(6),+ sim.computeLengthNorm(6) < 1.0F);+ assertTrue("norm 7 higher then norm 6",+ sim.computeLengthNorm(7) < sim.computeLengthNorm(6));+ assertEquals("norm 20", 0.25F, sim.computeLengthNorm(20), 0.0F);+ }++ /** hyperbolic with parameters */+ public void testHyperbolicParameters() throws Exception {+ SweetSpotSimilarity sim = getSimilarity("text_hyperbolic",+ SweetSpotSimilarity.class);++ for (int i = 1; i <=1000; i++) {+ assertTrue("MIN tf: i="+i+" : s="+sim.tf(i),+ 3.3F <= sim.tf(i));+ assertTrue("MAX tf: i="+i+" : s="+sim.tf(i),+ sim.tf(i) <= 7.7F);+ }+ assertEquals("MID tf", 3.3F+(7.7F - 3.3F)/2.0F, sim.tf(5), 0.00001F);++ // norms: plateau from 1-5, shallow slope+ assertEquals("norm 1", 1.00F, sim.computeLengthNorm(1), 0.0F);+ assertEquals("norm 2", 1.00F, sim.computeLengthNorm(2), 0.0F);+ assertEquals("norm 3", 1.00F, sim.computeLengthNorm(3), 0.0F);+ assertEquals("norm 4", 1.00F, sim.computeLengthNorm(4), 0.0F);+ assertEquals("norm 5", 1.00F, sim.computeLengthNorm(5), 0.0F);+ assertTrue("norm 6 too high: " + sim.computeLengthNorm(6),+ sim.computeLengthNorm(6) < 1.0F);+ assertTrue("norm 7 higher then norm 6",+ sim.computeLengthNorm(7) < sim.computeLengthNorm(6));+ assertTrue("norm 20 not high enough: " + sim.computeLengthNorm(20),+ 0.25F < sim.computeLengthNorm(20));+ }+}