Shah, Vineel wrote:
This is easy to do with the Similarity API. Just define all of the methods to return 1.0, except queryNorm(), which, for your purposes, should return the inverse of the value passed.Here's what I'm trying to do: A query that looks for for "java unix windows" in the "keywords" field of an index.If the document has "java unix", the score is .66..., regardless of any other factor. I want 1.0 for all three, .33... for just one, and no hit for none.
I've attached a demonstration that implements the scoring you desire.
Doug
import org.apache.lucene.index.*; import org.apache.lucene.search.*; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.analysis.SimpleAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import java.util.Vector; public class SimilarityDemo { public static class DemoSimilarity extends Similarity { public float lengthNorm(String field, int numTerms) { return 1.0f; } public float queryNorm(float sum) { return 1.0f / sum; } public float tf(float freq) { return 1.0f; } public float sloppyFreq(int distance) { return 1.0f; } public float idf(Vector terms, Searcher searcher) { return 1.0f; } public float idf(int docFreq, int numDocs) { return 1.0f; } public float coord(int overlap, int maxOverlap) { return 1.0f; } } public static void main(String[] args) throws Exception { RAMDirectory store = new RAMDirectory(); IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true); writer.setSimilarity(new DemoSimilarity()); Document d0 = new Document(); d0.add(Field.Text("keywords", "java unix windows")); writer.addDocument(d0); Document d1 = new Document(); d1.add(Field.Text("keywords", "java unix")); writer.addDocument(d1); Document d2 = new Document(); d2.add(Field.Text("keywords", "java and several other keywords")); writer.addDocument(d2); Document d3 = new Document(); d3.add(Field.Text("keywords", "no matching keywords")); writer.addDocument(d3); writer.optimize(); writer.close(); final float[] scores = new float[4]; Searcher searcher = new IndexSearcher(store); searcher.setSimilarity(new DemoSimilarity()); Term java = new Term("keywords", "java"); Term unix = new Term("keywords", "unix"); Term windows = new Term("keywords", "windows"); BooleanQuery bq = new BooleanQuery(); bq.add(new TermQuery(java), false, false); bq.add(new TermQuery(unix), false, false); bq.add(new TermQuery(windows), false, false); //System.out.println(bq.toString("keywords")); searcher.search (bq, new HitCollector() { public final void collect(int doc, float score) { System.out.println("Doc=" + doc + " score=" + score); } }); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>