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/10/22 11:58:49 UTC

svn commit: r587050 - in /lucene/java/trunk: ./ src/java/org/apache/lucene/search/ src/java/org/apache/lucene/search/function/ src/test/org/apache/lucene/search/ src/test/org/apache/lucene/search/payloads/

Author: doronc
Date: Mon Oct 22 02:58:48 2007
New Revision: 587050

URL: http://svn.apache.org/viewvc?rev=587050&view=rev
Log:
LUCENE-1028: Fixed Weight serialization for few queries.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java
    lucene/java/trunk/src/java/org/apache/lucene/search/function/FieldCacheSource.java
    lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/payloads/TestBoostingTermQuery.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Mon Oct 22 02:58:48 2007
@@ -127,6 +127,11 @@
     sharing an index over NFS, can be writers in quick succession.
     (Patrick Kimber vis Mike McCandless)
 
+21. LUCENE-1028: Fixed Weight serialization for few queries:
+    DisjunctionMaxQuery, ValueSourceQuery, CustomScoreQuery.
+    Serialization check added for all queries.
+    (Kyle Maxwell via Doron Cohen)
+    
 New features
 
  1. LUCENE-906: Elision filter for French.

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java Mon Oct 22 02:58:48 2007
@@ -89,12 +89,12 @@
   /* The Weight for DisjunctionMaxQuery's, used to normalize, score and explain these queries */
   private class DisjunctionMaxWeight implements Weight {
 
-    private Searcher searcher;       // The searcher with which we are associated.
+    private Similarity similarity;   // The similarity which we are associated.
     private ArrayList weights = new ArrayList();  // The Weight's for our subqueries, in 1-1 correspondence with disjuncts
 
     /* Construct the Weight for this Query searched by searcher.  Recursively construct subquery weights. */
     public DisjunctionMaxWeight(Searcher searcher) throws IOException {
-      this.searcher = searcher;
+      this.similarity = searcher.getSimilarity();
       for (int i = 0; i < disjuncts.size(); i++)
         weights.add(((Query) disjuncts.get(i)).createWeight(searcher));
     }
@@ -125,7 +125,7 @@
 
     /* Create the scorer used to score our associated DisjunctionMaxQuery */
     public Scorer scorer(IndexReader reader) throws IOException {
-      DisjunctionMaxScorer result = new DisjunctionMaxScorer(tieBreakerMultiplier, getSimilarity(searcher));
+      DisjunctionMaxScorer result = new DisjunctionMaxScorer(tieBreakerMultiplier, similarity);
       for (int i = 0 ; i < weights.size(); i++) {
         Weight w = (Weight) weights.get(i);
         Scorer subScorer = w.scorer(reader);

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/function/CustomScoreQuery.java Mon Oct 22 02:58:48 2007
@@ -172,13 +172,13 @@
   //=========================== W E I G H T ============================
   
   private class CustomWeight implements Weight {
-    Searcher searcher;
+    Similarity similarity;
     Weight subQueryWeight;
     Weight valSrcWeight; // optional
     boolean qStrict;
 
     public CustomWeight(Searcher searcher) throws IOException {
-      this.searcher = searcher;
+      this.similarity = getSimilarity(searcher);
       this.subQueryWeight = subQuery.weight(searcher); 
       if (valSrcQuery!=null) {
         this.valSrcWeight = valSrcQuery.createWeight(searcher);
@@ -227,7 +227,7 @@
     public Scorer scorer(IndexReader reader) throws IOException {
       Scorer subQueryScorer = subQueryWeight.scorer(reader);
       Scorer valSrcScorer = (valSrcWeight==null ? null : valSrcWeight.scorer(reader));
-      return new CustomScorer(getSimilarity(searcher), reader, this, subQueryScorer, valSrcScorer);
+      return new CustomScorer(similarity, reader, this, subQueryScorer, valSrcScorer);
     }
 
     /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/function/FieldCacheSource.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/function/FieldCacheSource.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/function/FieldCacheSource.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/function/FieldCacheSource.java Mon Oct 22 02:58:48 2007
@@ -43,7 +43,6 @@
  */
 public abstract class FieldCacheSource extends ValueSource {
   private String field;
-  private FieldCache cache = FieldCache.DEFAULT;
 
   /**
    * Create a cached field source for the input field.  
@@ -54,7 +53,7 @@
 
   /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
   public final DocValues getValues(IndexReader reader) throws IOException {
-    return getCachedFieldValues(cache, field, reader);
+    return getCachedFieldValues(FieldCache.DEFAULT, field, reader);
   }
 
   /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
@@ -77,7 +76,6 @@
     }
     FieldCacheSource other = (FieldCacheSource) o;
     return 
-      this.cache == other.cache &&
       this.field.equals(other.field) && 
       cachedFieldSourceEquals(other);
   }
@@ -85,7 +83,6 @@
   /*(non-Javadoc) @see java.lang.Object#hashCode() */
   public final int hashCode() {
     return 
-      cache.hashCode() + 
       field.hashCode() +
       cachedFieldSourceHashCode();
   }

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/function/ValueSourceQuery.java Mon Oct 22 02:58:48 2007
@@ -62,12 +62,12 @@
   }
 
   private class ValueSourceWeight implements Weight {
-    Searcher searcher;
+    Similarity similarity;
     float queryNorm;
     float queryWeight;
 
     public ValueSourceWeight(Searcher searcher) {
-      this.searcher = searcher;
+      this.similarity = getSimilarity(searcher);
     }
 
     /*(non-Javadoc) @see org.apache.lucene.search.Weight#getQuery() */
@@ -94,7 +94,7 @@
 
     /*(non-Javadoc) @see org.apache.lucene.search.Weight#scorer(org.apache.lucene.index.IndexReader) */
     public Scorer scorer(IndexReader reader) throws IOException {
-      return new ValueSourceScorer(getSimilarity(searcher), reader, this);
+      return new ValueSourceScorer(similarity, reader, this);
     }
 
     /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/QueryUtils.java Mon Oct 22 02:58:48 2007
@@ -2,7 +2,11 @@
 
 import junit.framework.TestCase;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 
 /**
  * Copyright 2005 Apache Software Foundation
@@ -86,11 +90,37 @@
           checkSkipTo(q1,is);
         }
         checkExplanations(q1,s);
+        checkSerialization(q1,s);
       }
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
+
+  /** check that the query weight is serializable. 
+   * @throws IOException if serialization check fail. 
+   */
+  private static void checkSerialization(Query q, Searcher s) throws IOException {
+    Weight w = q.weight(s);
+    try {
+      ByteArrayOutputStream bos = new ByteArrayOutputStream();
+      ObjectOutputStream oos = new ObjectOutputStream(bos);
+      oos.writeObject(w);
+      oos.close();
+      ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
+      Weight w2 = (Weight) ois.readObject();
+      ois.close();
+      
+      //skip rquals() test for now - most weights don't overide equals() and we won't add this just for the tests.
+      //TestCase.assertEquals("writeObject(w) != w.  ("+w+")",w2,w);   
+      
+    } catch (Exception e) {
+      IOException e2 = new IOException("Serialization failed for "+w);
+      e2.initCause(e);
+      throw e2;
+    }
+  }
+
 
   /** alternate scorer skipTo(),skipTo(),next(),next(),skipTo(),skipTo(), etc
    * and ensure a hitcollector receives same docs and scores

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java Mon Oct 22 02:58:48 2007
@@ -19,24 +19,6 @@
 
 import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.lucene.search.spans.*;
-import org.apache.lucene.store.RAMDirectory;
-
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.Term;
-
-import org.apache.lucene.analysis.WhitespaceAnalyzer;
-
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-
-import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.queryParser.ParseException;
-
-import junit.framework.TestCase;
-
-import java.util.Random;
-import java.util.BitSet;
 
 /**
  * TestExplanations subclass that builds up super crazy complex queries
@@ -51,11 +33,16 @@
    */
   public void setUp() throws Exception {
     super.setUp();
-    searcher.setSimilarity(new DefaultSimilarity() {
+    searcher.setSimilarity(createQnorm1Similarity());
+  }
+
+  // must be static for weight serialization tests 
+  private static DefaultSimilarity createQnorm1Similarity() {
+    return new DefaultSimilarity() {
         public float queryNorm(float sumOfSquaredWeights) {
           return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);
         }
-      });
+      };
   }
 
   

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestFilteredQuery.java Mon Oct 22 02:58:48 2007
@@ -17,7 +17,6 @@
  * limitations under the License.
  */
 
-import junit.framework.TestCase;
 import org.apache.lucene.analysis.WhitespaceAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
@@ -26,6 +25,8 @@
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.LuceneTestCase;
+
 import java.util.BitSet;
 
 /**
@@ -38,7 +39,7 @@
  * @since   1.4
  */
 public class TestFilteredQuery
-extends TestCase {
+extends LuceneTestCase {
 
   private IndexSearcher searcher;
   private RAMDirectory directory;
@@ -75,7 +76,12 @@
 
     searcher = new IndexSearcher (directory);
     query = new TermQuery (new Term ("field", "three"));
-    filter = new Filter() {
+    filter = newStaticFilterB();
+  }
+
+  // must be static for serialization tests
+  private static Filter newStaticFilterB() {
+    return new Filter() {
       public BitSet bits (IndexReader reader) {
         BitSet bitset = new BitSet(5);
         bitset.set (1);
@@ -120,13 +126,7 @@
     QueryUtils.check(filteredquery,searcher);
     
     // test boost
-    Filter f = new Filter() {
-      public BitSet bits (IndexReader reader) {
-        BitSet bitset = new BitSet(5);
-        bitset.set(0, 5);
-        return bitset;
-      }
-    };
+    Filter f = newStaticFilterA();
     
     float boost = 2.5f;
     BooleanQuery bq1 = new BooleanQuery();
@@ -145,6 +145,17 @@
     
     assertEquals(boost, filteredquery.getBoost(), 0);
     assertEquals(1.0f, tq.getBoost(), 0); // the boost value of the underlying query shouldn't have changed 
+  }
+
+  // must be static for serialization tests 
+  private static Filter newStaticFilterA() {
+    return new Filter() {
+      public BitSet bits (IndexReader reader) {
+        BitSet bitset = new BitSet(5);
+        bitset.set(0, 5);
+        return bitset;
+      }
+    };
   }
   
   /**

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/payloads/TestBoostingTermQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/payloads/TestBoostingTermQuery.java?rev=587050&r1=587049&r2=587050&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/payloads/TestBoostingTermQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/payloads/TestBoostingTermQuery.java Mon Oct 22 02:58:48 2007
@@ -192,7 +192,8 @@
     CheckHits.checkHitCollector(query, "noPayLoad", searcher, results);
   }
 
-  class BoostingSimilarity extends DefaultSimilarity {
+  // must be static for weight serialization tests 
+  static class BoostingSimilarity extends DefaultSimilarity {
 
     // TODO: Remove warning after API has been finalized
     public float scorePayload(byte[] payload, int offset, int length) {