You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2013/02/27 02:58:08 UTC
svn commit: r1450577 - in /lucene/dev/trunk/solr: ./
core/src/java/org/apache/solr/search/similarities/
core/src/test-files/solr/collection1/conf/
core/src/test/org/apache/solr/schema/
core/src/test/org/apache/solr/search/similarities/
Author: hossman
Date: Wed Feb 27 01:58:07 2013
New Revision: 1450577
URL: http://svn.apache.org/r1450577
Log:
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
Added:
lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java (with props)
lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml (with props)
lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml (with props)
lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml (with props)
lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml (with props)
lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml (with props)
lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java (with props)
Modified:
lucene/dev/trunk/solr/CHANGES.txt
lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java
lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1450577&r1=1450576&r2=1450577&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Wed Feb 27 01:58:07 2013
@@ -94,6 +94,10 @@ New Features
* SOLR-4210: Requests to a Collection that does not exist on the receiving node
should 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
----------------------
Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java?rev=1450577&r1=1450576&r2=1450577&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/DefaultSimilarityFactory.java Wed Feb 27 01:58:07 2013
@@ -38,7 +38,7 @@ import org.apache.solr.schema.Similarity
* @lucene.experimental
*/
public class DefaultSimilarityFactory extends SimilarityFactory {
- private boolean discountOverlaps;
+ protected boolean discountOverlaps;
@Override
public void init(SolrParams params) {
Added: lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java (added)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/search/similarities/SweetSpotSimilarityFactory.java Wed Feb 27 01:58:07 2013
@@ -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);
+ }
+ };
+}
Added: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml (added)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-both-tf.xml Wed Feb 27 01:58:07 2013
@@ -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>
Added: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml (added)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-baseline.xml Wed Feb 27 01:58:07 2013
@@ -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>
Added: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml (added)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-hyperbolic.xml Wed Feb 27 01:58:07 2013
@@ -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>
Added: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml (added)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/bad-schema-sweetspot-partial-norms.xml Wed Feb 27 01:58:07 2013
@@ -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>
Added: lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml (added)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/collection1/conf/schema-sweetspot.xml Wed Feb 27 01:58:07 2013
@@ -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>
Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java?rev=1450577&r1=1450576&r2=1450577&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java Wed Feb 27 01:58:07 2013
@@ -89,4 +89,14 @@ public class BadIndexSchemaTest extends
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");
+ }
+
}
Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java?rev=1450577&r1=1450576&r2=1450577&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/BaseSimilarityTestCase.java Wed Feb 27 01:58:07 2013
@@ -37,4 +37,17 @@ public abstract class BaseSimilarityTest
}
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);
+ }
}
Added: lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java?rev=1450577&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java (added)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/search/similarities/TestSweetSpotSimilarityFactory.java Wed Feb 27 01:58:07 2013
@@ -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));
+ }
+}