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

cpoerschke pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 316cfc5c229 SOLR-17626: add RawTFSimilarityFactory class (#2715)
316cfc5c229 is described below

commit 316cfc5c22973e3598c79fad540242bd426c0841
Author: Christine Poerschke <[email protected]>
AuthorDate: Fri Jan 24 10:45:22 2025 +0000

    SOLR-17626: add RawTFSimilarityFactory class (#2715)
    
    (cherry picked from commit b3b20d6564885e96f3881c6acea577be98be9a64)
---
 solr/CHANGES.txt                                   |  2 +
 .../similarities/RawTFSimilarityFactory.java       | 51 ++++++++++++++++++++++
 .../solr/collection1/conf/schema-rawtf.xml         | 51 ++++++++++++++++++++++
 .../similarities/TestRawTFSimilarityFactory.java   | 50 +++++++++++++++++++++
 4 files changed, 154 insertions(+)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 2342e1480f2..af4b26d6ac0 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -11,6 +11,8 @@ New Features
   fetching and computing it on the fly.  To avoid a backwards compatibilty 
concern, this won't work
   for wt=javabin.  (Matthew Biscocho, David Smiley)
 
+* SOLR-17626: Add RawTFSimilarityFactory class. (Christine Poerschke)
+
 Improvements
 ---------------------
 * SOLR-15751: The v2 API now has parity with the v1 "COLSTATUS" and "segments" 
APIs, which can be used to fetch detailed information about 
diff --git 
a/solr/core/src/java/org/apache/solr/search/similarities/RawTFSimilarityFactory.java
 
b/solr/core/src/java/org/apache/solr/search/similarities/RawTFSimilarityFactory.java
new file mode 100644
index 00000000000..2b8d12b7764
--- /dev/null
+++ 
b/solr/core/src/java/org/apache/solr/search/similarities/RawTFSimilarityFactory.java
@@ -0,0 +1,51 @@
+/*
+ * 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.search.similarities;
+
+import org.apache.lucene.search.similarities.RawTFSimilarity;
+import org.apache.lucene.search.similarities.Similarity;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.schema.SimilarityFactory;
+
+/**
+ * Factory for RawTFSimilarity.
+ *
+ * <p>Parameters:
+ *
+ * <ul>
+ *   <li>discountOverlaps (bool): True if overlap tokens (tokens with a 
position of increment of
+ *       zero) are discounted from the document's length. The default is 
<code>true</code>
+ * </ul>
+ *
+ * @lucene.experimental
+ * @since 9.9.0
+ */
+public class RawTFSimilarityFactory extends SimilarityFactory {
+  private RawTFSimilarity similarity;
+
+  @Override
+  public void init(SolrParams params) {
+    super.init(params);
+    boolean discountOverlaps = params.getBool("discountOverlaps", true);
+    similarity = new RawTFSimilarity(discountOverlaps);
+  }
+
+  @Override
+  public Similarity getSimilarity() {
+    return similarity;
+  }
+}
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-rawtf.xml 
b/solr/core/src/test-files/solr/collection1/conf/schema-rawtf.xml
new file mode 100644
index 00000000000..710ac129b26
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/schema-rawtf.xml
@@ -0,0 +1,51 @@
+<?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 RawTFSimilarityFactory -->
+
+<schema name="test" version="1.7">
+  <fieldType name="string" class="solr.StrField" omitNorms="true" 
positionIncrementGap="0"/>
+
+  <!-- default parameters -->
+  <fieldType name="text" class="solr.TextField">
+    <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
+    <similarity class="solr.RawTFSimilarityFactory"/>
+  </fieldType>
+
+  <!-- with parameters -->
+  <fieldType name="text_params_0" class="solr.TextField">
+    <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
+    <similarity class="solr.RawTFSimilarityFactory">
+      <bool name="discountOverlaps">false</bool>
+    </similarity>
+  </fieldType>
+  <fieldType name="text_params_1" class="solr.TextField">
+    <analyzer class="org.apache.lucene.analysis.standard.StandardAnalyzer"/>
+    <similarity class="solr.RawTFSimilarityFactory">
+      <bool name="discountOverlaps">true</bool>
+    </similarity>
+  </fieldType>
+
+  <field name="id" type="string" indexed="true" stored="true" 
multiValued="false" required="false"/>
+  <field name="text" type="text" indexed="true" stored="false"/>
+  <field name="text_params_0" type="text_params_0" indexed="true" 
stored="false"/>
+  <field name="text_params_1" type="text_params_1" indexed="true" 
stored="false"/>
+
+  <uniqueKey>id</uniqueKey>
+
+</schema>
diff --git 
a/solr/core/src/test/org/apache/solr/search/similarities/TestRawTFSimilarityFactory.java
 
b/solr/core/src/test/org/apache/solr/search/similarities/TestRawTFSimilarityFactory.java
new file mode 100644
index 00000000000..1630edc34b9
--- /dev/null
+++ 
b/solr/core/src/test/org/apache/solr/search/similarities/TestRawTFSimilarityFactory.java
@@ -0,0 +1,50 @@
+/*
+ * 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.search.similarities;
+
+import org.apache.lucene.search.similarities.RawTFSimilarity;
+import org.apache.lucene.search.similarities.Similarity;
+import org.junit.BeforeClass;
+
+/** Tests {@link RawTFSimilarityFactory} */
+public class TestRawTFSimilarityFactory extends BaseSimilarityTestCase {
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    initCore("solrconfig-basic.xml", "schema-rawtf.xml");
+  }
+
+  public void test() {
+    Similarity sim = getSimilarity("text");
+    assertEquals(RawTFSimilarity.class, sim.getClass());
+    RawTFSimilarity rtfSim = (RawTFSimilarity) sim;
+    assertTrue(rtfSim.getDiscountOverlaps());
+  }
+
+  public void testParameters0() {
+    Similarity sim = getSimilarity("text_params_0");
+    assertEquals(RawTFSimilarity.class, sim.getClass());
+    RawTFSimilarity rtfSim = (RawTFSimilarity) sim;
+    assertFalse(rtfSim.getDiscountOverlaps());
+  }
+
+  public void testParameters1() {
+    Similarity sim = getSimilarity("text_params_1");
+    assertEquals(RawTFSimilarity.class, sim.getClass());
+    RawTFSimilarity rtfSim = (RawTFSimilarity) sim;
+    assertTrue(rtfSim.getDiscountOverlaps());
+  }
+}

Reply via email to