Solr 6.3扩展DefaultSimilarity类重写tf()方法的相关咨询
Hey there! Let's walk through exactly what you need to know to extend DefaultSimilarity and override its tf() method in Solr 6.3.
1. Where to find the DefaultSimilarity class
The DefaultSimilarity class lives in the Lucene core library, which is a foundational dependency for Solr. Specifically, it's located in the package:org.apache.lucene.search.similarities
You can locate it in two common ways:
- From Solr's installed files: Navigate to your Solr installation's
server/libdirectory, find thelucene-core-6.3.0.jarfile. If you unzip this jar, you'll find the class file under the path corresponding to the package structure (org/apache/lucene/search/similarities/DefaultSimilarity.class). - In an IDE with Maven/Gradle: If you're building a custom plugin via a build tool, add Solr's 6.3.0 dependency (for Maven, use
org.apache.solr:solr-core:6.3.0), then you can directly import theorg.apache.lucene.search.similarities.DefaultSimilarityclass in your code.
2. Where to place your custom Similarity class
Best practice is to create this as a standalone Java project (not modifying Solr's core code directly—this makes upgrades way easier). Here's how to structure it:
- Create a custom package for your class (e.g.,
com.yourteam.solr.custom.similarities) to avoid namespace conflicts. - Write your custom class, extending
DefaultSimilarityand overriding thetf()method:package com.yourteam.solr.custom.similarities; import org.apache.lucene.search.similarities.DefaultSimilarity; public class CustomTFSimilarity extends DefaultSimilarity { @Override public float tf(float freq) { // Your custom TF calculation logic here return (float) Math.sqrt(freq); // Example: override to use square root instead of default } } - Package this class into a jar file (using Maven, Gradle, or even a simple
javac+jarcommand). - Place the jar in one of these locations depending on your needs:
- Global use across all Solr cores: Put the jar in Solr's
server/solr/libdirectory. - Use for a single core only: Create a
libfolder inside your target core's directory (e.g.,server/solr/your_core/lib) and place the jar there.
- Global use across all Solr cores: Put the jar in Solr's
Quick note on configuration
Once your jar is in place, don't forget to update your core's solrconfig.xml to use your custom similarity class:
<similarity class="com.yourteam.solr.custom.similarities.CustomTFSimilarity"/>
Restart Solr, and your custom TF calculation will be active!
内容的提问来源于stack exchange,提问作者abhay patel

