You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by do...@apache.org on 2007/04/25 23:10:45 UTC

svn commit: r532487 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/MultiSearcher.java src/test/org/apache/lucene/search/TestMultiSearcher.java

Author: doronc
Date: Wed Apr 25 14:10:43 2007
New Revision: 532487

URL: http://svn.apache.org/viewvc?view=rev&rev=532487
Log:
LUCENE-789: fix MultiSearcher to not ignore its custom similarity.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiSearcher.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?view=diff&rev=532487&r1=532486&r2=532487
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Wed Apr 25 14:10:43 2007
@@ -105,6 +105,11 @@
 13. LUCENE-736: Sloppy phrase query with repeating terms matches wrong docs.
     For example query "B C B"~2 matches the doc "A B C D E". (Doron Cohen)
     
+14. LUCENE-789: Fixed: custom similarity is ignored when using MultiSearcher (problem reported 
+    by Alexey Lef). Now the similarity applied by MultiSearcer.setSimilarity(sim) is being used. 
+    Note that as before this fix, creating a multiSearcher from Searchers for whom custom similarity 
+    was set has no effect - it is masked by the similarity of the MultiSearcher. This is as 
+    designed, because MultiSearcher operates on Searchables (not Searchers). (Doron Cohen)
 
 New features
 

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java?view=diff&rev=532487&r1=532486&r2=532487
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MultiSearcher.java Wed Apr 25 14:10:43 2007
@@ -43,9 +43,10 @@
     private Map dfMap; // Map from Terms to corresponding doc freqs
     private int maxDoc; // document count
 
-    public CachedDfSource(Map dfMap, int maxDoc) {
+    public CachedDfSource(Map dfMap, int maxDoc, Similarity similarity) {
       this.dfMap = dfMap;
       this.maxDoc = maxDoc;
+      setSimilarity(similarity);
     }
 
     public int docFreq(Term term) {
@@ -106,7 +107,7 @@
     public TopFieldDocs search(Weight weight,Filter filter,int n,Sort sort) {
       throw new UnsupportedOperationException();
     }
-  };
+  }
 
 
   private Searchable[] searchables;
@@ -320,7 +321,7 @@
 
     // step4
     int numDocs = maxDoc();
-    CachedDfSource cacheSim = new CachedDfSource(dfMap, numDocs);
+    CachedDfSource cacheSim = new CachedDfSource(dfMap, numDocs, getSimilarity());
 
     return rewrittenQuery.weight(cacheSim);
   }

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiSearcher.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiSearcher.java?view=diff&rev=532487&r1=532486&r2=532487
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiSearcher.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestMultiSearcher.java Wed Apr 25 14:10:43 2007
@@ -354,4 +354,42 @@
         ramDirectory1.close();
         ramDirectory2.close();
     }
+    
+    /**
+     * test that custom similarity is in effect when using MultiSearcher (LUCENE-789).
+     * @throws IOException 
+     */
+    public void testCustomSimilarity () throws IOException {
+        RAMDirectory dir = new RAMDirectory();
+        initIndex(dir, 10, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...
+        IndexSearcher srchr = new IndexSearcher(dir);
+        MultiSearcher msrchr = getMultiSearcherInstance(new Searcher[]{srchr});
+        
+        Similarity customSimilarity = new DefaultSimilarity() {
+            // overide all
+            public float idf(int docFreq, int numDocs) { return 100.0f; }
+            public float coord(int overlap, int maxOverlap) { return 1.0f; }
+            public float lengthNorm(String fieldName, int numTokens) { return 1.0f; }
+            public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
+            public float sloppyFreq(int distance) { return 1.0f; }
+            public float tf(float freq) { return 1.0f; }
+        };
+        
+        srchr.setSimilarity(customSimilarity);
+        msrchr.setSimilarity(customSimilarity);
+  
+        Query query=new TermQuery(new Term("contents", "doc0"));
+  
+        // Get a score from IndexSearcher
+        TopDocs topDocs = srchr.search(query, null, 1);
+        float score1 = topDocs.getMaxScore();
+        
+        // Get the score from MultiSearcher
+        topDocs = msrchr.search(query, null, 1);
+        float scoreN = topDocs.getMaxScore();
+        
+        // The scores from the IndexSearcher and Multisearcher should be the same
+        // if the same similarity is used.
+        assertEquals("MultiSearcher score must be equal to single esrcher score!", score1, scoreN, 1e-6);
+    }
 }